0

I created a JAVA code, and I don't have any errors, but when I run the code, the output does this: Enter a word: Thank you for entering a word! And it does not let me enter anything, when I intend for the code to let me enter a word, then it checks if it is a word, and gives the answer if it is a word, or none if it isn't. (It is my first time asking on this site) Here's the code:

package files;
import java.util.Scanner;
public class Testprinter {

static boolean myBoolean = false;   
static Scanner userInput = new Scanner(System.in);


public static void main(String[] args){
    String usersInput;

    while(myBoolean != true)
    {   
    System.out.print("Enter a word: ");

    usersInput = userInput.toString();
    myBoolean = checkInput(usersInput);
    }

    checkifComplete();

}

public static boolean checkInput(String usersInput){
    if(usersInput == (String)usersInput)
    {
        return true;

    } else { return false; }

}
public static void checkifComplete(){
    if(myBoolean = true){
        System.out.print("Thank you for entering a word!");

    }

}


}
jiggumbob
  • 141
  • 1
  • 2
  • 11

3 Answers3

2

This line is wrong:

if (usersInput == (String)usersInput)

It should be:

if (usersInput.equals(usersInput))

In Java, strings (and in general: all objects, that is all types that are non-primitive) must me compared using the equals() method, which tests for equality. The == operator is fine for testing equality between primitive types, but for objects it tests for identity - a different concept, and 99% of the time, not what you want.

And besides, you're comparing a string with itself! it'll always return true, I'm quite sure that's not what you want to do… notice that the parameter must have a different name, currently it's called just like the attribute. Perhaps this is what you meant?

public static boolean checkInput(String input) {
    return usersInput.equals(input);
}
Óscar López
  • 215,818
  • 33
  • 288
  • 367
  • You can use `==` to test - it just checks something other than what you might expect, right? That the objects are the same, rather than their contents are equal – chrisb2244 Sep 08 '14 at 01:07
  • Downvoter here. This doesn't fix the code; the issue is that he is reading the toString result. It is part of it, though. – Pokechu22 Sep 08 '14 at 01:12
  • 2
    I'm not downvoter, but OP has different problem, using scanner – user1143343 Sep 08 '14 at 01:12
  • Ok, agreed. But given that the question is titled "How to fix a word requesting program?", my answer solves at least part of the problems – Óscar López Sep 08 '14 at 01:22
2

You forgot scanner.nextLine(); thats reason its not asking you enter anything.
Instead of
usersInput = userInput.toString();
Use:

  String  usersInputStr = scanner.nextLine();

Follow this link - for how to use scanner: How can I read input from the console using the Scanner class in Java?

Community
  • 1
  • 1
Õzbek
  • 14,370
  • 10
  • 49
  • 84
0

Your issue is using userinput.toString(), when you should be using usersInput = userInput.next();. You are currently retrieving the string representation of the scanner, not getting a word.

Corrected main:

public static void main(String[] args){
    String usersInput;

    while(myBoolean != true)
    {   
    System.out.print("Enter a word: ");

    usersInput = userInput.next();
    myBoolean = checkInput(usersInput);
    }

    checkifComplete();

}
Pokechu22
  • 4,790
  • 8
  • 35
  • 59