0

I am working on a Java code:

import java.util.Scanner;

public class main
{
    static Scanner console = new Scanner (System.in);
    public static void main (String aurgs[]) {

        System.out.print("Which function would you like to vist? L for Lottery, R for Random Math");
        char lotto = 'l';
        char rdm = 'b';

        if (console.nextChar() = lotto) /**error is here*/ {
            lottery();
        }
    }
    public static void lottery (String aurgs[]) {
        String announ = "Your lottery numbers for today are: ";
        System.out.print(announ);
        int [] numbers = {15, 68, 25, 66};
        double lucky = 50.2;
        for (int i: numbers )
            System.out.print(i + "\n");

        String announ2 = "Your lucky number is: ";
        System.out.println(announ2 + lucky);
        }
}

When I compile it, BlueJ says:

cannot find symbol - method nextChar()

I do not understand what is wrong.

Brandon Nguyen
  • 345
  • 2
  • 3
  • 15
  • You can't just make up methods and expect them to work. No, programming is an exercise in precision, and compilers are unforgiving. You'll want to learn to use the Java API and use it a **lot**. – Hovercraft Full Of Eels Oct 17 '13 at 03:37
  • It may be because you are using the assignment operator = instead of the comparison operator == in your if clause, but @Hovercraft is probably right. – David Fleeman Oct 17 '13 at 03:38
  • Being a bit more specific: There is no `Scanner#nextChar()` method. (But there are a number of similar questions, which makes me thing some professor or class notes started with an incorrect assertion ..) – user2864740 Oct 17 '13 at 03:39
  • Use some IDEs such as Eclipse, they will tell you what methods available to you. – Chowdappa Oct 17 '13 at 03:40
  • See also: http://stackoverflow.com/questions/18746185/why-doesnt-the-scanner-class-have-a-nextchar-method , http://stackoverflow.com/questions/19340543/how-could-i-extend-java-util-scanner-class-to-add-a-nextchar-method?lq=1 – user2864740 Oct 17 '13 at 03:41

3 Answers3

3

The Scanner class has no nextChar() method. The methods available are listed here, none of which are nextChar(). You can instead use next() and read a single character as a string.

hexacyanide
  • 88,222
  • 31
  • 159
  • 162
2

I do not understand what is wrong.

It is simple. Scanner doesn't have nextChar() method.

If you want to read characters with Scanner you can change its delimiter to empty string ""

console.useDelimiter("");

and use next() method.


Also you should add one more = in

if (console.nextChar() = lotto)

if you want to compare chars (single = is used for assigning values).

Pshemo
  • 122,468
  • 25
  • 185
  • 269
1

In order to get a char from a Scanner you need to use next(), which returns a String. Then just get the first char from that.

char x = console.next().charAt(0);
yamafontes
  • 5,552
  • 1
  • 18
  • 18