Hi,
I am trying to read a lot of information from a File, using a Scanner. And I have problems reading a Date, how can this be done?
I need it as a Date to pass it to a constructor and use it later.
Scannerread Date from file with Scanner
Page 1 of 1
9 Replies - 1434 Views - Last Post: 24 November 2010 - 02:55 PM
Replies To: Scanner
#2
Re: Scanner
Posted 24 November 2010 - 03:18 AM
Bocard, on 24 November 2010 - 10:01 AM, said:
I am trying to read a lot of information from a File, using a Scanner.
Better use a BufferedReader:
BufferedReader in = new BufferedReader(new FileReader("file.txt"));
String all = "";
String row = "";
while ( (row = in.readLine()) != null){
all += row;
}
// Do Something with the Data...
Then you can Split your String by unsing the split() or the indexOf() and substring()-Methods.
Greetings: Luke
#3
Re: Scanner
Posted 24 November 2010 - 06:00 AM
When building a String, better use a StringBuffer Object:
http://download.orac...ringBuffer.html
use append to add String Objects, and then the toString method to get a the String Object.
http://download.orac...ringBuffer.html
use append to add String Objects, and then the toString method to get a the String Object.
BufferedReader in = new BufferedReader(new FileReader("file.txt"));
String all = "";
StringBuffer sb = new StringBuffer();
while ( (row = in.readLine()) != null){
sb.append(row);
}
This post has been edited by japanir: 24 November 2010 - 06:01 AM
#4
Re: Scanner
Posted 24 November 2010 - 10:07 AM
So far, I have only learned how to use Scanner to read from files, I would like to know if it is possible for me to read a Date type from a file using a Scanner...and if it is possible, maybe you can give me an example too.
The project I am working on needs to get strings, ints, dates and other stuff from a file and then use them as constructors to make objects which will populate arraylists.
Thx in advance.
The project I am working on needs to get strings, ints, dates and other stuff from a file and then use them as constructors to make objects which will populate arraylists.
Thx in advance.
#5
Re: Scanner
Posted 24 November 2010 - 11:09 AM
Which format do the Date-Values have in your file? Maybe you can read them to a String and parse them: http://www.java2s.co...gdateString.htm...
Greetings: Luke
Greetings: Luke
#6
Re: Scanner
Posted 24 November 2010 - 11:17 AM
Take a look at the Scanner API:
http://download.orac...il/Scanner.html
there are some useful methods like:
nextInt(), to get the next int value in the input.
As MrLuke187 wisely said, when using the next<Data Type> methods of the Scanner, it is best if you know the format of the file, and the exact data type of each value.
If you are not sure, better read as String.
Also, your compiler has it's definition for a valid date format, valid boolean value, so make sure you know those formats as well
http://download.orac...il/Scanner.html
there are some useful methods like:
nextInt(), to get the next int value in the input.
As MrLuke187 wisely said, when using the next<Data Type> methods of the Scanner, it is best if you know the format of the file, and the exact data type of each value.
If you are not sure, better read as String.
Also, your compiler has it's definition for a valid date format, valid boolean value, so make sure you know those formats as well
#7
Re: Scanner
Posted 24 November 2010 - 11:18 AM
scanners can often be better than buffered readers due to simplicity.
Scanners are a lot easier to manipulate.
to begin, you need to declare a file object which will contain the path
to your file.
now, you can point a scanner to this file object like you would to any
other variable. simply put the "fileo" into the parentheses for the
Scanner:
when you want to read from the file, all you need to do is get the information
the same way you do from a variable or from user input:
Hope this helps,
~Wes
[edit] Sorry, I didn't see japanir's post
[/edit]
Scanners are a lot easier to manipulate.
to begin, you need to declare a file object which will contain the path
to your file.
File fileo = new File( "/this/is/where/your/path.goes" );
now, you can point a scanner to this file object like you would to any
other variable. simply put the "fileo" into the parentheses for the
Scanner:
Scanner fscan = new Scanner( fileo );
when you want to read from the file, all you need to do is get the information
the same way you do from a variable or from user input:
String mystring = fscan.nextLine(); // Puts the line of the index into mystring
// and sets the index to the next line in the file
mystring = fscan.next(); // like nextLine() but tops at any whitespace
int mynum = fscan.nextInt(); // reads in the next integer to mynum
Double mydoub = fscan.nextDouble(); // reads in the next decimal number to mydoub
fscan.nextLine(); // moves the index to the next line in the file
Hope this helps,
~Wes
[edit] Sorry, I didn't see japanir's post
This post has been edited by -shadow-: 24 November 2010 - 11:19 AM
#8
Re: Scanner
Posted 24 November 2010 - 11:28 AM
-shadow-, on 24 November 2010 - 06:18 PM, said:
scanners can often be better than buffered readers due to simplicity.
Scanners are a lot easier to manipulate.
Scanners are a lot easier to manipulate.
Not quite my opinion, too. I think, reading the File with a BufferedReader and than manipulate the lines with the String-Methods is more efficient than using a scanner. And they aren't that hard to use
You might also want to look at this: http://www.dreaminco...mpledateformat/
Greetings: Luke
This post has been edited by MrLuke187: 24 November 2010 - 02:31 PM
#9
Re: Scanner
Posted 24 November 2010 - 02:41 PM
thank you very much guys, i actually used the date format and i parsed the string. the file will be made by me, because i also have to implement an add method which will add stuff to the file.
i used it with:
and it works, but now i have another problem.
after reading the data, i put all the info (strings, ints, dates) and stuff into a constructor to make objects and add them into an arraylist. to test my code after that, i try to print the arraylist and at the date part, i get
thx in advance.
i used it with:
String d = input.next(); DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
Date date = df.parse(d);
and it works, but now i have another problem.
after reading the data, i put all the info (strings, ints, dates) and stuff into a constructor to make objects and add them into an arraylist. to test my code after that, i try to print the arraylist and at the date part, i get
Mon Apr 01 00:00:00 CEST 1991in the output. any idea how i can see only the date, month and year? i only need this data, not everything. the way i would like to see it would be "dd-mm-yyyy" too if possible, but any other options are cool too i guess.
thx in advance.
This post has been edited by Bocard: 24 November 2010 - 02:43 PM
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote








|