Beginner, Reading from file error
Hello, I have just done a program in Java that reads from a file, stores the values and the prints them, though I have a small problem.
Code :
public class Read {
public static void main(String[] args) throws Exception {
File testFile = new File("me.txt");
Scanner scan = new Scanner(testFile);
String name;
int phone;
name = scan.nextLine();
phone = scan.nextInt();
System.out.println("Name: " + name);
System.out.println("Phone: " + phone);
scan.close();
}
}
I get the error:
Code :
Exception in thread "main" java.util.InputMismatchException: For input string: "07983857686"
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at week3.ReadMe.main(ReadMe.java:28)
If I change the phone number from int to String it works, though I wan't to know why? The phone numbers is made up of only numbers, so It must be due to the 0 in front?
Re: Beginner, Reading from file error
Quote:
Originally Posted by
Lynce
If I change the phone number from int to String it works, though I wan't to know why? The phone numbers is made up of only numbers, so It must be due to the 0 in front?
Did you try it without the 0? What happened?
Re: Beginner, Reading from file error
Try to use FileReader instead of File, and come-back with an update. I know for sure that with FileReader should work because I used it recently.
application context