user1015523
user1015523

Reputation: 332

String questions

I have a problem and a question with using Strings.

The problem I am having is reading a line in a text file. Let's say in my .txt file I have the word 'hello' and I am simply trying to validate that the user inputs the same thing.

The .txt file simply has the word 'hello'

Here is the code.

String fileWord;

File filename = fileOpen.getSelectedFile(); // Open the .txt file
FileReader filereader = new FileReader(filename);
BufferedReader inputFile = new BufferedReader(filereader);

String input = JOptionPane.showInputDialog("Say hello");
fileWord = inputFile.readLine();

if (input == fileWord)
     System.out.println("They match!");

However, this doesn't work. I did a System.out.println on both input and fileWord and they appear to be the same, but they're not as the if() statement doesn't work.

Another question I have is if in a .txt file I have on one line 'hello goodbye' how do I read one word on a line at a time? For example, if the user inputs 'hello' then it finds it in the text file and returns 'goodbye'.

EDIT I've gotten further in my program and have run into another issue. Whenever a user inputs a word, if it doesn't find the word in the file, it is suppose to write the word to a new file. In my program, I'm using a loop to search through the file to find the word. When I input a word that doesn't exist in the file, I get an exception error and the program closes. I can't quite figure out where and how I figure out that the word isn't in the file.

while (!input.equals(fileWord))
{
     fileWord = inputFile.readLine();
     String[] wordSplit = englishWord.split("\\s+", 2);
     fileWord = wordSplit[0];
     fileWord2 = wordSplit[1];
}

System.out.println("The opposite of " + fileWord + " is " + fileWord2);

This example would find "hello" in the text file and output "goodbye" and that works fine. But, if I put some random word as the input and it won't find it, what do I do to move on and proceed with adding it into a new file? I can figure out how to write to a file, I think...I just can't get out of the loop correctly without an exception.

Upvotes: 0

Views: 179

Answers (4)

danpaq
danpaq

Reputation: 324

Do not use == with Strings. Use String.equals.

== compares the id of the objects; not the contents.

Upvotes: 0

Dave Newton
Dave Newton

Reputation: 160181

Strings are compared with the .equals() method:

input.equals(fileWord)

For lines with multiple words, use the split() method to break them apart into an array, then compare input against each array element. (And break out of the loop if there's a match).

Upvotes: 0

esaj
esaj

Reputation: 16035

You need to use equals(), not == to compare two String for equality. Using == with objects returns true only if both sides refer to the exact same object (Two Strings with same contents are still two different String-objects).

Upvotes: 1

NPE
NPE

Reputation: 500247

Use equals() to compare strings and not ==:

if (input.equals(fileWord))

As to your second question, the easiest way is to read the entire line, then split() it. That'll give you a two-element array of Strings. You can use the first element for comparison and return the second element if there's a match:

String[] words = line.split("\\s+", 2);
// words[0] will contain "hello"
// words[1] will contain "goodbye"

Upvotes: 3

Related Questions