PDA

View Full Version : (JAVA) Basic I/O



Shaun
July 3rd, 2010, 18:33
This is just an example.

Ok well here's a program I made to demonstrate basic input/output operations in Java. It is documented to help you learn.

Open you're IDE and make a new java file called basicIO and in it put this.

[code=java]/**
* Pro Shaun (Shaun)
* 6/9/08
*
* Program: basicIO.java
* Description: Demonstrates basic inputs and outputs in Java
**/

/*Import required libraries for I/O*/
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class basicIO {

/*pointer in points to a new instance of BufferedReader which will be used to read/accept inputs*/
private static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

public static void main(String[] args) {
int quit = 0; // will be used to repeat the program

while (quit != 2) {
try { // input must have a try/catch

/*Input a String of text*/
System.out.print("Input a String of text: "); // tell user what to do
String inputString = in.readLine(); // reads the String entered

/*Input a Character*/
System.out.print("Input a Character: "); // tell user what to do
char inputChar = ((in.readLine()).toCharArray())[0]; // reads the character(s) entered, ignores all but the first one

/*Input a double*/
System.out.print("Input a double value: "); // tell user what to do
double inputDouble = Double.parseDouble(in.readLine()); // reads the double entered

/*Input an integer*/
System.out.print("Input an Integer value: "); // tell user what to do
int inputInt = Integer.parseInt(in.readLine()); // reads the int entered

/*Output*/
System.out.println("\n"+"Inputted Sting: "+inputString+"\n"+
"Inputted char: "+inputChar+"\n"+
"Inputted double: "+inputDouble+"\n"+
"Inputted int: "+inputInt+"\n");

/*Input an int for int quit to see if it will be run again*/
System.out.print("Do you wish to run the program again?(1 yes, 2 no): "); // tell user what to do
quit = Integer.parseInt(in.readLine()); // read integer inputted
System.out.println(""); // print another line for the sake of readability

}catch (Exception e) { // catch errors
System.out.println("\n"+"Error: Bad input!"+"\n"); // if an error occurs, tell user what happened
}
}
System.out.println("Thank you for using the program, bye."); // just some end-program output
}
}

and here's a sample run:

--------------------Configuration: basicIO - JDK version 1.6.0_05 <Default> - <Default>--------------------
Input a String of text: I herd yu lyke mudkipz
Input a Character: W
Input a double value: 345.404
Input an Integer value: 33245

Inputted Sting: I herd yu lyke mudkipz
Inputted char: W
Inputted double: 345.404
Inputted int: 33245

Do you wish to run the program again?(1 yes, 2 no): 1

Input a String of text: Multiple runs FTW
Input a Character: D
Input a double value: 43.2
Input an Integer value: -30

Inputted Sting: Multiple runs FTW
Inputted char: D
Inputted double: 43.2
Inputted int: -30

Do you wish to run the program again?(1 yes, 2 no): 2

Thank you for using the program, bye.

Process completed.

Errors handled:

--------------------Configuration: basicIO - JDK version 1.6.0_05 <Default> - <Default>--------------------
Input a String of text: Lets test 4 errorz
Input a Character: R
Input a double value: not a double

Error: Bad input!

Input a String of text: hehe
Input a Character: G
Input a double value: 34
Input an Integer value: not an int

Error: Bad input!

Input a String of text: ok lets do it right
Input a Character: f
Input a double value: 43.5
Input an Integer value: 3

Inputted Sting: ok lets do it right
Inputted char: f
Inputted double: 43.5
Inputted int: 3

Do you wish to run the program again?(1 yes, 2 no): 2

Thank you for using the program, bye.

Process completed.

This program Takes a String as input, then a character. If you input multiple characters, this automatically just selects the first one entered. Next it takes a double value and finally an int. Then it reiterates it to show you it actually worked.

Canownueasy`
July 5th, 2010, 04:34
Use proper Conventions please!

Shaun
July 14th, 2010, 16:14
Use proper Conventions please!

lulzwut? WHY?!

samuraiblood2
July 15th, 2010, 07:38
Ever hear of the Scanner class?

Only the registered members can see the link.

reese
July 20th, 2010, 03:13
Ever hear of the Scanner class?

Only the registered members can see the link.

For real, seems easier then doing this.