Hello i want to use strings out of a while loop for further operations.. but how do i "extract" them ot of the while loop?
I want to use sollWert1,sollWert2,sollWert3,sollWert4.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class ReadText {
public static void main(String[] args) {
// TODO Auto-generated method stub
BufferedReader br = null;
String file_at_path_to_desktop = System.getProperty("user.home") + "\\" + "Desktop" + "\\" + "SollAuftrag.txt";
try {
br = new BufferedReader(new FileReader(new File(file_at_path_to_desktop)));
String line = null;
while ((line = br.readLine()) != null) {
String[] parts = line.split(";");
String sollWert1= parts[0];
String sollWert2= parts[1];
String sollWert3= parts[2];
String sollWert4= parts[3];
System.out.println("Wert1: " + sollWert1);
System.out.println("Wert2: " + sollWert2);
System.out.println("Wert3: " + sollWert3);
System.out.println("Wert4: " + sollWert4);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
sollWert1etc values from each line, even when there's only one line, then you could leave the comparison inside the loop over lines. If you know there's exactly one line, you don't need to loop over lines at all.