mercredi 29 avril 2020

Strings from the same source, imported in the same way, not registering as the same string? [duplicate]

So I am making a "restaurant management tool" and one of the classes' function is to manually enter the amount of an ingredient you have. It imports a list of stored ingredients from a .txt file and puts them in a JComboBox. When the button is pressed, it goes through the same file again, finds the selected option and replaces its quantity with the one that is written in the JTextField by the user.

For some reason, it is not entering the if statement. I have no idea why the two Strings would be different, as they come from the same source and they are imported in the same way (except for one of them going through a JComboBox first).

Here's my code (I put an arrow next to the part in question, I just thought the problem might be coming from somewhere else):

import java.io.*;
import java.nio.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Toolkit;

public class EditStock implements ActionListener{
    private static JLabel header;
    private static JFrame f;
    private static JComboBox optList;
    public static boolean authorized;
    public static JButton subB;
    private static JTextField amT;
    private static Scanner sc3;
    private static String token1;
    private static int n;
    private static Scanner sc4;
     public static void EditStockfunc() throws IOException{
        File stock = new File("C:\\Users\\liker\\OneDrive\\New\\new new\\Computer Science\\IA\\stock.txt");
        Scanner sc = new Scanner (stock);
        Scanner sc2 = new Scanner (stock);
        sc3 = new Scanner(stock);
        sc4 = new Scanner(stock);
        token1 = "";

        JPanel p = new JPanel();
        f = new JFrame("RMT 1.0");
        f.setSize(250,200);
        f.setLocation(1080,530);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
        f.add(p);

        header = new JLabel("Which quantity would you like to change?");
        header.setBounds(10,20,80,25);
        p.add(header);



        int x = 0; 
        while(sc.hasNext()){
            x+=1;
            token1 = sc.nextLine();
        }               
        String[] temp = {""};
        String[] options = new String[x];

        x = 0;
        while(sc2.hasNext()){         
           token1 = sc2.nextLine();                   
           temp = token1.split(",");
           System.out.println(temp[0]);
           options[x] = temp[0]; 
           x+=1;
         }     

        optList = new JComboBox(options);
        optList.setBounds(75,35,100,25);
        optList.setSelectedIndex(0);
        p.add(optList);                      

        JLabel tLabel = new JLabel("Correct amount:");
        tLabel.setBounds(10,20,80,25);
        p.add(tLabel);
        amT = new JTextField(20);
        amT.setBounds(100,20,165,25);
        p.add(amT);
        subB = new JButton("Submit");
        subB.setBounds(150,150,80,25);
        subB.addActionListener(new EditStock());
        p.add(subB);
     }
    @Override
    public void actionPerformed(ActionEvent e){
       File stock = new File("C:\\Users\\liker\\OneDrive\\New\\new new\\Computer Science\\IA\\stock.txt");
       int a = Integer.parseInt(amT.getText());
       String o = (String)optList.getSelectedItem();               
       int x = 0; 
       while(sc4.hasNext()){
            x+=1;
            token1 = sc4.nextLine();
        }               
       String[] temp = {""};
       String[] options = new String[x];
       x = 0;
       while(sc3.hasNext()){                   
           token1 = sc3.nextLine();                   
           temp = token1.split(",");           
           System.out.println(temp[1]);
           if (o == temp[0]){ <---------------------- This part
               temp[1] = Integer.toString(a);
           }           
           System.out.println(temp[1]+o+temp[0]+a);
           options[x] = temp[1]; 
           x+=1;

        }

    }
}

And here's my .txt file:

Tomato,15
Potato,23
Spagetti,8
Beef,10

Aucun commentaire:

Enregistrer un commentaire