0

I am trying to get some user input in a function but for some reason my code will not recognize my input streams. When I go to compile I get an error on line 81 saying reader.readLine() can not be found. Does anyone know how to fix this? Or is it possible to have the run function happen in the initial do-while loop without any issues?s

import java.io.*;

public class JavaLab3 {


public static void main(String[] args) {
  // first we define our input streams.
  InputStreamReader input = new InputStreamReader(System.in);
  BufferedReader reader = new BufferedReader(input);

  // variable declarations
  String   sName, playAgain;

  // we catch exceptions if some are thrown.
  // an exception would be entering a string when a number is expected
  try {
        System.out.println("what is your name?");

        // we read a string from the stream
        sName = reader.readLine();
        do {
          run();
          System.out.println(sName + "would you like to play again?");
          System.out.println("Please answer in lowercase 'yes' or 'no'.");
          playAgain = reader.readLine();
        } while (playAgain != "no");

  } catch (IOException e){
        System.out.println("Error reading from user");
  }

}

public static int maxRun (int runTotal) {
int highScore = 0;
if (runTotal > highScore) {
  highScore = runTotal;
} else {
  `highScore = highScore`}
 return highScore;
}


public static int run () {

Integer currentRun = 0, uNumber, counter;
final Integer MAX = 4;
final Integer MAX_NUMBER = 100;

//While current total is less than the max
while (currentRun < MAX) {
  System.out.println("Please enter a number.");
  //store user number
  uNumber = Integer.parseInt(reader.readLine());  //Line throwing the error.
  //for each number under 5 repetitions
  for (counter = 0; counter <= MAX_NUMBER ; counter++ ) {
    if (uNumber < 0) {
      System.out.println("Please enter a positive number.");
    } else if ((uNumber % 2) != 0) {
      System.out.println("Please enter an even number.");
    } else {
      //sets current total and restarts the loop
      currentRun = uNumber + currentRun;
    }
  }
}
//Calls maxRun function to see if score the score is the highest.
maxRun(currentRun);
System.out.println("The max for this run was, " + currentRun + ".");
return currentRun;
}

}

3 Answers3

1

reader is defined within the scope of the main() method. It doesn't exist in the scope of run().

You can define it as a member of the class, so both methods will have access to it. Or pass it as a parameter between the methods.

Malt
  • 28,965
  • 9
  • 65
  • 105
0

reader is defined inside the main method and its scope is inside that, to access that in run method, you can pass that in run() method so that it can be available there.

Viki Jain
  • 119
  • 1
  • 7
-1

The BufferedInput reader definition should be declared outside of the main function i.e inside the class then It will be global and accessible by any method of the class.

Class name {
       buffereInput reader = ....
 .....
 }