0

I tried read from file double values and using Scanner with this aim.

It throws InputMismatchException :

"input.txt"  java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:909)
    at java.util.Scanner.next(Scanner.java:1530)
    at java.util.Scanner.nextDouble(Scanner.java:2456)

And I can't understand why this happen?

Code:

public class Largest
{
    public static void main(String[] args)
    throws FileNotFoundException
    {
        String filename = "input.txt"; 
        Scanner in = new Scanner(filename);

        double largest = in.nextDouble();
        while (in.hasNextDouble())
        {
            double input = in.nextDouble();
            if (input > largest)
            {
                largest = input;
            }
        }
        in.close();
        System.out.println("Largest value: " + largest);
    }
}

UPDATE:
I tried change double largest = in.nextDouble(); to double largest = 0;
But it get wrong input:

filename     Actual              Expected
-------------------------------------------------------------
"input.txt"  Largest value: 0.0  Largest value: 1.343239923E9
"input2.txt" Largest value: 0.0  Largest value: 40.1   

File content is like this:

89343455
46746846
56.78
55486411

How to solve this trouble?

7
  • double largest = in.nextDouble(); line causing issue. Input you are getting at this line seems not a double value. Commented Jul 1, 2013 at 14:52
  • @Nambari How to circumvent this issue? Itried make double largest = 0. Is't arises exception but input is wrong - largest = 0.0 Commented Jul 1, 2013 at 14:53
  • Do same check, hasNextDouble(), then only get double. Commented Jul 1, 2013 at 14:54
  • @Ravi Thapliyal It's wrong we can't check largest if this double won't larger then 0. Larger keep being larger all time... Commented Jul 1, 2013 at 14:58
  • 1
    @nazar_art where is the sample input? copy paste the content of input.txt Commented Jul 1, 2013 at 15:08

3 Answers 3

1

I found solution - need to create File object and then feed it to scanner class:

String filename = "input.txt"; 
File newFile = new File(filename);
Scanner in = new Scanner(newFile);
Sign up to request clarification or add additional context in comments.

Comments

0

Try this

import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.Scanner;

public class MainClass{
public static void main(String[] args)
        throws FileNotFoundException
{
    Scanner in = new Scanner(new File("D:\\input.txt"));
    String largestNum=in.next().trim();
    double largest = Double.parseDouble(largestNum);
    while (in.hasNextDouble())
    {
        String Num=in.next().trim();
        double input = Double.parseDouble(Num);
        if (input > largest)
        {
            largest = input;
        }
    }
    in.close();
    System.out.println("Largest value: " + largest);
} }

2 Comments

It's not correct to assume each double is on a separate line; specially, when the OP's code clearly never used a nextLine().
@RaviThapliyal in that case you can use next();
0
  • Initialise largest to the first double (after checking the type)
  • Use the correct delimiter to parse your input (\n = newline)

    String filename = "input.txt"; 
    Scanner in = new Scanner(filename).useDelimiter("\\n");
    
    double largest;
    if (in.hasNextDouble())
        largest = in.nextDouble();
    
    while (in.hasNextDouble())
    {
        double input = in.nextDouble();
        if (input > largest)
        {
            largest = input;
        }
    }
    

4 Comments

It doesn't work Actual is - Largest value: 4.9E-324 and Expected is - Largest value: 1.343239923E9 Doesn't work
Can you also post your file contents?
@nazar_art Check my updated answer. You need to parse your file line by line since every input is on a new one.
This answer is incorrect. new Scanner("input.txt") creates a scanner for reading a string, not a file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.