Saturday, October 13, 2012

Java Input / Output

In the first program we had experience to show an output using "System.out.println()". You can also use "System.out.print()". The only difference is just .print will print your output continously while .println will print your output on the next line.

Now, we will be doing a program that will accept input. With this we can use the java packages,
  1. java.io.BufferedReader
  2. java.io.InputStreamReader
  3. java.util.Scanner

Let us use first java.io.BufferedReader and java.io.InputStreamReader
Just create new java application, then write this code.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bufferedreader;

/**
 *
 * @author Giovanni
 */
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class BUfferedReader {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {
        // TODO code application logic here
        System.out.print("Enter your name: ");
        InputStreamReader reader = new InputStreamReader(System.in);
        BufferedReader in = new BufferedReader(reader);
        String name = in.readLine();
        System.out.println("Hello, " + name + ". Enter three ints...");
        int[] values = new int[3];
        double sum = 0.0;
        for (int i = 0; i < values.length; i++) {
        System.out.print("Number " + (i + 1) + ": ");
        String temp = in.readLine();
        values[i] = Integer.parseInt(temp);
        sum += values[i];
        }
        System.out.println("The average equals " + sum / values.length);
    }
}


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bufferedreadercont;

/**
 *
 * @author Giovanni
 */
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class BUfferedReaderCont {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        BufferedReader br = null;
        try {
            InputStreamReader inp = new InputStreamReader(System.in);
            br = new BufferedReader(inp);
            char ans;
            do {
                System.out.println("Enter Roll number : ");
                int rno = Integer.parseInt(br.readLine());

                System.out.println("Enter name: ");
                String name = br.readLine();

                System.out.println("Continue y/n: ");
                ans = (char) br.read();
                br.readLine();
            } while (ans == 'y');
        } finally {
            if (br != null) {
                br.close();
            }
        }

    }
}


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package readconsole;

/**
 *
 * @author Giovanni
 */
import java.util.Scanner; 

public class ReadConsole {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner scanner = new Scanner(System.in); 
 
        System.out.print("Enter your full name: "); 
        String name = scanner.nextLine(); 
         
        System.out.print("Enter your Zodiac sign: "); 
        String zodiac = scanner.next(); 
         
        System.out.print("Enter your weight (kg): "); 
        double weight = scanner.nextDouble(); 
         
        System.out.print("Enter your lucky number: "); 
        int luckyNum = scanner.nextInt(); 
         
        System.out.println("Hello, " + name + "."); 
        System.out.println("Your lucky number is  " + luckyNum + "."); 
        System.out.println("You weigh " + weight + " kg."); 
        System.out.println("Your Zodiac sign is " + zodiac + ".");
    }
}


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package parsestring;

/**
 *
 * @author Giovanni
 */
import java.util.Scanner;

public class ParseString {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner scanner = new Scanner("1 2 3 4 5 6 7 8 9 10"); 
 
        while (scanner.hasNextInt()) { 
            int num = scanner.nextInt(); 
             
            if (num % 2 == 0) 
                System.out.println(num); 
        } 
    }
}


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package parsestring;

/**
 *
 * @author Giovanni
 */
import java.util.Scanner;

public class ParseString {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner scanner =  new Scanner("1, 2, 3, 4, 5, 6, 7, 8, 9, 10").useDelimiter(", "); 
 
        while (scanner.hasNextInt()) { 
            int num = scanner.nextInt(); 
             
            if (num % 2 == 0) 
                System.out.println(num); 
        }
    }
}



No comments:

Post a Comment