Assignemnt #125 and Summing Three Numbers From Any File
Code
/// Name: Raff Lisbona
/// Period: 6
/// Program Name: Summing Three Numbers From any file
/// File Name: SummingThreeNumsFromAnyFile.java
/// Date Finished: 5/25/2016
import java.util.Scanner;
import java.io.File;
public class SumFromAnyFile
{
public static void main(String[] args) throws Exception
{
String file;
Scanner kb = new Scanner(System.in);
System.out.print("Which file would you like to read numbers from: ");
file = kb.next();
if( file.equals("3nums1.txt"))
{
System.out.println("Reading numbers from file \"3nums1.txt\"");
int num1, num2, num3;
Scanner fileIn = new Scanner( new File("3nums1.txt"));
num1 = fileIn.nextInt();
num2 = fileIn.nextInt();
num3 = fileIn.nextInt();
int sum = num1 + num2 + num3;
System.out.println(num1 + " + " + num2 + " + " + num3 + " = " + sum);
fileIn.close();
}
else if ( file.equals("3nums2.txt"))
{
System.out.println("Reading numbers from file \"3nums2.txt\"");
int num1, num2, num3;
Scanner fileIn = new Scanner( new File("3nums2.txt"));
num1 = fileIn.nextInt();
num2 = fileIn.nextInt();
num3 = fileIn.nextInt();
int sum = num1 + num2 + num3;
System.out.println(num1 + " + " + num2 + " + " + num3 + " = " + sum);
fileIn.close();
}
else if ( file.equals("3nums3.txt"))
{
System.out.println("Reading numbers from file \"3nums3.txt\"");
int num1, num2, num3;
Scanner fileIn = new Scanner( new File("3nums3.txt"));
num1 = fileIn.nextInt();
num2 = fileIn.nextInt();
num3 = fileIn.nextInt();
int sum = num1 + num2 + num3;
System.out.println(num1 + " + " + num2 + " + " + num3 + " = " + sum);
fileIn.close();
}
}
}
Picture of the output