-1

I have a javafx homework at school

enter image description here

I was able to save the data as requested

enter image description here

My question is how can I read the data from the Text File so that all the data is back in the right place

enter image description here

Integer cb_years_v;
            String cb_manuf_v,smb_type_v,tg_v,cb_extras_v,lv_size_v,tf_vend_user_v,tf_vend_name_v,pf_pass_v,ta_other_v,price_value_v;
            
            cb_manuf_v = cb_manuf.getValue(); //ComboBox
            smb_type_v=ch_type.getText(); //SplitMenuButton
            tg_v = tg.getSelectedToggle().toString(); //ToggleGroup
            cb_extras_v = cb_extras.getValue(); //ComboBox
            cb_years_v = cb_years.getValue(); //ComboBox
            lv_size_v = lv_size.getSelectionModel().getSelectedItem().toString(); //ListView
            tf_vend_name_v = tf_vend_name.getText(); //TextField
            tf_vend_user_v = tf_vend_user.getText(); //TextField
            pf_pass_v = pf_pass.getText(); //PasswordField
            ta_other_v = ta_other.getText(); //TextArea
            price_value_v = price_value.getText(); //Label
            
            FileChooser chooser = new FileChooser();
            chooser.setTitle("Choose location To Save Report");;
            File selectedFile = null;
            while(selectedFile== null){
                selectedFile = chooser.showSaveDialog(null);
            }
            File file2 = null;
            file2 = selectedFile;
            PrintWriter outFile = null;
            try {
                outFile = new PrintWriter(file2);
            } catch (FileNotFoundException e2) {
                e2.printStackTrace();
            }
            outFile.println("Manufacturer = " + cb_manuf_v);
            outFile.println("Type = "+smb_type_v);  
            outFile.println("Color = " +tg_v);    
            outFile.println("Extras = " + cb_extras_v);
            outFile.println("Year = " + cb_years_v);
            outFile.println("Size = " + lv_size_v);
            outFile.println("Vendor Name = " + tf_vend_name_v);
            outFile.println("Vendor User = " + tf_vend_user_v);
            outFile.println("Password = " + pf_pass_v);
            outFile.println("Other = " + ta_other_v);
            outFile.println("Price = " + price_value_v);
            
            outFile.close();

Thank you for your help!

p.tibi
  • 3
  • 2
  • 1
    java naming conventions, please – kleopatra Nov 02 '20 at 15:59
  • `tg_v = tg.getSelectedToggle().toString(); //ToggleGroup` appears to be leading to noise in the data. – SedJ601 Nov 02 '20 at 16:39
  • I would suggest you follow @kleopatra advice. Using `JavaFX` naming conventions is very important for certain `Nodes` to act properly. https://www.oracle.com/java/technologies/javase/codeconventions-namingconventions.html – SedJ601 Nov 03 '20 at 14:25

4 Answers4

1

Here is another way you could do it. Instead of using Scanner, you could use Files.readAllLines. From there, you can use the line index as the control in a Switch-Statement or if a line contains the info you need to extract using an If-Statement. This example does not show the Switch-Statement version. It only shows the If-Statement version. I did not test this code so there may be errors.

try {
    File selectedFile = new File("");
    List < String > lines = Files.readAllLines(selectedFile.toPath());
    for (String line: lines) {
        if (line.contains("Manufacturer")) {
            cb_manuf.setValue(line.split("=")[1].strip());
        } else if (line.contains("Type")) {
            smb_type.setText(line.split("=")[1].strip());
        } else if (line.contains("Color")) {
            ...
        } else if (line.contains("Extras")) {
            ...
        } else if (line.contains("Year")) {
            ...
        } else if (line.contains("Size")) {
            ...
        } else if (line.contains("Vendor Name")) {
            ...
        } else if (line.contains("Vendor User")) {
            ...
        } else if (line.contains("Password")) {
            ...
        } else if (line.contains("Other")) {

        } else if (line.contains("Price")) {
            ...
        }
    }
} catch (IOException ex) {
    ex.printStackTrace();
}
SedJ601
  • 12,173
  • 3
  • 41
  • 59
0

You can use the Java Scanner class to read the file

 Scanner sc = new Scanner(new File(yourfilepath));
    while(sc.hasNextLine){
String line = sc.nextLine() //scanner can read line by line, you can concat all linesin a string to get text in the file
     }

here are more options to read txt files:

Reading a plain text file in Java

Sir Lopez
  • 55
  • 7
0

You can use Sir Lopez answer to get the string of every line and for each string you have to :

  • Detect which javaFX object is concerned, the best is to actually cut the string where there is an equal sign e.g from the line: "Color = blue" you get two strings "Color" and "blue". I won't do your homework for you but you can look at the split method.
  • Fill the javaFX object with the second value: tf_vend_name.setText(ValueFromFile);
Dharman
  • 30,962
  • 25
  • 85
  • 135
Mohameth
  • 376
  • 2
  • 10
0

My solution, I hope it will help someone else.

Thanks for the help!

 load.addEventHandler(ActionEvent.ACTION, (e) -> {

        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("Open Resource File");
        fileChooser.getExtensionFilters().addAll(new ExtensionFilter("Text Files", "*.txt"));
        File selectedFile = fileChooser.showOpenDialog(primaryStage);
        Scanner scan = null;

        try {
            scan = new Scanner(selectedFile);

            scan.next();
            String line = scan.nextLine();
            String[] lineArray = line.split(" = ");
            cb_manuf.setValue(lineArray[1]);

            scan.next();
            String line2 = scan.nextLine();
            String[] lineArray2 = line2.split(" = ");
            smb_type.setText(lineArray2[1]);

            scan.next();
            String line3 = scan.nextLine();
            String[] lineArray3 = line3.split(" = ");
            // tg.selectedToggleProperty(lineArray3[1]);

            scan.next();
            String line4 = scan.nextLine();
            String[] lineArray4 = line4.split(" = ");
            cb_extras.setValue(lineArray4[1]);

            scan.next();
            String line5 = scan.nextLine();
            String[] lineArray5 = line5.split(" = ");
            cb_years.setValue(Integer.parseInt(lineArray5[1]));

            scan.next();
            String line6 = scan.nextLine();
            String[] lineArray6 = line6.split(" = ");
            // lineArray6[1] = lv_size.getSelectionModel().getSelectedItem();

            scan.next();
            String line7 = scan.nextLine();
            String[] lineArray7 = line7.split(" = ");
            tf_vend_name.setText(lineArray7[1]);

            scan.next();
            String line8 = scan.nextLine();
            String[] lineArray8 = line8.split(" = ");
            tf_vend_user.setText(lineArray8[1]);

            scan.next();
            String line9 = scan.nextLine();
            String[] lineArray9 = line9.split(" = ");
            pf_pass.setText(lineArray9[1]);

            scan.next();
            String line10 = scan.nextLine();
            String[] lineArray10 = line10.split(" = ");
            ta_other.setText(lineArray10[1]);

            scan.next();
            String line11 = scan.nextLine();
            String[] lineArray11 = line11.split(" = ");
            price_value.setText(lineArray11[1]);

        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        } finally {
            scan.close();
        }
    }

    );
p.tibi
  • 3
  • 2