-3

I'm trying to make a program that reads in an external .txt file and manipulates it. The file has 5 different groups of data, 4 lines each (2 are int, 2 string). I need to read in the file using the Scanner class, Make an object to hold each group of data (write a class which stores the data group as a single object (lets call it ProgramData)). Then I need to create a ProgamData object and put that into an ArrayList, and repeat for each of the 5 groups.

I have a text file, and I read it in with the Scanner (I confirmed that I did this right through printing on the command line). I'm completely lost from there. Any help at all would be greatly appreciated.

Not like this will help, but here's my code so far:

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

public class Project1
{
    public static void main (String[] args) throws IOException 
    {

        File dataFile = new File("C:\\Users/data.txt");
        Scanner fileReader = new Scanner(dataFile);

        int firstLine = fileReader.nextInt();
        int secondLine = fileReader.nextInt();
        String whiteSpace = fileReader.nextLine();
        String thirdLine = fileReader.nextLine();
        String fourthLine = fileReader.nextLine();

        ArrayList<String> newArray = new ArrayList<String>();

    }


}   
user2813048
  • 11
  • 1
  • 1
  • 2
    You have to define a class `ProgramData` and create instances of it for each set of data from the file. The `ArrayList` should be `ArrayList`. We're not going to write the code for you, but this should point you in the right direction. – Jim Garrison Sep 24 '13 at 23:00
  • you will need to tokenize your String (which now represents your text file). Tokenize it based on something predictable, like commas if it's a CSV, tab's if its a Text-Tab-Delimited file, etc. Or if everything is on it's own line, tokenize by new line characters (\r\n on windows, and \n on *nix's) – SnakeDoc Sep 24 '13 at 23:00
  • 1
    So you've done the easy part... You need to have a minimal understanding of the problem and ask for help on specific help in order for us to help you. – Grammin Sep 24 '13 at 23:01

2 Answers2

0

Make sure when you're reading the input file, use the Scanner class's hasNext() method. It detects if there is still a line in the file so you don't reach the end of the file. Use it like so

 // get file input
 // this will make sure there are still lines left within the 
 // file and that you have not reached the end of the file
 while(fileReader.hasNext()) {

     int firstLine = fileReader.nextInt();
    int secondLine = fileReader.nextInt();
    String whiteSpace = fileReader.nextLine();
    String thirdLine = fileReader.nextLine();
    String fourthLine = fileReader.nextLine();

}

You need to take the provided above to do the operations you are looking for.

user2277872
  • 2,896
  • 1
  • 18
  • 21
0

Here are the steps you can follow:

  1. Create a class named as ProgramData
  2. Make a constructor which will accept your group data. --> What is constructor
  3. Now in Project1 Class read the file properly. --> Scanner Tutorial and Reading a txt file using scanner java

  4. Once you get all the first group data from file pass it to ProgramData class and create instance something like

      ProgramData pd1 = new ProgramData (/* list of parameter */)
    
  5. Add that ProgramData instace to Arraylist like below

     //  Define Arraylilst 
     ArrayList<ProgramData > list= new ArrayList<ProgramData >();
    
     // Do some operation like reading or collecting the data and creating object 
     // shown in step 4
    
     list.add(pd1); // add new object of group to list.
    

I hope this will help you to achieve your goal. If you have any question just ask. Good luck

Community
  • 1
  • 1
Smit
  • 4,617
  • 1
  • 22
  • 27