I am trying to create a rudimentary number guessing program with java, and it is not working out. I think i have some useless stuff in there, but whatever:

import java.util.Random;
import java.io.* ;

public final class Number_guesser {

  public static final void main(String... aArgs){
                InputStreamReader istream = new InputStreamReader(System.in) ;
          BufferedReader bufRead = new BufferedReader(istream) ;

    System.out.println("Welcome to the 'pick a number game'.");
    System.out.println("Press the 'Enter' or 'Return' key to continue after everything I say...");
      Getch();
    System.out.println("Think of a number from one to four...");
      Getch();
    System.out.println("Did you think of it?");
      Getch();
    System.out.println("Ok, good...");

    int START = 1;
    int END = 4;
    Random random = new Random();
    for (int idx = 1; idx <= 1; ++idx){
      showRandomInteger(START, END, random);
    }


  }

  private static void showRandomInteger(int aStart, int aEnd, Random aRandom){
    if ( aStart > aEnd ) {
      throw new IllegalArgumentException("Start cannot exceed End.");
    }

    long range = (long)aEnd - (long)aStart + 1;

    long fraction = (long)(range * aRandom.nextDouble());
    int randomNumber =  (int)(fraction + aStart);    
    log("Was your number " + randomNumber + "?");
  }

  private static void log(String aMessage){
    System.out.println(aMessage);
  }
  boolean go = true;

  While (go){
    try {
        System.out.println("Was I correct:");
        String word = bufRead.readLine();

       int word = Integer.parseInt(word);
    }
    catch (IOException err) {
               System.out.println("Error reading line");
          }
          catch(NumberFormatException err) {
               System.out.println("Error Converting Number");
          }


    if (word = "no"){
        System.out.println("Oh well, better luck next time");
        Getch();
    }


    if (word = "yes"){
        System.out.println("I knew it!");
    }

    else{
            System.out.println("Please say 'yes' or 'no' in lowercase letters");


    }

    System.out.println("would you like to play again?");
    char[] userInput = new char[1];
int a = System.in.read(userInput);

if( userInput[0] != "yes" ){
go = false; 
}

}

}

I got the following errors and I don't know what the H∑|| they mean:

Number_guesser.java:46: invalid method declaration; return type required
While (go){
^
Number_guesser.java:46: <identifier> expected
While (go){
^

Dani AI

Generated

Several root causes are visible in the thread: top-level statements outside any method, wrong keyword case, bad string/assignment usage, and mixed variable declarations. As pointed out, Java is case-sensitive (use while, not While). As suggested, the interactive loop and I/O must live inside a method (for example, inside main). provided the correct main signature — that is the method that should contain the game loop.

A compact, different-but-working example that avoids the original errors:

import java.util.Random;
import java.util.Scanner;

public class NumberGuesser {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Random rnd = new Random();
        boolean playing = true;

        while (playing) {
            int guess = rnd.nextInt(4) + 1; // 1..4
            System.out.println("Was your number " + guess + "? (yes/no)");
            String reply = scanner.nextLine().trim();

            if (reply.equalsIgnoreCase("yes")) {
                System.out.println("I knew it!");
            } else if (reply.equalsIgnoreCase("no")) {
                System.out.println("Oh well, better luck next time");
            } else {
                System.out.println("Please answer 'yes' or 'no'.");
                continue;
            }

            System.out.println("Play again? (yes/no)");
            playing = scanner.nextLine().trim().equalsIgnoreCase("yes");
        }
        scanner.close();
    }
}

Key troubleshooting notes tied to the original errors:

  • "illegal start of type" / "identifier expected": caused by placing while (go) { ... } at class scope. Move the loop inside main (or another method).
  • While vs while: Java keywords are lowercase.
  • if (word = "no"): = is assignment (and wrong type for condition). Use equals / equalsIgnoreCase to compare strings.
  • Redeclaring word as both String and int is invalid; parse into a differently named int only when numeric input is expected.
  • Getch() is not a Java standard function — use scanner.nextLine() to wait for Enter.
  • The compile/run error about class names usually means the compiler was invoked incorrectly. Ensure the filename matches the public class (case-sensitive) and compile/run like:
    • javac NumberGuesser.java
    • java NumberGuesser

The above addresses the specific compile messages and common logic mistakes from the posts, and provides a simple, safe pattern for handling console input and looping logic.

Recommended Answers

All 12 Replies

Java is case sensitive. The while keyword starts with w

Also, shouldn't lines 44 to 85 be included inside a function?

How would I do that?

include those lines inside a function... still learning and i decided to write all of this as soon as i learned it all...

and the main method looks like this

public static void main(String[] args) {

}

ok, I did what you told me to do NormR1, and it gave me this:

Number_guesser.java:47: illegal start of type
while (go){
^
Number_guesser.java:47: <identifier> expected
while (go){

Did you make the other recommended changes? The code after line 43 needs to be in a method. Please post the current code.

I just changed my main method, but it still gave me the same errors as this:

Number_guesser.java:47: illegal start of type
while (go){
^
Number_guesser.java:47: <identifier> expected
while (go){

this is the new code:

import java.util.Random;
import java.io.* ;

public final class Number_guesser {

public static void main(String[] args){
                InputStreamReader istream = new InputStreamReader(System.in) ;
          BufferedReader bufRead = new BufferedReader(istream) ;

    System.out.println("Welcome to the 'pick a number game'.");
    System.out.println("Press the 'Enter' or 'Return' key to continue after everything I say...");
      Getch();
    System.out.println("Think of a number from one to four...");
      Getch();
    System.out.println("Did you think of it?");
      Getch();
    System.out.println("Ok, good...");

    int START = 1;
    int END = 4;
    Random random = new Random();
    for (int idx = 1; idx <= 1; ++idx){
      showRandomInteger(START, END, random);
    }


  }

  private static void showRandomInteger(int aStart, int aEnd, Random aRandom){
    if ( aStart > aEnd ) {
      throw new IllegalArgumentException("Start cannot exceed End.");
    }

    long range = (long)aEnd - (long)aStart + 1;

    long fraction = (long)(range * aRandom.nextDouble());
    int randomNumber =  (int)(fraction + aStart);    
    log("Was your number " + randomNumber + "?");
  }

  private static void log(String aMessage){
    System.out.println(aMessage);
  }

  boolean go = true;

  while (go){
    try {
        System.out.println("Was I correct:");
        String word = bufRead.readLine();

       int word = Integer.parseInt(word);
    }
    catch (IOException err) {
               System.out.println("Error reading line");
          }
          catch(NumberFormatException err) {
               System.out.println("Error Converting Number");
          }


    if (word = "no"){
        System.out.println("Oh well, better luck next time");
        Getch();
    }


    if (word = "yes"){
        System.out.println("I knew it!");
    }

    else{
            System.out.println("Please say 'yes' or 'no' in lowercase letters");


    }

    System.out.println("would you like to play again?");
    char[] userInput = new char[1];
int a = System.in.read(userInput);

if( userInput[0] != "yes" ){
go = false; 
}

}

}

This is the code for lines 41- 43

  private static void log(String aMessage){
    System.out.println(aMessage);
  }

I am pretty sure the rest is still in the main method

no you were right, sorry. It is still giving me this though:

error: Class names, 'Number_guesser', are only accepted if annotation processing is explicitly requested
1 error

The code after line 44 must be inside of a method,

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.