Java STDIO Examples

Here are some STIO examples I found online. If they do not work -- let me know. The first example is a teamm supplied code sample. The others ame from http://www.loirak.com/prog/java.php

  1. Provided by 2010 Team America (from ULL)
     import java.util.*;
     import java.io.*;
    
     public class example {
        public static void main(String[] args){
            Scanner in = new Scanner(System.in);
            int num = in.nextInt();
            for (int i=0; i<num; i++) {
                    String carnum = in.next();
                    String name = in.nextLine();
                    System.out.println(name+" drives car number "+carnum);
            }
        }
    }
     
  2. class Hello {
      public static void main (String[] args) {
        System.out.println("Welcome to the world of Java Programming.");
      } // method main
    } // class Hello
  3. import java.io.*; //include Java's standard Input and Output routines
    
    class Echo {
      public static void main (String[] args) throws IOException {
    
        // Defines the standard input stream
        BufferedReader stdin = new BufferedReader
          (new InputStreamReader(System.in));
        String message; // Creates a varible called message for input
    
        System.out.print ("Enter the message : ");
        System.out.flush(); // empties buffer, before you input text
        message = stdin.readLine();
    
        System.out.print("You "); 
        System.out.println("entered : " + message);
    
      } // method main
    } // end of class Echo
  4. import java.io.*; 
    
    class Candy {
      public static void main (String[] args) throws IOException {
    
        BufferedReader stdin = new BufferedReader
          (new InputStreamReader(System.in));
        int num1, num2, sum; // declares three integers
        double dollars; // declares a number that can have decimals
    
        System.out.print ("How many candy bars do you want  : ");
        System.out.flush(); 
    
        // read a line, and then converts it to an integer
        num1 = Integer.parseInt( stdin.readLine());
    
        System.out.print ("How many suckers you do want  : ");
        System.out.flush(); 
        num2 = Integer.parseInt( stdin.readLine());
    
        sum = num1 + num2; // Adds the two numbers;
        dollars = (double) sum * .75;
        System.out.println("You owe : $" + dollars);
      } // method main
    } 
  5. import java.io.*; 
    
    class Loopit {
      public static void main (String[] args) throws IOException {
        BufferedReader stdin = new BufferedReader
          (new InputStreamReader (System.in));
        int count, max, num;
        num = 0; // Assign initial value of count
        while (num != -1) {
          System.out.print ("Enter a number to factorialize (-1 to quit): ");
          System.out.flush();
          num = Integer.parseInt (stdin.readLine());
          max = 1; // Assign to 1, so factorial isn't zero every time
          if (num == -1) { 
            System.out.println("Okay, quiting...");
          }
          else { // Since they're not quitting we better factorialize
            for  (count = 1; count<=num; count++) {
              max = count * max;
            }    
            System.out.println (num+"! (factorial) is : "+ max);
          }
        } 
      } // method main
    }