2

I need help with an assignment I have to do for my class. I have only just started with Java so i'm pretty new with it. I need to write a code that will scan numbers from a text file then calculate them and create some averages. I used a for loop to scan the file but I don't know how to scan it so it skips every third line. I need each column of text rather than each line. The text file contains 3 columns and 7 rows.

public static void main(String [] args){
    readData("Sample.in");
}

static void readData(String fileName){
    try{
        int[] breakfast= new int[7];
        int[] lunch=new int[7];
        int[] dinner= new int[7];

        File input = new File(fileName);        // Creates Scanner to read line from the text file
        Scanner scanLine = new Scanner(input);  // Reads entire line from the file

        String line= scanLine.nextLine();       // To read numbers from the Line buffer

        Scanner scanNumber = new Scanner(line); // to read three numbers from the line buffer

        for(int meal =0; meal<3; meal++){       // Checks whether the number is present in the line
            if(scanNumber.hasNextInt()){        // read number from the line buffer and store it in the calories variable

                int calories = scanNumber.nextInt();
                System.out.print(calories+" ");

            }
        }
        System.out.println("");
    } catch(FileNotFoundException e){                   // catches exception in case the file is missing
        System.out.println(e);
    }
}

Example input file:

200 1000 800 
450 845 1200
800 250 400 
0 1500 1800
600 500 1000
700 1400 1700
675 400 900
Smit
  • 4,685
  • 1
  • 24
  • 28
user2769894
  • 23
  • 1
  • 1
  • 5
  • 1
    What is the purpose of these? `int[] breakfast= new int[3]; int[] lunch=new int[3]; int[] dinner= new int[3];` You may be on to something there, but it doesn't make sense as written - those variables aren't stored to or used. – clwhisk Sep 11 '13 at 18:38
  • Could you please post a sample content of your file? – PM 77-1 Sep 11 '13 at 18:42
  • I dont think you need two Scanner object. One will do the work for you if you scan whole line at once and then split is using appropriate delimiter. when line count becomes 3 skip that line. – Smit Sep 11 '13 at 18:45
  • Sorry its kind of messy, but I want to store the numbers from the txt file into these three arrays. – user2769894 Sep 11 '13 at 18:59
  • Example input file:Every ; is a new line 200 1000 800; 450 845 1200; 800 250 400; 0 1500 1800; 600 500 1000; 700 1400 1700; 675 400 900; – user2769894 Sep 11 '13 at 19:01
  • @user2769894 Please dont add sample info as comment. Its not readable here. I edited and updated the post (use `edit` button to make updates to actual post). Use `@` in front of name so that message could get to that person. – Smit Sep 11 '13 at 19:28
  • @user2769894 From your input file which lines you dont want to read? Also please take a look at my previous comment. Look at http://docs.oracle.com/javase/tutorial/essential/io/scanning.html – Smit Sep 11 '13 at 19:36
  • @smit Thanks for he input, I'm new to this site as well. I Want to eventually read all of the numbers and them put them into the arrays however the first column will go into the first array and second into the second and so on. – user2769894 Sep 11 '13 at 20:45
  • @user2769894 I posted some pointer as answer. Check that out and let me know if you caught somewhere while implementing this. Good Luck. – Smit Sep 11 '13 at 21:42

1 Answers1

1

Here is a non-code solution

  1. Open the file.
  2. Create the scanner.
  3. while (scanner.hasNextLine())
  4. use the scanner and read one line.
  5. split the line using String.split(); // read the api and use the version of split that matches your requirements.
  6. split returns an array. you care about elements 0, 1, and 2.
  7. Convert elements 0, 1, and 2 to an integer (read Integer in the api documentation).
  8. add elements 0, 1, and 2 to Lists.

Some java into:

List<Integer> breakfast = new ArrayList<Integer>();
List<Integer> dinner = new ArrayList<Integer>();
List<Integer> lunch = new ArrayList<Integer>();

// add an element to breakfast:
breakfast.add(blammy);

// Iterate through dinner:
int dinnerTotal = 0
for (Integer current : dinner)
{
    dinnerTotal += current.intValue();
}
DwB
  • 37,124
  • 11
  • 56
  • 82