-1

I am trying to do a program that uses call methods. However, when this is compiled the program tries to read dataIn as a symbol and gives me the error: Cannot find variable I cannot seem to find the problem to why this is happening. Any help?

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

public class Adventure1 {
  public static void main(String[] args) throws IOException {
    String strInput = "Y";
    while (strInput == "Y") {
      int caveNo;
      doIntro();
      caveNo = getChoice();
      doTest(caveNo);
      System.out.println("Do you want to play again? (Y or N)");
      strInput = dataIn.readLine();
    }
  }

  //Start game
  public static void doIntro() {
    System.out.println("You are in a land full of dragons. In front of you,");
    System.out.println("you see two caves. In one cave, the dragon is friendly");
    System.out.println("and will share his treasure with you. The other dragon");
    System.out.println("is greedy and hungry, and will eat you on sight.");
    System.out.println("");
  }

  //Choosing a cave
  public static int getChoice() {
    String strCave;
    int intCave;

    while (strCave != "1" && strCave != "2") {
      System.out.println("Choose a cave (1 or 2)");
      strCave = dataIn.readLine();
      intCave = Integer.parseInt(strCave);
    }
    return intCave;
  }

  //Determining which cave you picked
  public static int doTest(int intCave) {
    Random ran = new Random();
    int friendlyCave = ran.nextInt(1) + 1;

  }
}

This is what happens when I compile:

E:\CMIN216\Adventure1.java:15: error: cannot find symbol
            strInput = dataIn.readLine();
                       ^
  symbol:   variable dataIn
  location: class Adventure1
E:\CMIN216\Adventure1.java:37: error: cannot find symbol
            strCave = dataIn.readLine();
                      ^
  symbol:   variable dataIn
  location: class Adventure1
2 errors

Tool completed with exit code 1

Code is incomplete, by the way. I was so busy on trying to fix this that I did not finish it.

user207421
  • 289,834
  • 37
  • 266
  • 440
  • The variable dataIn does not exist. Perhaps you wanted to create a BufferedReader called dataIn first. – Josh Vazquez Jul 15 '15 at 03:01
  • The error is telling you that you haven't declared or instantiated the variable 'dataIn'. As @JoshVazquez noted, from your usage you probably intended to make it a BufferedReader. BufferedReader dataIn = new BufferedReader(...). – spork Jul 15 '15 at 03:06
  • Looked over old code and noticed the BufferedReader was missing. Fixed one of the errors. How do I add it within the call? – Beau Worth Jul 15 '15 at 03:09
  • @BeauWorth, take a look at [this SO](http://stackoverflow.com/questions/11871520/how-could-i-read-input-from-the-console-using-the-scanner-class) – Codebender Jul 15 '15 at 03:11
  • Fixed your misleading title. Don't paraphrase error messages. Report them verbatim. – user207421 Jul 15 '15 at 03:21
  • Fixed. Replaced using scanners from what Codebender brought up. Thanks – Beau Worth Jul 15 '15 at 03:33

1 Answers1

0

I admit I forgot about BufferedReader, but Scanner worked just fine.

Scanner scanner2 = new Scanner(System.in);
StrCave = scanner2.nextLine();
    

Thanks, Codebender.