Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Organizing this...
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
.:hacker:.




PostPosted: Mon May 16, 2005 6:40 pm   Post subject: Organizing this...

Hi, it's me again and I have a couple quick questions Very Happy .

Let's say I have number data on an editpad/notepad file, how would I organize that into a table (with text borders) using a 2-D array? I've got the thing to compile and all, but it only prints out one line:

code:
public class Assign5
{
    public static void ArrayOne (String [] args) throws IOException
    {
    int [][] precipitation = new int [5][7];
         
        for (int i = 0; i < 5; i++)
        {
        for (int j = 0; j<3; j++)
        {
        precipitation [5][7] = 0;
        }
            }
        }
   
    // The main method
    public static void main (String [] args) throws IOException
        {
     
        // variables for the averages
        int averAll;
        int averNorth;
        int averApril;
        int highJune;
        int lowest;
        int secondlow;
     
        FileInputStream precip;
        DataInputStream In;
        String fileInput = "";
     
    try
        {
        precip = new FileInputStream("precipitation.txt");
       
        In = new DataInputStream (precip);
       
        fileInput = In.readLine();
        System.out.print (fileInput);
       
        In.close();
    }
   
    catch (FileNotFoundException e)
    {
        System.out.println("Error - this file does not exist");
    }
       
    catch (IOException e)
    {
        System.out.println ("error=" + e.toString ());
      }
    }


Or... do I need to change it into an output instead of an input, i'm not sure. I wrote the file like this but it only prints out the months:

Month North East South West
Jan.
Feb.
Mar. (numbers go here)
Apr.
May
Jun.

Also, once I've got the file in there how would I calculate averages and other related things this way...? I'm not completely sure.
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Mon May 16, 2005 7:51 pm   Post subject: (No subject)

Let's try some decently formatted code for starters.

Java:
public class Assign5
{
   public static void ArrayOne (String [] args) throws IOException
   {
      int [][] precipitation = new int [5][7];
         
      for (int i = 0; i < 5; i++)
      {
         for (int j = 0; j < 3; j++)
         {
            precipitation[5][7] = 0;
         }
      }
   }
   
   // The main method
   public static void main (String [] args) throws IOException
   {
       // variables for the averages
       int averAll;
       int averNorth;
       int averApril;
       int highJune;
       int lowest;
       int secondlow;
     
       FileInputStream precip;
       DataInputStream In;
       String fileInput = "";
     
       try
       {
          precip = new FileInputStream("precipitation.txt");
       
          In = new DataInputStream (precip);
       
          fileInput = In.readLine();
          System.out.print (fileInput);
       
          In.close();
       }
       catch (FileNotFoundException e)
       {
          System.out.println("Error - this file does not exist");
       }
       catch (IOException e)
       {
          System.out.println ("error=" + e.toString ());
       }
    }
}


Now...

Java:
      for (int i = 0; i < 5; i++)
      {
         for (int j = 0; j<3; j++)
         {
            precipitation[5][7] = 0;
         }
      }


You're trying to set everything equal to zero. Yet, you're just assigning 0 to precipitation[5][7] over and over.

Your next problem: that doesn't exist. When you declare a 2D array with: [5][7] that means it is five seven element arrays. They start at zer, though, so the highest you can go is 4 and 6.

Java:


Variables in Java always begin with a lowercase letter.

Java:
In.close();


This should go in a "finally" block.
.:hacker:.




PostPosted: Tue May 17, 2005 6:25 am   Post subject: ...

So... if I started at one, woud be able to go higher?

Heh heh... sorry if this sounds like a stupid question.
wtd




PostPosted: Tue May 17, 2005 11:12 am   Post subject: Re: ...

.:hacker:. wrote:
So... if I started at one, woud be able to go higher?

Heh heh... sorry if this sounds like a stupid question.


All arras in Java have a starting index of zero. Can't change it.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: