-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

3
  • Please read a tutorial on IO, this one maybe. Commented Jan 15, 2014 at 18:12
  • Also, that is not an error. Commented Jan 15, 2014 at 18:12
  • Scanner#nextLine() .. Example : String s = input.nextLine() Commented Jan 15, 2014 at 18:16

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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);
    }


}

}

Comments

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.

1 Comment

@Nicken99, My friend if this works for please right click on the answer, so it will be helpful for others
-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);  

 }  
}   

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.