I need to retrieve two lines from 1 .txt files and output them to a dialog box. My code as of now is
private String getfirstItem() {
String info = "";
File details = new File(myFile);
if(!details.exists()){
try {
details.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
BufferedReader read = null;
try {
read = new BufferedReader (new FileReader(myFile));
} catch (FileNotFoundException e3) {
e3.printStackTrace();
}
for (int i = baseStartLine; i < baseStartLine + 1; i++) {
try {
info = read.readLine();
} catch (IOException e) {
e.printStackTrace();
}
}
firstItem = info;
try {
read.close();
} catch (IOException e3) {
e3.printStackTrace();
}
return firstItem;
}
private String getsecondItem() {
File details = new File(myFile);
String info = "";
BufferedReader reader = null;
if(!details.exists()){
try {
details.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}}
try {
reader = new BufferedReader (new FileReader(myFile));
} catch (FileNotFoundException e3) {
e3.printStackTrace();
}
for (int i = modelStartLine; i < modelStartLine + 1; i++) {
try {
info= reader.readLine();
} catch (IOException e) {
e.printStackTrace();}
modelName = info;} try {
reader.close();
} catch (IOException e3) {
e3.printStackTrace();
}
return secondItem;
}
However, i keep getting the same value for both? modelStartLine=1 and baseStartLine =2
You’re never actually skipping any lines. You start your loop index from a different number, but you still loop only once, from the beginning of the file. Your loop should look something like this:
Now you can just call the function like this:
However. Since you just want the first two lines of the file, you can just read them both initially: