4

I currently have this code that needs user input to pseudo-ify some words to send out and commit obfuscation on the integer later. I just need to get the user input. Anyone have a good source of api to get this from? I think I should be looking for System. commands.

Thanks in advance :)

Community
  • 1
  • 1
user2262111
  • 563
  • 1
  • 6
  • 16

4 Answers4

14

The Scanner class was implemented in Java 5.0 to make getting input easier:

Scanner input = new Scanner(System.in) the System.in will allow for console input.

Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
/* will wait for input then assign it to the variable,
 * in this case it will wait for an int.
 */
System.out.println(i); // will print the variable

https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

Stormblessed
  • 201
  • 4
  • 16
Joban
  • 1,318
  • 7
  • 19
4

Are you looking for this? As you asked about getting input via command line so if you run your java program via command line and want to pass it some inputs, they are received by args parameter in main. look here

Echoing Command-Line Arguments

The Echo example displays each of its command-line arguments on a line by itself:

public class Echo {
    public static void main (String[] args) {
        for (String s: args) {
            System.out.println(s);
        }
    }
}
nommyravian
  • 1,316
  • 2
  • 12
  • 30
  • This is completely irrelevant. I'm looking for input, not output. – user2262111 Apr 10 '13 at 00:13
  • Don't you think the input is coming from command line through the 'args' parameter??? It's the way for getting input from command line and printing it on console. What else did you ask? – nommyravian Apr 10 '13 at 00:15
  • There is no input? How do you get input from the String[] args? My question was I wanted to incorporate user input so I can take it and user comparison commands with it. – user2262111 Apr 10 '13 at 00:20
  • Do you know how to run a java prgram from command line? When you've a java program say HelloWorld.java and want to run it using command line and take user inputs, you go open cmd, go to the directory where HelloWorld.java is, run the code and type the user inputs in command line. That's what my answer is all about because your asked how to get input from command line. Got it? – nommyravian Apr 10 '13 at 00:25
  • 1
    see here if you need to know how to run a program from command line; http://www.skylit.com/javamethods/faqs/javaindos.html – nommyravian Apr 10 '13 at 00:25
  • I thought that you are running your program via command line. it's better to go through your question before down voting an answer. – nommyravian Apr 10 '13 at 00:28
  • I sorted you out @nommyravian. Not sure why I bothered either! – BrantApps Apr 10 '13 at 00:38
  • What? When I run a class, it exits after executing... – user2262111 Apr 10 '13 at 00:38
  • Oh I see. I can handle batch. I know just about every command. I think your thinking of commands like how to I do something like make a custom command. ex: killbob – user2262111 Apr 10 '13 at 00:40
2

You might want to use the Scanner class: java.util.Scanner

The basic setup is simple:

Scanner scan = new Scanner(System.in);
int number = scan.nextInt();

This sets up a scanner object and makes the scanner get an integer from the default input stream. However, this doesn't check to see if the user entered "good data". It will cause errors if the user enters anything other than a number. You might want to include a loop until the user enters valid data.

Here's the same code, but with error checking:

Scanner scan = new Scanner(System.in);
boolean validData = false;
int number=0;
do{
    System.out.println("Enter a Number");
    try{
        number = scan.nextInt();//tries to get data. Goes to catch if invalid data
        validData = true;//if gets data successfully, sets boolean to true
    }catch(InputMismatchException e){
        //executes when this exception occurs
        System.out.println("Input has to be a number. ");
    }
}while(validData==false);//loops until validData is true

This program will ask the user to enter a number. If the user enters bad data, an InputMismatchException is thrown, taking the program to the catch clause and skiping setting validData to true. This loops until valid data is entered.

Hope this helps.

dmichaelc
  • 101
  • 5
  • The `Scanner` class includes methods like `hasNext`, `hasNextInt`, `hasNextBoolean` etc, which makes it a lot easier. – Lone nebula Apr 10 '13 at 00:30
0

Yea, System.in will work fine for a small number of predictably entered commands. Apache has a nice little tool if you want a more pro/robust mechanism.

See: http://commons.apache.org/proper/commons-cli/

BrantApps
  • 6,362
  • 2
  • 27
  • 60
  • Ah SO. You weird thing. Yes apparently 2 routes of parsing command line inputs one from Apache Commons is negative input. Today gets stranger and stranger. – BrantApps Apr 10 '13 at 00:28
  • Thanks for posting your answer! Please note that you should post the useful points of an answer here, on this site, or your post risks being deleted [See the FAQ where it mentions answers that are 'barely more than a link'](http://stackoverflow.com/faq#deletion). You may still include the link if you wish, but only as a 'reference'. The answer should stand on its own without needing the link. The down vote *may* be because the fact that your answer is relevant is not immediately obvious by what you have typed here; but if you had included more, it would have been. – Andrew Barber Apr 10 '13 at 02:14
  • Thanks for the explanation. I have fallen foul of this once before. However in this instance, the required code to make it work "OK" followed by a caveat of using the simplistic approach and then an alternative to the specified approach outlined by others should move this out of that bracket. I am a fan of brevity and hate 'Chinese Whispers' of other's interpretation of a change. – BrantApps Apr 10 '13 at 08:11