[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 385
  • Last Modified:

InputMismatch Exception

Trying to get scanner to read double values from a text file and I get this.

Exception in thread "main" java.util.InputMismatchException
        at java.util.Scanner.throwFor(Scanner.java:840)
        at java.util.Scanner.next(Scanner.java:1461)
        at java.util.Scanner.nextDouble(Scanner.java:2387)
        at Array.main(Array.java:24)



public static void main(String[] args) throws FileNotFoundException

	{
		Scanner inFile = new Scanner(new FileReader("data"));

		double[] scores = new double[8];
		int index;

		for(index = 0; index < scores.length; index++)
		scores[index] = inFile.nextDouble();

Open in new window

0
Sdot718
Asked:
Sdot718
  • 5
  • 3
1 Solution
 
CEHJCommented:
Please post the file called 'data'
0
 
for_yanCommented:
Can all pieces in the file be interpreted as double?
Post your input, please
0
 
Sdot718Author Commented:
Data.Text posted in the code.
//Data.txt

Ashley Boyle 	8.2  8.0  9.2  4.6  8.3  8.9  7.0  9.5

Open in new window

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
for_yanCommented:
the first two items cannnot be double
0
 
CEHJCommented:
You need to get rid of everything other than the doubles
0
 
for_yanCommented:
you need to skip the first tow with inFile.next()
0
 
for_yanCommented:
public static void main(String[] args) throws FileNotFoundException

	{
		Scanner inFile = new Scanner(new FileReader("data"));

		double[] scores = new double[6];
		int index;
inFile.next();
inFile.next();

		for(index = 0; index < 6; index++)
		scores[index] = inFile.nextDouble();

Open in new window

0
 
for_yanCommented:
Do all of your lines follow exactly the same pattern?
0
 
CEHJCommented:
A couple of points there:

a. the answer accepted doesn't read all the doubles
b. if you're going to have info in the file that is ignored, it would be more efficient to leave it out in the first place as i mentioned earlier
c. if you're determined to leave the string info in there for some reason, you'll find the following approach more flexible for mixed data:
public static void main(String[] args) throws FileNotFoundException {
        Scanner inFile = new Scanner(new FileReader("data"));

        double[] scores = new double[8];
        int index = 0;
	while(inFile.hasNext()) {
	    if(inFile.hasNextDouble()) {
		scores[index++] = inFile.nextDouble();
	    }
	    else {
		inFile.next();
	    }
	}
	System.out.println(Arrays.toString(scores)); // test
    }

Open in new window

0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now