Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6771413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:25:45+00:00 2026-05-26T15:25:45+00:00

I have a problem and a question with using Strings. The problem I am

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:25:46+00:00Added an answer on May 26, 2026 at 3:25 pm

    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"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have basically the same problem outlined in this question, however I am using
Following on from my previous question [link text][1] , I have a new problem.
Situation: Hello! I have a little problem in a C# project. I am using
I have the same question problem as described here How to purge a cached
(I have a problem that I illustrated in this question but had no correct
Following on from a question I posted yesterday about GUIs, I have another problem
I seem to have the exact opposite problem than this question on stopping dock
The responses I've got to this question have solved the problem I had in
This is more of a theoretical question than an actual problem I have. If
This is a code review question more then anything. I have the following problem:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.