We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

InputMismatch Exception

Sdot718
Sdot718 asked
on
Medium Priority
416 Views
Last Modified: 2012-05-11
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

Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

Commented:
Please post the file called 'data'
Awarded 2011
Awarded 2011

Commented:
Can all pieces in the file be interpreted as double?
Post your input, please

Author

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

Awarded 2011
Awarded 2011

Commented:
the first two items cannnot be double
CERTIFIED EXPERT
Top Expert 2016

Commented:
You need to get rid of everything other than the doubles
Awarded 2011
Awarded 2011

Commented:
you need to skip the first tow with inFile.next()
Awarded 2011
Awarded 2011
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Awarded 2011
Awarded 2011

Commented:
Do all of your lines follow exactly the same pattern?
CERTIFIED EXPERT
Top Expert 2016

Commented:
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

Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.