-1

I am making a Repository Client for my sites repositories and I need to be able to detect when a user enters a certain id to detect which URL Repository to get it from.

Here is my code:

    JTextField repo = new JTextField();
    JButton submit = new JButton("Download!");
    String repod = repo.getText();
    final String repoid = repod;
    submit.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            if(repoid.equals("pb")){
                //this
            }else if(repoid.equals("mb")){
                //this
            }else if(repoid.equals("repo29-update")){
                //this
            }else{
                System.out.println("Contacting SERVER...");
                System.out.println("Finding REPOID: "+repoid);
                System.out.println("Not Found! Returning Error!");
                JLabel err = new JLabel("Invalid REPOID");
                JFrame a = new JFrame("Error");
                a.setSize(300,100);
                a.setLocationRelativeTo(null);
                a.setResizable(false);
                a.setAlwaysOnTop(true);
                a.add(err);
                a.setVisible(true);
            }
        }
    });

When I go through this script, I always get the else option, why?

This is not a duplicate, I changed the == to .equals() and it still doesnt work.

KMCD00
  • 3
  • 2
  • 1
    You create a `JTextField` and about a nanosecond later you get the contents ... so, `repod` and thus `repoid` are empty strings. – Brian Roach Jan 12 '14 at 20:42
  • `String repod = repo.getText();` <-- needs to be in your `actionPerformed`. And just check against `repod`, no need for `repiod` – Paul Samsotha Jan 12 '14 at 20:53

1 Answers1

1

You should use .equals() when comparing strings.

if(repoid.equals("pb")){...
Tobias Golbs
  • 4,586
  • 3
  • 28
  • 49
Sionnach733
  • 4,686
  • 4
  • 36
  • 51