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){
^

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.