Results 1 to 19 of 19
Like Tree2Likes
  • 2 Post By Norm

Thread: Reading file problem using Scanner

  1. #1
    nfsmwbe's Avatar
    nfsmwbe is offline Member
    Join Date
    Oct 2011
    Posts
    15
    Rep Power
    0

    Default Reading file problem using Scanner

    employee.txt
    Hamidah Hassanuddin;12;3666.75;5
    Auni Zahirah Luqman;10;4000.11;7
    Helmi Hussain;5;2000.23;5
    Amirul Haziq Mohd Yazid;8;1500.10;8
    Maisara Abd Wahub;3;700.12;2

    employee.java
    Java Code:
    import java.io.*;
    import java.util.Scanner;
    public class employee {
    	public static void main(String[] args) throws IOException{
    		Scanner scan = new Scanner(new File("employee.txt"));
    		
    		
    		scan.useDelimiter(";");
    		while(scan.hasNext()){
    			String a = scan.next();
    			int b = scan.nextInt();
    			double c = scan.nextDouble();
    			int d = scan.nextInt();
    			
    			if(b<10){
    				System.out.print(a + " " + c);
    			}
    
    			else{
    				System.out.print(a + " " + c);
    			}
    
    		}
    		scan.close();
    	}
    }
    error message

    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at employee.main(employee.java:13)


    can anyone help me..
    Last edited by Norm; 01-03-2012 at 03:58 PM. Reason: added code tags

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,830
    Rep Power
    19

    Default Re: Reading file problem using Scanner

    Which line is line 13?
    Print out everything that scan reads in as it reads it in as well, so you can tell us which line of the input file it is failing on (though it's likely the first one).

  3. #3
    nfsmwbe's Avatar
    nfsmwbe is offline Member
    Join Date
    Oct 2011
    Posts
    15
    Rep Power
    0

    Default Re: Reading file problem using Scanner

    i got problem in line 11, 12 and 13..
    it only can use string such:
    String b = scan.next();
    String c = scan.next();
    String d = scan.next();

    but i want to use (b<10) in line 13...

    thanks for help

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Reading file problem using Scanner

    Does your code read in 4 Strings ok? What is in the Strings? You need to print out their values.
    This code with Strings is for debugging the problem. Once we see the problem, then we should be able to find a way to read the data as ints.

  5. #5
    nfsmwbe's Avatar
    nfsmwbe is offline Member
    Join Date
    Oct 2011
    Posts
    15
    Rep Power
    0

    Default Re: Reading file problem using Scanner

    i already put employee.txt at top of the page..

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Reading file problem using Scanner

    What we're trying to do is teach you how to debug a program. You need to see what the Scanner methods are doing. One way is read the values in as Strings so there is no InputMismatchException and then print the Strings out to see what is being read.
    You are assuming that the nextInt is reading a "5" and trying to convert it to an int. But there is a problem. What is the problem? We don't know, so we need to debug the code and see.
    To find the problem read Strings and print out the contents of the Strings.
    Fubarable and Tolls like this.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Reading file problem using Scanner

    Have you solved the java.util.InputMismatchException problem?
    Can you explain how you did it so others will know?

  8. #8
    nfsmwbe's Avatar
    nfsmwbe is offline Member
    Join Date
    Oct 2011
    Posts
    15
    Rep Power
    0

    Default Re: Reading file problem using Scanner

    i don't know what is java.util.InputMismatchException..
    can u solve the question..
    i still got problem

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Reading file problem using Scanner

    can u solve the question..
    Please change the code to read Strings and print them out. Copy and paste the printouts here so we can see what the program is doing. When we see the printouts, we can help you solve the problem.

    i don't know what is java.util.InputMismatchException.
    Read the API doc for the Scanner class's method that threw that exception. It is explained there.

  10. #10
    nfsmwbe's Avatar
    nfsmwbe is offline Member
    Join Date
    Oct 2011
    Posts
    15
    Rep Power
    0

    Default Re: Reading file problem using Scanner

    Java Code:
    import java.io.*;
    import java.util.Scanner;
    public class employee2 {
    	public static void main(String[] args) throws IOException{
    		Scanner scan = new Scanner(new File("employee.txt"));
    		
    		
    		scan.useDelimiter(";");
    		while(scan.hasNext()){
    			String a = scan.next();
    			String b = scan.next();
    			String c = scan.next();
    			String d = scan.next();
    				
    			System.out.print(a + " " + b  + " " + c  + " " + d);
    		}
    		scan.close();
    	}
    }
    output:

    Hamidah Hassanuddin 12 3666.75 5
    Auni Zahirah Luqman10 4000.11 7
    Helmi Hussain 52000.23 5
    Amirul Haziq Mohd Yazid 8 1500.108
    Maisara Abd Wahub 3 700.12 2

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Reading file problem using Scanner

    You need to print each of the variables on a separate line so you can tell which was a and which was b and which was c and which was d. The way you printed it I can not tell where the values came from.
    Also you should print the values with delimiting Strings (one in front and one after):
    println("a=" + a + "<");

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Reading file problem using Scanner

    Some observations:
    The posted code used: System.out.print(
    but the output shows that there was a newline ('\n') somewhere in the Strings that were printed.
    The print method does not add newlines to the output!
    Auni Zahirah Luqman10
    Also in this line there is no blank between the name and the number
    Last edited by Norm; 01-04-2012 at 12:18 AM.

  13. #13
    nfsmwbe's Avatar
    nfsmwbe is offline Member
    Join Date
    Oct 2011
    Posts
    15
    Rep Power
    0

    Default Re: Reading file problem using Scanner

    Java Code:
    import java.io.*;
    import java.util.Scanner;
    public class employee2 {
    	public static void main(String[] args) throws IOException{
    		Scanner scan = new Scanner(new File("employee.txt"));
    		
    		int i = 1;
    		scan.useDelimiter(";");
    		while(scan.hasNext()){
    			String a = scan.next();
    			String b = scan.next();
    			String c = scan.next();
    			String d = scan.next();
    				
    			System.out.println("Line " + i);
    			System.out.println("a: " + a);
    			System.out.println("b: " + b);
    			System.out.println("c: " + c);
    			System.out.println("d: " + d);
    			
    			i++;
    		}
    		scan.close();
    	}
    }
    output:
    Line 1
    a: Hamidah Hassanuddin
    b: 12
    c: 3666.75
    d: 5
    Auni Zahirah Luqman
    Line 2
    a: 10
    b: 4000.11
    c: 7
    Helmi Hussain
    d: 5
    Line 3
    a: 2000.23
    b: 5
    Amirul Haziq Mohd Yazid
    c: 8
    d: 1500.10
    Line 4
    a: 8
    Maisara Abd Wahub
    b: 3
    c: 700.12
    d: 2

    i know the problem now..
    it read string d as 5 and the next line 'Auni Zahirah Luqman' because there is no ';' after 5..
    so how to fix it..

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Reading file problem using Scanner

    The problem is the newline is not recognized as a delimiter.
    You need to make a regular expression that has both ; and \n as delimiters. I don't know regular expressions.

    Or you could do it in two steps:
    Scan the line from the file with \n as delimiter
    Scan that line with ; as the delimiter

  15. #15
    nfsmwbe's Avatar
    nfsmwbe is offline Member
    Join Date
    Oct 2011
    Posts
    15
    Rep Power
    0

    Default Re: Reading file problem using Scanner

    Java Code:
    import java.io.*;
    import java.util.Scanner;
    public class employee2 {
    	public static void main(String[] args) throws IOException{
    		Scanner scan = new Scanner(new File("employee.txt"));
    		
    		int i = 1;
    		
    		scan.useDelimiter(";|\n");
    		while(scan.hasNextLine()){
    			String a = scan.next();
    			int b = scan.nextInt();
    			double c = scan.nextDouble();
    			String d = scan.next();
    				
    			System.out.println("Line " + i);
    			System.out.println("a: " + a);
    			System.out.println("b: " + b);
    			System.out.println("c: " + c);
    			System.out.println("d: " + d);
    			
    			i++;
    		}
    		scan.close();
    	}
    }
    output:
    Line 1
    a: Hamidah Hassanuddin
    b: 12
    c: 3666.75
    d: 5

    Line 2
    a: Auni Zahirah Luqman
    b: 10
    c: 4000.11
    d: 7

    Line 3
    a: Helmi Hussain
    b: 5
    c: 2000.23
    d: 5

    Line 4
    a: Amirul Haziq Mohd Yazid
    b: 8
    c: 1500.1
    d: 8

    Line 5
    a: Maisara Abd Wahub
    b: 3
    c: 700.12
    d: 2

    why I can't change String d = scan.next(); to int d = scan.nextInt();?
    why it has 2 new line after d?
    Java Code:
    d: 5
    
    Line 2

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Reading file problem using Scanner

    why it has 2 new line after d?
    To see the contents of the variable you should print the values with delimiting Strings (one in front and one after):
    println("d=" + d + "<");

    What happens with the new regex pattern? Have you tried using d = nextInt() ??
    Last edited by Norm; 01-04-2012 at 01:38 PM.

  17. #17
    nfsmwbe's Avatar
    nfsmwbe is offline Member
    Join Date
    Oct 2011
    Posts
    15
    Rep Power
    0

    Default Got it

    Java Code:
    import java.io.*;
    import java.util.Scanner;
    public class employee2 {
    	public static void main(String[] args) throws IOException{
    		Scanner scan = new Scanner(new File("employee.txt"));
    		
    		int i = 1;
    		
    		scan.useDelimiter(";|\r\n");
    		while(scan.hasNext()){
    			String a = scan.next();
    			int b = scan.nextInt();
    			double c = scan.nextDouble();
    			int d = scan.nextInt();
    				
    			System.out.println("Line " + i);
    			System.out.println("a: " + a);
    			System.out.println("b: " + b);
    			System.out.println("c: " + c);
    			System.out.println("d: " + d);
    			
    			i++;
    		}
    		scan.close();
    	}
    }
    output:
    Line 1
    a: Hamidah Hassanuddin
    b: 12
    c: 3666.75
    d: 5
    Line 2
    a: Auni Zahirah Luqman
    b: 10
    c: 4000.11
    d: 7
    Line 3
    a: Helmi Hussain
    b: 5
    c: 2000.23
    d: 5
    Line 4
    a: Amirul Haziq Mohd Yazid
    b: 8
    c: 1500.1
    d: 8
    Line 5
    a: Maisara Abd Wahub
    b: 3
    c: 700.12
    d: 2
    Last edited by nfsmwbe; 01-04-2012 at 03:19 PM.

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Reading file problem using Scanner

    Good work.

  19. #19
    nfsmwbe's Avatar
    nfsmwbe is offline Member
    Join Date
    Oct 2011
    Posts
    15
    Rep Power
    0

Similar Threads

  1. reading a file problem
    By aianta in forum New To Java
    Replies: 3
    Last Post: 08-03-2011, 07:22 PM
  2. Reading from file problem
    By BillyB in forum New To Java
    Replies: 10
    Last Post: 03-18-2011, 04:09 AM
  3. problem reading file
    By jmoutia in forum New To Java
    Replies: 0
    Last Post: 10-31-2010, 03:19 AM
  4. Problem reading from a file
    By sarapeace in forum New To Java
    Replies: 13
    Last Post: 10-03-2010, 02:08 PM
  5. Reg: File Reading Problem
    By balaji csc in forum New To Java
    Replies: 0
    Last Post: 11-06-2009, 04:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •