I tried read from file double values and using Scanner
with this aim.
It throws InputMismatchException :
\"input.txt\" java.util.InputM
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;
}
}
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);
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);
} }