Scanner

read Date from file with Scanner

Page 1 of 1

9 Replies - 1797 Views - Last Post: 24 November 2010 - 02:55 PM Rate Topic: -----

#1 Bocard  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 222
  • Joined: 24-September 08

Scanner

Posted 24 November 2010 - 03:00 AM

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.
Is This A Good Question/Topic? 0
  • +

Replies To: Scanner

#2 MrLuke187  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 81
  • View blog
  • Posts: 237
  • Joined: 09-July 10

Re: Scanner

Posted 24 November 2010 - 03:18 AM

View PostBocard, 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
Was This Post Helpful? 0
  • +
  • -

#3 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

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.
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

Was This Post Helpful? 1
  • +
  • -

#4 Bocard  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 222
  • Joined: 24-September 08

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.
Was This Post Helpful? 0
  • +
  • -

#5 MrLuke187  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 81
  • View blog
  • Posts: 237
  • Joined: 09-July 10

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
Was This Post Helpful? 2
  • +
  • -

#6 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

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 :)
Was This Post Helpful? 0
  • +
  • -

#7 -shadow-  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 204
  • Joined: 18-November 10

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.
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 :([/edit]

This post has been edited by -shadow-: 24 November 2010 - 11:19 AM

Was This Post Helpful? 0
  • +
  • -

#8 MrLuke187  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 81
  • View blog
  • Posts: 237
  • Joined: 09-July 10

Re: Scanner

Posted 24 November 2010 - 11:28 AM

View Post-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.

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

Was This Post Helpful? 0
  • +
  • -

#9 Bocard  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 222
  • Joined: 24-September 08

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:
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 1991
in 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

Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10185
  • View blog
  • Posts: 37,603
  • Joined: 27-December 08

Re: Scanner

Posted 24 November 2010 - 02:55 PM

See the DateFormat.format(Date) method.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1