-3

I'm don't know how to read from the console in Java. If it's possible I want to do it using a scanner. This is what i tried while learning Java.

package Scanners;

import java.util.Scanner;

public class ConsoleScanner {


    static Scanner input = new Scanner(System.in);

    public static void main(String[] args){

        if(input.equals("Hello"))
            System.out.println("You typed in: Hello ");
        if(input.equals("Good Bye"))
            System.out.println("You typed in: Good Bye");
        else{
            System.out.println("You typed in: " + input);
        }


    }

}

It give's me this error:

You typed in: java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=.][decimal separator=\,][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q?\E][infinity string=\Q?\E]

If there is a better way to read from the console then please post it. - Thanks

Nicken99
  • 9
  • 1
  • 5

4 Answers4

0

You don't want to print the Scanner itself. You want to call the various Scanner functions to get the input. Have a look at the Scanner API and the Scanner tutorial (which are the first and second results for googling "Java Scanner") for more info.

Kevin Workman
  • 39,413
  • 8
  • 61
  • 94
0

Try this:

package Scanners;

import java.util.Scanner;

public class ConsoleScanner {

static Scanner scanner = new Scanner(System.in); //Creates the scanner

public static void main(String[] args){

    String input = scanner.NextLine();  //Sets the string input equal to whatever the user types next

    if(input.equals("Hello"))
        System.out.println("You typed in: Hello ");
    if(input.equals("Good Bye"))
        System.out.println("You typed in: Good Bye");
    else{
        System.out.println("You typed in: " + input);
    }


}

}

J Code
  • 524
  • 1
  • 6
  • 18
0

My friend you have to use

Scanner scanner= new Scanner(System.in);
    input = scanner.next();

This method finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern.

Girish
  • 1,667
  • 1
  • 16
  • 28
-1
import java.util.Scanner;  
class ScannerDemo{  
 public static void main(String args[]){  

   Scanner sc=new Scanner(System.in);  

   System.out.println("Enter your age");  
   int age=sc.nextInt();  

   System.out.println("age:"+age);  

 }  
}   
Raju Rathore
  • 149
  • 5