-2

I'm scanning a text file in order to find two sequential words that are equal with my own. If I find them I'll display a window. Βut I can't make it work. I am pretty sure the problem is in this part of the code. I'm pretty novice in Java so any help would be much appreciated.

public void readFile(String a, String b){
    s=new Scanner("userDataStorage.txt");
    while (s.hasNext()){
        String u= s.next();
        String p=s.next();
        if(a==u&&b==p){
            l.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            l.setSize(400,300);
            l.setVisible(true);
        }
    }
}
giannis christofakis
  • 8,201
  • 4
  • 54
  • 65
  • 2
    You should use `(a.equals(u) && b.equals(p))` instead of `(a==u&&b==p)` – Lone nebula Jun 13 '13 at 17:31
  • 1
    What is 'l' in your code and which errors you are getting? – Darshan Mehta Jun 13 '13 at 17:32
  • Variables u and p should be declared outside the while loop to make the program more efficient. – Darshan Mehta Jun 13 '13 at 17:33
  • Post the error that you are getting. As Lone nebula pointed out, use .equals instead of == – Mubin Jun 13 '13 at 17:33
  • Well, the l is just a window from another class that I want to pop up if a and u is equal and b and p is equal. The problem is not that the program is too inefficient, but that it simply dos'nt work. When i run from my main I get the window I am looking for, but when I enter the two variables that i use for a and b, I get a bunch of error messages. These are the errors i get: – Sindre Brattegard Jun 13 '13 at 17:46
  • Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem: Unhandled exception type FileNotFoundException at FileMangment.readFile(FileMangment.java:32) at Gui$LoginHandler.actionPerformed(Gui.java:39) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) – Sindre Brattegard Jun 13 '13 at 17:47
  • And a lot mor, but I was'nt allowed to enter more – Sindre Brattegard Jun 13 '13 at 17:47

2 Answers2

4

If you want to read data from file then you probably should use

new Scanner(new File("userDataStorage.txt"));

otherwise scanner will just treat "userDataStorage.txt" as raw data to scan, not as path to file.

Also if you compare strings then use equals method instead of == operator

if (a.equals(u) && b.equals(p))
Community
  • 1
  • 1
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • 2
    The OP is also calling `s.next()` twice per iteration of the loop, which is likely to break when it gets to the end of the file. – Henry Keiter Jun 13 '13 at 17:34
  • @HenryKeiter I assume that OP stores in file Strings in pairs so if `hasNext` returns true then it also mean that there are two more elements. But if he doesn't then you are absolutely right. – Pshemo Jun 13 '13 at 17:39
0

If you need to compare Strings use

a.equals(p) && b.equals(p)

or

 a.equalsIgnoreCase(u) && b.equalsIgnoreCase(p)
giannis christofakis
  • 8,201
  • 4
  • 54
  • 65