//The following three lines are needed to get a number from the keyboard. import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Sum3 { public static void main(String[] args) { System.out.print("What number do you want to sum up to? "); int n = getInt(); int sum = 0; for (int i = 1; i <= n; i = i + 1) { sum += i; } System.out.println("The sum of the numbers from 1 to " + n + " is " + sum + "."); } //The following 16 lines are needed to get a number from the keyboard. private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); private static int getInt() { try { return Integer.parseInt(reader.readLine()); } catch (IOException e) { System.err.println("Couldn't receive input: " + e.getMessage()); } catch (NumberFormatException e) { System.err.println(e.getMessage() + ", the characters were not a number."); } return 0; } }