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
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..:=(:
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).
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:)-:
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.
Re: Reading file problem using Scanner
i already put employee.txt at top of the page..
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.
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?
Re: Reading file problem using Scanner
i don't know what is java.util.InputMismatchException..
can u solve the question..
i still got problem
Re: Reading file problem using Scanner
Quote:
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.
Quote:
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.
Re: Reading file problem using Scanner
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
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 + "<");
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
Re: Reading file problem using Scanner
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..
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
Re: Reading file problem using Scanner
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?
Re: Reading file problem using Scanner
Quote:
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() ??
Re: Reading file problem using Scanner
Re: Reading file problem using Scanner
thanks a lot Norm:(happy):