Using delimiter when reading a file

88,634

Solution 1

Use read.next() instead of read.nextLine()

   title = read.next();
   category = read.next();
   runningTime = read.next();
   year = read.next();
   price = read.next();

Solution 2

I think you want to call .next() which returns a String instead of .nextLine(). Your .nextLine() call is moving past the current line.

Scanner read = new Scanner (new File("datafile.txt"));
   read.useDelimiter(",");
   String title, category, runningTime, year, price;

   while(read.hasNext())
   {
       title = read.next();
       category = read.next();
       runningTime = read.next();
       year = read.next();
       price = read.next();
     System.out.println(title + " " + category + " " + runningTime + " " + year + " " + price + "\n"); //just for debugging
   }
   read.close();

Solution 3

You should be using next(); where you are using nextLine();

Have a look at the tutorial: http://docs.oracle.com/javase/tutorial/essential/io/scanning.html

Notice the lines:

try {
   s = new Scanner(new BufferedReader(new FileReader("xanadu.txt")));

   while (s.hasNext()) {
   System.out.println(s.next());
}

Solution 4

All of the answers above are right and actually the same. However, one important point everybody should remember that the Scanner has a buffer size of only 1024. That means if the length of the delimited text is more, the parsing will stop.

So, a little enhancement to the given solution, use BufferedReader instead of directly passing the file to the Scanner. Example:

    BufferedReader in = new BufferedReader(new FileReader("datafile.txt"), 16*1024);
    Scanner read = new Scanner(in);
    read.useDelimiter(",");
    String title, category, runningTime, year, price;

    while(read.hasNext())
    {
        title = read.next();
        category = read.next();
        runningTime = read.next();
        year = read.next();
        price = read.next();
        System.out.println(title + " " + category + " " + runningTime + " " + year + " " + price + "\n"); //just for debugging
    }
    read.close();

Solution 5

You can also use a String.split() function to convert the string to an array of strings, then iterate over each of them for your values.

How to convert comma-separated String to ArrayList? see this for more details.

Share:
88,634
Robert Spratlin
Author by

Robert Spratlin

Updated on October 30, 2020

Comments

  • Robert Spratlin
    Robert Spratlin over 3 years

    I have little experience using delimiters and i need to read a text file that stores several objects whose data is stored in single lines separate by commas (","). The seperate strings are then used to create a new object which is added to an arraylist.

    Amadeus,Drama,160 Mins.,1984,14.83
    As Good As It Gets,Drama,139 Mins.,1998,11.3
    Batman,Action,126 Mins.,1989,10.15
    Billy Elliot,Drama,111 Mins.,2001,10.23
    Blade Runner,Science Fiction,117 Mins.,1982,11.98
    Shadowlands,Drama,133 Mins.,1993,9.89
    Shrek,Animation,93 Mins,2001,15.99
    Snatch,Action,103 Mins,2001,20.67
    The Lord of the Rings,Fantasy,178 Mins,2001,25.87
    

    I am using Scanner to read the file, however i get a no line found error and the entire file is stored into one string:

    Scanner read = new Scanner (new File("datafile.txt"));
    read.useDelimiter(",");
    String title, category, runningTime, year, price;
    
    while (read.hasNext())
    {
       title = read.nextLine();
       category = read.nextLine();
       runningTime = read.nextLine();
       year = read.nextLine();
       price = read.nextLine();
       System.out.println(title + " " + category + " " + runningTime + " " +
                          year + " " + price + "\n"); // just for debugging
    }
    read.close();