1

I tried to make a little program with Scanner but it says that my While loop is incorrect. The error message is: 'line cannot be resolved to a variable'

I tried to fix this problem, but when I do, another error message comes up!

Here, I put the code:

import java.util.Scanner;

public class Application {
    public static void main(String[] args) {

        while (line != "yes") {

            Scanner input = new Scanner(System.in);
            System.out.println("Do you want some chocolate?");
            String line = input.nextLine();
            System.out.println("You are wrong!");
        }

        System.out.println("Yup! Good answer! ^.^");

    }
}

What is incorrect? Thanks in advance for your understanding.

4 Answers4

3

You have to put the definition of line outside of your while loop. It is not defined by that point in your current code. By the way, you should also only initialize your Scanner once.

public static void main(String[] args) {
    String line = "";
    Scanner input = new Scanner(System.in); // Notice that I moved "input" here.
    while (!line.equals("yes")) {
        System.out.println("Do you want some chocolate?");
        line = input.nextLine();
        System.out.println("You are wrong!");
    }

    System.out.println("Yup! Good answer! ^.^");
}

Note also that you have to use String#equals instead of just == for comparing Strings.

A better way to do this while loop would be to use a do-while instead, like so:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    String line;
    do {
        System.out.println("Do you want some chocolate?");
        line = input.nextLine();
        System.out.println("You are wrong!");
    } while (!line.equals("yes"));

    System.out.println("Yup! Good answer! ^.^");
}

As Tom stated, this will preclude the need for initializing line with the empty string.

As Jonathan stated, you might want to use equalsIgnoreCase instead of just equals so that the user can enter "yes", "YES", "YeS", or any other combination of cases. However, this is purely your choice.

Jashaszun
  • 9,207
  • 3
  • 29
  • 57
1

line is used before it is declared. please declare line outside the while loop.

Andreas
  • 4,937
  • 2
  • 25
  • 35
0

You have declared line inside the while loop. This means that it is unavailable at to use in the condition. You should declare it (and the Scanner) before the beginning of your while loop instead.

Serlite
  • 12,130
  • 5
  • 38
  • 49
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
0

in this case, i suppose, you should use a do while loop, because you have to ask the question at least once before checking the condition. I wrote the following solution and tested, it works.

    import java.util.Scanner;

    public class dowhile {

        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            String line = "";
            do {

                System.out.println("Do you want some chocolate?");
                line = input.nextLine();
                System.out.println("You are wrong!");
            } while (!(line.equals("yes")));

            System.out.println("Yup! Good answer! ^.^");

        }
    }


One thing to note is that you cannot compare strings in java using == or !=, because strings are not premitives, they are objects.
I have used a String object and used "equals" method to compare the values of the strings. you can also use "compareTo" method, which will compare the actual string objects not values.

Bmax
  • 590
  • 4
  • 11
  • 24