Hello. Help me please. You need to read from .txt file strings that look like this:
February 14
December 10th
June, 22
...
Day to read in the int variable, the name of the month in the String variable.
I can’t figure it out how to do it?
scanner sc = new scanner (new file ("D: \\ 1.Txt"));
While (SC.HASNEXT ()) {
int tempdy = sc.nextint ();
String Tempmonth = SC.NEXTLINE ();
TheTree.insert (Tempday, Tempmonth); // Inserting data into a binary tree
}
Answer 1, Authority 100%
scanner sc = new scanner (new file ("D: \\ 1.Txt"));
int tempdy;
String [] Splitted;
String Tempmonth;
While (SC.HASNEXT ()) {
splitted = sc.nextline (). Split (");
tempdy = integer.paraseint (splitted [0]);
tempmonth = splitted [1];
TheTree.insert (Tempday, Tempmonth); // Inserting data into a binary tree
}
UPD: Checked reading from the file – everything works:
import java.io.file;
Import java.io.fileNotFoundException;
Import java.util.scanner;
Public Class Test {
Public Static Void Main (String [] Args) Throws FileNotFoundException {
Scanner SC = NEW Scanner (New File ("file.txt"));
int tempdy;
String [] Splitted;
String Tempmonth;
While (SC.HASNEXT ()) {
splitted = sc.nextline (). Split (");
tempdy = integer.paraseint (splitted [0]);
tempmonth = splitted [1];
System.out.PrintLN (Tempday + "," + Tempmonth);
}
}
}
Answer 2
If you use Scanner for a large number, say 10 ^ 5 will already be performed more than 1 second. Therefore, for large data it is better to use StreamTokenizer. Here is an example of work.
import java.io. *;
Public Class example_read {
Public Static Void Main (String [] Args) Throws IoException {
StreamTokenizer Reader = New BufferedInPutStream (New FileInputStream (New File (Input.txt))));
While (reader.nextToken ()! = streamtokenizer.tt_eof) {
int day = (int) reader.nval;
reader.nextToken ();
String Month = Reader.Sval;
System.out.PrintLN (Day + "" + Month);
}
}
}