5 Replies - 412 Views - Last Post: 18 February 2012 - 06:47 AM Rate Topic: -----

#1 GDubz  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 91
  • Joined: 06-January 12

IO error

Posted 18 February 2012 - 05:44 AM

Getting error...
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at Lab4.loadFile(Lab4.java:53)
at Lab4.main(Lab4.java:25)


public static void loadFile(Scanner in, HashMap<String, Product> productMap) throws IOException
	{
		System.out.println("Enter database file name: ");
		Scanner inFile = new Scanner(new File(in.nextLine()));
		while (inFile.hasNextLine())
		{
			Product p = new Product();
			System.out.println("here");
			p.setName(inFile.nextLine().trim());
			p.setType(inFile.nextLine().trim());
			p.setPrice(inFile.nextDouble());
			p.setStock(inFile.nextInt());
			productMap.put(p.getName(), p);
			
		}
	}



Is This A Good Question/Topic? 0
  • +

Replies To: IO error

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2203
  • View blog
  • Posts: 5,235
  • Joined: 10-September 10

Re: IO error

Posted 18 February 2012 - 05:49 AM

The error suggests the contents of your file do not match your data type being read from it. Compare the nextLine(), nextDouble(), etc. calls to the file's contents and make sure they match. If you still need help, it would help to post the contents of your file.
Was This Post Helpful? 0
  • +
  • -

#3 GDubz  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 91
  • Joined: 06-January 12

Re: IO error

Posted 18 February 2012 - 06:01 AM

I belive it matches, this is the file


The Shawshank Redemption
DVD
19.95
1
Dracula
Book
4.95
1
The Dark Knight
DVD
19.95
1
Lego Ultimate Building Set
Toy
29.95
3
The Girl With The Dragon Tattoo
Book
14.95
1
Iron Man
DVD
19.95
1
Under The Dome
Book
19.95
1

This post has been edited by GDubz: 18 February 2012 - 06:02 AM

Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2203
  • View blog
  • Posts: 5,235
  • Joined: 10-September 10

Re: IO error

Posted 18 February 2012 - 06:11 AM

I think you need to parse the numeric input from a String object to the desired format. So your first number read (the Double where you're getting the error) would be:

p.setPrice( Double.parseDouble( inFile.readLine() ) );
Was This Post Helpful? 0
  • +
  • -

#5 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: IO error

Posted 18 February 2012 - 06:44 AM

The problem is that after reading one loop, the last reading method was nextInt() which do not read the line feed, so the next iteration nextLine() reads the line feed from previous iteration and so it mixed the flow.
To solve the problem, just put nextLine() at the end of the loop like:
p.setStock(inFile.nextInt());  inFile.nextLine(); //add nextLine()


This is the normal problem when you mix nextLine() with other next() methods, better use nextLine() for this case like what Greb showed above instead of nextDouble() and nextInt()

This post has been edited by smohd: 18 February 2012 - 06:47 AM

Was This Post Helpful? 1
  • +
  • -

#6 GDubz  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 91
  • Joined: 06-January 12

Re: IO error

Posted 18 February 2012 - 06:47 AM

that was it smohd, thank you
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1