Skip to content

Instantly share code, notes, and snippets.

@Spikey3
Created February 13, 2012 19:30
Show Gist options
  • Save Spikey3/1819366 to your computer and use it in GitHub Desktop.
Save Spikey3/1819366 to your computer and use it in GitHub Desktop.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
public class Calculator {
/**
* @param args
*/
public static void main(String[] args) {
// declaring reference to array
int anArray[];
// Create array with 3 elements
anArray = new int[3];
// Display"Enter three numbers"
System.out.println("Enter three numbers");
// specify the reader variable
BufferedReader reader;
// to be a standard input buffer
reader = new BufferedReader(new InputStreamReader(System.in));
boolean done = false;
int position = 0;
//Placing Numbers written by user into array
while (!done) {
try {
String value = reader.readLine();
Integer i = Integer.valueOf(value);
anArray[position] = i;
position++;
//Declaring after three integers imputed by user array is "true"
if (position >= 3) {
done = true;
}
//Creating an exception for if user imputes non integer
} catch (IOException ex) {
ex.printStackTrace();
} catch (NumberFormatException ex) {
System.out.println("That wasn't a number, try again.");
}
}
{
//Equation Array position 0 + Array position 1 + Array position 2
int a = anArray [0]+ anArray[1]+anArray[2];
//System out Arrays
System.out.println("The answer is " + a);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment