Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Beginner, Reading from file error

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

    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:

    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?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Beginner, Reading from file error

    Quote Originally Posted by Lynce View Post
    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?

  3. #3
    Member
    Join Date
    Oct 2011
    Posts
    50
    My Mood
    Fine
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default 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
    Last edited by daniel.j2ee; December 13th, 2011 at 05:02 PM.

Similar Threads

  1. Need help with reading from .dat file
    By cyoung in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: April 26th, 2011, 07:34 AM
  2. error with a java game..(beginner)
    By Skat in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 1st, 2010, 02:04 PM
  3. Help with reading from file
    By drazenmd in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: June 15th, 2010, 03:43 AM
  4. Anyone Help me in XML file Reading??????????
    By goplrao in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: May 2nd, 2010, 11:04 AM
  5. [SOLVED] Problem in reading a file from java class
    By aznprdgy in forum File I/O & Other I/O Streams
    Replies: 11
    Last Post: March 23rd, 2009, 09:31 AM