Java Scanner won't read
I'm trying to read from a file that has information wr开发者_运维百科itten in the format: someName:aDoubleNumber and returns the name and the double. I've written the following scanner, but it doesn't work, any ideas why?
Scanner readFile = new Scanner("text.txt");
readFile.useDelimiter(":");
while (fileScanner.hasNext()) {
String name = readFile.next();
}
You're not reading from a file. You're reading the String "text.txt". You need a file first.
Scanner readFile = new Scanner(new File("text.txt")); // don't forget to catch FileNotFoundException!
readFile.useDelimiter(":|\\n");
while (fileScanner.hasNext()) {
String name = readFile.next();
double value = readFile.nextDouble();
System.out.println(name + " " + value);
}
I took the code from your comment and formatted, and I get this
public class MyProject {
class FileInput {
Scanner readFile = new Scanner(new File("text.txt")); // don't forget to catch FileNotFoundException!
readFile.useDelimiter(":|\\n");
while (fileScanner.hasNext()) {
String name = readFile.next();
double value = readFile.nextDouble();
System.out.println(name + " " + value);
}
}
}
Now you seem to have a problem - perhaps you meant class FileInput
to be public void fileInput() throws Exception
? When I do this, it compiles. Now we need a main()
method to run it! So I add this:
public static void main(String[] args) throws Exception {
MyProject proj = new MyProject();
proj.fileInput();
}
Now when I ran it I actually got an error. This means there was actually a problem in the code I gave you to begin with. Of course, that code was never intended to be copy/pasted, but was more to give an idea of capability. Anyway, the error is:
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 MyProject.fileInput(MyProject.java:9) <--- Where it happened in our code
at MyProject.main(MyProject.java:16)
The line where it happened was on the parseDouble. So, instead, let's try doing it a different way. We can get the double as a raw piece of text, and then parse it into an actual double like this:
while (readFile.hasNext()) {
String name = readFile.next();
String valueStr = readFile.next();
double value = Double.parseDouble(valueStr);
System.out.println(name + " " + value);
}
So the whole completed program is:
import java.util.*;
import java.io.*;
public class MyProject {
public void fileInput() throws Exception {
Scanner readFile = new Scanner(new File("text.txt")); // don't forget to catch FileNotFoundException!
readFile.useDelimiter(":|\\n");
while (readFile.hasNext()) {
String name = readFile.next();
String valueStr = readFile.next();
double value = Double.parseDouble(valueStr);
System.out.println("Name: " + name);
System.out.println("Value: " + value);
System.out.println(""); // blank line
}
}
public static void main(String[] args) throws Exception {
MyProject proj = new MyProject();
proj.fileInput();
}
So for the input file text being:
this:1234.5
that:321.0
the other:0.122
The output was
C:\Documents and Settings\glowcoder\My Documents>java MyProject
Name: this
Value: 1234.5
Name: that
Value: 321.0
Name: the other
Value: 0.122
C:\Documents and Settings\glowcoder\My Documents>java MyProject
}
精彩评论