2 Replies - 34453 Views - Last Post: 26 May 2009 - 12:38 AM Rate Topic: -----

#1 jaybfox4  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 06-April 09

How to read csv File?

Posted 25 May 2009 - 04:13 PM

So i got a csv file that has a bunch of movies in it along with there year, rating, profit, length, and popularity. So some of the lines in the csv file would be like....(all seperated by each cell)

Halloween 1978 R 47 93 7.7
Hard Rain 1998 R 19.819 95 5.2
Men in Black 1997 PG-13 250.147 98 7.4
I Know What You Did Last Summer 1997 R 72.219 100 6.5
Volcano 1997 PG-13 47.474 102 5.8
Chasing Amy 1997 R 12.006 105 7.9
Terminator 1984 R 36.9 108 7.7
Grease 1978 PG 181.28 110 7.3

And my code is not working right, i tried playing around with it, but im not getting anywhere.

I am currently not finding the file in eclipse but i have been testing around in java and im getting....

So i copied all the data in the excel document and put it in Notepad, and i could read the file fine.
But when i tried to read it as a csv file, it gave me this

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:838)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextInt(Scanner.java:2091)
at java.util.Scanner.nextInt(Scanner.java:2050)
at Tester.main(Tester.java:55)

This is the scanner part of my program here....
Scanner s = null;
		 try {
		   s = new Scanner(new File("MOVIES.csv"));
		 } catch (FileNotFoundException e) {
		   System.out.println("File \"Movies.csv\" not found in the current directory.");
		   System.out.println("Exiting the program.");
		   System.exit(0);
		 }
		List<Movie> theList = new ArrayList<Movie>();
		StringBuilder sb = new StringBuilder();
		while (s.hasNext()) {
			while (s.hasNext() && !s.hasNextInt()) {
				sb.append(s.next()).append(" ");
			}
			String title = sb.toString().trim();
			int year = s.nextInt();  //this is line 55
			String rating = s.next();
			double profit  = s.nextDouble();
			int length = s.nextInt();
			double popularity  = s.nextDouble();

			theList.add(new Movie(title, year, rating, profit, length, popularity));
			sb.setLength(0);
		}
				 s.close();



So i dont know why i can read it as a txt file but not a csv file????

I have not been successful with file reading and i could use some help. Thanks

This post has been edited by jaybfox4: 25 May 2009 - 05:31 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How to read csv File?

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 3872
  • View blog
  • Posts: 11,405
  • Joined: 18-April 07

Re: How to read csv File?

Posted 25 May 2009 - 10:08 PM

This code appears fine as long as your file contains complete lines with no line missing a piece of information. Be it a text file or a csv, it should still be the same.

What the error is telling you is that it has read in an element first and then tried to read in more items and found none. So if you had a line in your file that just had the name of the movie but nothing else, then it would cause this error.

You use hasNext fine at the beginning and then in the inner while loop, but you read in that inner loop, so that means you could have potentially read up all the data. So you have to check again hasNext before you go about reading the rest of the line.

Look at your input file, there is something wrong in it that is causing this error. Make sure you have complete data and that all tokens are seen by Java.

Then revise your code to check hasNext before EACH read. You have to make sure there is data there before you attempt to read in another token or else it will error out.

:)
Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2110
  • View blog
  • Posts: 8,776
  • Joined: 20-September 08

Re: How to read csv File?

Posted 26 May 2009 - 12:38 AM

Quote

So i dont know why i can read it as a txt file but not a csv file????


How are you saving the file? A csv file IS a text file, or certainly should be

Attaching the problem file with your post would be a good idea

This post has been edited by g00se: 26 May 2009 - 12:39 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1