Hi, i have a question regarding reading integer from the file where there are also characters.
The sample file might look like this:
height = 234,
goals = 23,
defeats = 4,
numbers = 22,
So im using nested Scanners. One to read the line and second one to read that particular line BUT how can i get the numbers out. The Scanner class methods hasNextInt() does not seem to work. Any feedback welcomed.
Scanner class to read the file
Page 1 of 17 Replies - 4111 Views - Last Post: 27 November 2010 - 10:42 PM
Replies To: Scanner class to read the file
#2
Re: Scanner class to read the file
Posted 27 November 2010 - 02:47 PM
what do you mean by nested Scanners?
can you post your code? might be easier to debug
can you post your code? might be easier to debug
#3
Re: Scanner class to read the file
Posted 27 November 2010 - 02:52 PM
You could read that into a Properties instance, although you'd need to swallow the commas
#4
Re: Scanner class to read the file
Posted 27 November 2010 - 03:00 PM
I think the easiest way of doing this would be to use regex. Well this would be the approach I would take and maybe some others on here. Below is a simple template to get the numbers for you - it is incomplete. You certainly wont need two scanners, valuable memory will be wasted plus it's not really good for two Scanners to access the same file at the same time.
To get the other numbers, you will have to include a while loop which I will leave for you to do; use the concept I am using with replaceAll() to get the 'height', 'goals' and etc.. from the text file. Hint - have a look at trying to replace each character which is not a word character with blank. This may be of use - Java Regex Cheat Sheet, which I advise to bookmark
.
To get the other numbers, you will have to include a while loop which I will leave for you to do; use the concept I am using with replaceAll() to get the 'height', 'goals' and etc.. from the text file. Hint - have a look at trying to replace each character which is not a word character with blank. This may be of use - Java Regex Cheat Sheet, which I advise to bookmark
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class template {
public static void main (String [] args){
//Initialise variables
Scanner s = null;
String line = "";
int num = 0;
//Read file with Scanner
try {
s = new Scanner(new File("sample"));
//If file not found, throw exception.
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// Now read each line if its available.
if(s.hasNextLine()){
line = s.nextLine();
}
/*Replace each non digit with "" and then typecast
to an integer if we can.*/
try {
num=Integer.parseInt(line.replaceAll("[^0-9]", ""));
/*If we can't, throw an exception. */
} catch (NumberFormatException e){
System.err.println("Cannot cast");
}
//Simply print.
System.out.println(num);
}
}
#5
Re: Scanner class to read the file
Posted 27 November 2010 - 03:03 PM
Sorry.. thats my code.
File file = new File("a.txt");
Scanner fileScanner = new Scanner(file);
while(fileScanner.hasNextLine()){
String line = fileScanner.nextLine();
Scanner s = new Scanner(line);
while(s.hasNextInt()){
System.out.print(s.nextInt());
}
}
}
#6
Re: Scanner class to read the file
Posted 27 November 2010 - 03:04 PM
@japanir,
I believe he means two instances of the Scanner class which I see quite a few people do which really is a bad idea.
I believe he means two instances of the Scanner class which I see quite a few people do which really is a bad idea.
//Initialise variables
Scanner s = null;
Scanner s1 = null;
//Read file with Scanner
try {
s = new Scanner(new File("sample"));
s1 = new Scanner(new File("sample"));
//...
#7
Re: Scanner class to read the file
Posted 27 November 2010 - 04:20 PM
Or instead of Properties, use ResourceBundle. Save the data as 'x.properties' and try the following:
import java.util.ResourceBundle;
public class CommaProps {
private ResourceBundle bundle;
public CommaProps() {
bundle = ResourceBundle.getBundle("x");
}
public static void main(String[] args) {
CommaProps cp = new CommaProps();
System.out.println(cp.getProperty("goals"));
}
public String getProperty(String key) {
String result = bundle.getString(key);
return (result == null) ? null : result.substring(0, result.length() -
1);
}
}
This post has been edited by g00se: 27 November 2010 - 04:21 PM
#8
Re: Scanner class to read the file
Posted 27 November 2010 - 10:42 PM
japanir, on 27 November 2010 - 03:47 PM, said:
what do you mean by nested Scanners?
can you post your code? might be easier to debug
can you post your code? might be easier to debug
One Scanner to read lines from the file as String
One Scanner to parse the read lines
if all the lines are in the format
xxx = 123,
The easiest way is to read line by line using a Scanner
then use the String class split() method on " ,"
and then Integer.parseInt() on split[2]
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|