Results 1 to 19 of 19
- 01-03-2012, 01:19 PM #1
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(); } }
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
- 01-03-2012, 03:49 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
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).
- 01-03-2012, 10:35 PM #3
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
- 01-03-2012, 10:45 PM #4
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.
- 01-03-2012, 11:00 PM #5
Re: Reading file problem using Scanner
i already put employee.txt at top of the page..
- 01-03-2012, 11:05 PM #6
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.
- 01-03-2012, 11:23 PM #7
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?
- 01-03-2012, 11:29 PM #8
Re: Reading file problem using Scanner
i don't know what is java.util.InputMismatchException..
can u solve the question..
i still got problem
- 01-03-2012, 11:34 PM #9
Re: Reading file problem using Scanner
can u solve the question..
i don't know what is java.util.InputMismatchException.
- 01-03-2012, 11:51 PM #10
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(); } }
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
- 01-04-2012, 12:07 AM #11
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 + "<");
- 01-04-2012, 12:16 AM #12
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 numberLast edited by Norm; 01-04-2012 at 12:18 AM.
- 01-04-2012, 12:19 AM #13
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(); } }
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..
- 01-04-2012, 12:25 AM #14
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
- 01-04-2012, 03:20 AM #15
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(); } }
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
- 01-04-2012, 01:35 PM #16
Re: Reading file problem using Scanner
why it has 2 new line after d?
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.
- 01-04-2012, 03:16 PM #17
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(); } }
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: 2Last edited by nfsmwbe; 01-04-2012 at 03:19 PM.
- 01-04-2012, 03:24 PM #18
Re: Reading file problem using Scanner
Good work.
- 01-04-2012, 03:26 PM #19
Similar Threads
-
reading a file problem
By aianta in forum New To JavaReplies: 3Last Post: 08-03-2011, 07:22 PM -
Reading from file problem
By BillyB in forum New To JavaReplies: 10Last Post: 03-18-2011, 04:09 AM -
problem reading file
By jmoutia in forum New To JavaReplies: 0Last Post: 10-31-2010, 02:19 AM -
Problem reading from a file
By sarapeace in forum New To JavaReplies: 13Last Post: 10-03-2010, 02:08 PM -
Reg: File Reading Problem
By balaji csc in forum New To JavaReplies: 0Last Post: 11-06-2009, 04:22 PM
Bookmarks