Skip to Content
bytegarden

Input Methods

Numeric Input

  • nextInt() - reads integer
  • nextFloat() - reads float
  • nextDouble() - reads double
  • nextByte() - reads byte
  • nextShort() - reads short
  • nextLong() - reads long

Text Input

  • next() - reads single word
  • nextLine() - reads full line
  • nextBoolean() - reads boolean
  • next().charAt(int index) - reads character at index

Validation Methods

  • hasNextInt() and similar methods
  • Returns boolean (true/false)
  • Useful for input validation before reading

System References

  • System.in - built-in object reference to keyboard
  • System.out - built-in object reference to monitor/display

Command Line Arguments

  • Accessible through args[] in the program

  • Reference: See Command Line example (CommandLine.java, below)

    CommandLine.java
    // using command line arguments in a java program
    
    public class CommandLine {
        public static void main(String[] args) {
            System.out.println("Command Line Arguments");
            // if (args.length > 0) {
            //     System.out.println(args[0]);
            // } else {
            //     System.out.println("No arguments provided for args[0]");
            // }
            // if (args.length > 1) {
            //     System.out.println(args[1]);
            // } else {
            //     System.out.println("No arguments provided for args[1]");
            // }
            System.out.println(args[0]);
            System.out.println(args[1]);
        }
    }
    Runs in your browser with Java 21. Ctrl/โŒ˜ + Enter to run.Built with CheerpJ