0

I am using netbeans.

I am trying to read standard in or arguments using Scanner. However whenever I try to display something, I get something completely different.

Scanner input = new Scanner(System.in);

    System.out.println(input);

So, for example if the preset command line argument is "Awesome!" I want it to print out Awesome! But I get some long gibberish such as :

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]

Jeff
  • 12,555
  • 5
  • 33
  • 60
  • so i tried using Scanner s = new Scanner(System.in); String input = s.nextLin(); System.out.println(input); And it still won't print out my command line argument – GreatShark Feb 15 '16 at 05:51
  • Okay, anyway, I used command prompt to compile and run instead of an IDE like Net Beans. Don`t know why Net Beans gives me a weird answer. – GreatShark Feb 16 '16 at 19:42

2 Answers2

1

Here Scanner is the class name, a is the name of object, new keyword is used to allocate the memory and System.in is the input stream. you can use following methods of Scanner class (for your case its string) :

  1. nextInt to input an integer
  2. nextFloat to input a float
  3. nextLine to input a string

    public static void main(String[] args) {
    
        Scanner in = new Scanner(System.in);
        String user_value = in.nextLine();        
        System.out.println(user_value);
    }
    
Omal Perera
  • 2,971
  • 3
  • 21
  • 26
  • I try to do this in net beans with the command line arguments and nothing gets printed? – GreatShark Feb 15 '16 at 06:28
  • @GreatShark - just copy above code and paste inside the main method. (You should import `java.util.Scanner;`) . Then run it and type something in console and press enter. -exactly it should works!! – Omal Perera Feb 15 '16 at 07:04
0

Here is the code you should use:

import java.util.Scanner; 

public class InputTesting { 

    public static void main( String[] args ) { 
        Scanner input = new Scanner ( System.in ); 
        String str1; 
        System.out.println("Input string: "); 
        str1 = input.next(); 
        System.out.println( str1 ); 
    }
}
Vic
  • 21,473
  • 11
  • 76
  • 97
omer727
  • 7,117
  • 6
  • 26
  • 39