Home java java. Count lines from file

java. Count lines from file

Author

Date

Category

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
}

sample work

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);
    }
  }
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions