'how can i choose which lines to print in file in java?
i have this file name input.txt:
3 1
Hoa Lac, Thang Long, Le Quang Dao, Me Tri, Pham Hung, My Dinh, Cau Giay, Kim Ma, Hoan Kiem
Bus 74, Price 9000, Hoa Lac, Thang Long, Le Quang Dao, Me Tri, Pham Hung, My Dinh
Bus 99, Price 50000, Hoa Lac, My Dinh
Bus 34, Price 7000, My Dinh, Cau Giay, Kim Ma, Hoan Kiem
From: Hoa Lac, To: Hoan Kiem
i want to try to store each line as a String in array and i have write like this
public static void readFile(){
try {
String data[] = new String[100];
File myObj = new File("input.txt");
Scanner myReader = new Scanner(myObj);
int m;
int n;
m = myReader.nextInt();
n = myReader.nextInt();
System.out.println("m = " + m);
System.out.println("n = " + n);
while (myReader.hasNextLine()){
int i = 0;
data[i] = myReader.nextLine();
System.out.println(data[i]);
i++;
}
} catch (FileNotFoundException e) {
System.out.println("cannot find file!");
e.printStackTrace();
}
}
so far i can only read file and display all of them at once, but i can not control which line i want to print(ex:i want to print only line: 2 3 4), please help me and explain to me please.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
