package Lab09;
public class Lab09Driver {
public static void main(String[] args) {
new Lab09Driver();
}
public Lab09Driver() {
Scanner input = new Scanner(System.in);
System.out.println("Would you like to load a previous file? (Y/N)");
String.choice = input.nextLine().trim().toUpperCase();
if (choice.charAt(0) == 'Y') {
System.out.println("Enter the name of the file that you want to load:");
String.fileName = input.nextLine();
try {
Scanner fileInput = new Scanner(new File(fileName));
while(fileInput.hasNextLine()){
String answer = fileInput.nextLine();
System.out.println(answer);
}fileInput.close();
} catch (FileNotFoundException e) {
System.out.println("Could not find the file, and the program is exiting");
System.exit(0);
}
} else {
String[] questions = {
"How old are you?",
"What city were you born in?",
"What is your favorite pets name?",
"What color are your eyes?",
"What is your mother's first name?",
"What is your favorite sport?",
"Do you have any siblings?"};
PrintStream pStream = null;
try {
File outFile = new File("myFile.txt");
FileOutputStrem fos = new FileOutputStream(outFile);
pStream = new PrintStream(fos);
for (String.question : questions) {
System.out.println(question);
String answer = input.nextLine();
pStrem.println(answer);
}
}catch (FileNotFounderException e) {
e.printStackTrack();
} finally {
pStream.close();
}
}
}
}
What I have to do to fix this programimport java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Scanner;
public class Lab09Driver {
public static void main(String[] args) {
new Lab09Driver();
}
public Lab09Driver() {
Scanner input = new Scanner(System.in);
System.out.println("Would you like to load a previous file? (Y/N)");
String choice = input.nextLine().trim().toUpperCase();
if (choice.charAt(0) == 'Y') {
System.out.println("Enter the name of the file that you want to load:");
String fileName = input.nextLine();
try {
Scanner fileInput = new Scanner(new File(fileName));
while (fileInput.hasNextLine()) {
String answer = fileInput.nextLine();
System.out.println(answer);
}
fileInput.close();
} catch (FileNotFoundException e) {
System.out.println("Could not find the file, and the program is exiting");
System.exit(0);
}
} else {
String[] questions = {
"How old are you?",
"What city were you born in?",
"What is your favorite pets name?",
"What color are your eyes?",
"What is your mother's first name?",
"What is your favorite sport?",
"Do you have any siblings?"};
PrintStream pStream = null;
try {
File outFile = new File("myFile.txt");
FileOutputStream fos = new FileOutputStream(outFile);
pStream = new PrintStream(fos);
for (String question : questions) {
System.out.println(question);
String answer = input.nextLine();
pStream.println(answer);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
pStream.close();
}
}
}
}
package Lab09; public class Lab09Driver { public static void main(String[] args) { new Lab09Driver(); } public...
Problem: Use the code I have provided to start writing a program that accepts a large file as input (given to you) and takes all of these numbers and enters them into an array. Once all of the numbers are in your array your job is to sort them. You must use either the insertion or selection sort to accomplish this. Input: Each line of input will be one item to be added to your array. Output: Your output will...
import java.util.Scanner; import java.io.File; public class Exception2 { public static void main(String[] args) { int total = 0; int num = 0; File myFile = null; Scanner inputFile = null; myFile = new File("inFile.txt"); inputFile = new Scanner(myFile); while (inputFile.hasNext()) { num = inputFile.nextInt(); total += num; } System.out.println("The total value is " + total); } } /* In the first program, the Scanner may throw an...
import java.util.Scanner; import java.util.ArrayList; public class P3A2_BRANDT_4005916 { public static void main(String[] args) { String name; String answer; int correct = 0; int incorrect = 0; Scanner phantom = new Scanner(System.in); System.out.println("Hello, What is your name?"); name = phantom.nextLine(); System.out.println("Welcome " + name + "!\n"); System.out.println("My name is Danielle Brandt. " +"This is a quiz program that...
import java.util.Scanner; public class StudentClient { public static void main(String[] args) { Student s1 = new Student(); Student s2 = new Student("Smith", "123-45-6789", 3.2); Student s3 = new Student("Jones", "987-65-4321", 3.7); System.out.println("The name of student #1 is "); System.out.println("The social security number of student #1 is " + s1.toString()); System.out.println("Student #2 is " + s2); System.out.println("the name of student #3 is " + s3.getName()); System.out.println("The social security number...
import java.util.Scanner; public class Client{ public static void main(String args[]){ Coin quarter = new Coin(25); Coin dime = new Coin(10); Coin nickel = new Coin(5); Scanner keyboard = new Scanner(System.in); int i = 0; int total = 0; while(true){ i++; System.out.println("Round " + i + ": "); quarter.toss(); System.out.println("Quarter is " + quarter.getSideUp()); if(quarter.getSideUp() == "HEADS") total = total + quarter.getValue(); dime.toss(); System.out.println("Dime is " + dime.getSideUp()); if(dime.getSideUp() == "HEADS") total = total +...
import java.util.Scanner; public class Age { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int a; System.out.println("Input your age"); a = sc.nextInt(); boolean mess = isAllowed(a); String mess2 = ?isAllowed(a)return"You are allowed to vote";:"You arent allowed"; String age = personAge(a); personAge(a); } public static String personAge(int age) { String mess = ""; if(age<18) return mess = "You are minor"; else if(age>=18 && age<=22) return mess = "You are legal you can vote"; else if(age>=22 && age<=60) return...
import java.util.Scanner; public class TriangleMaker { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("Welcome to the Triangle Maker! Enter the size of the triangle."); Scanner keyboard = new Scanner(System.in); int size = keyboard.nextInt(); for (int i = 1; i <= size; i++) { for (int j = 0; j < i; j++) { System.out.print("*"); } System.out.println(); } for (int...
import java.util.Scanner; public class LabProgram { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); String inputMonth; int inputDay; inputMonth = scnr.nextString(); inputDay = scnr.nextInt(); String season; if(inputMonth == "April","May","June"){ String season = "Spring"; }else if(inputMonth == "July","August","September"){ String season = "Summer"; }else if(inputMonth == "October","November","December"){ String season = "Autumn"; }else if(inputMonth == "January","February","March"){ System.out.println("Winter"); }else{ System.out.println("Invalid"); } if((inputMonth == "March") && (inputDay >= 20)){ String season = "Spring"; }else if((inputMonth == "June") && (inputDay >= 21)){...
import java.io.*; import java.util.Scanner; public class CrimeStats { private static final String INPUT_FILE = "seattle-crime-stats-by-1990-census-tract-1996-2007.csv"; private static final String OUTPUT_FILE = "crimes.txt"; public static void main(String[] args) { int totalCrimes = 0; // read all the rows from the csv file and add the total count in the totalCrimes variable try { Scanner fileScanner = new Scanner(new File(INPUT_FILE)); String line = fileScanner.nextLine(); // skip first line while (fileScanner.hasNext()) { String[] tokens = fileScanner.nextLine().split(","); if (tokens.length == 4) { totalCrimes +=...
import java.util.Scanner; public class EggBasket2 { public static void main (String [] args) { int numberOfBaskets, eggsPerBasket, totalEggs; Scanner keyboard = new Scanner (System.in); System.out.println ("Enter the number of eggs in each basket:"); eggsPerBasket = keyboard.nextInt (); System.out.println ("Enter the number of baskets:"); numberOfBaskets = keyboard.nextInt (); totalEggs = numberOfBaskets * eggsPerBasket; System.out.println ("If you have"); System.out.println (eggsPerBasket + " eggs per basket and"); System.out.println (numberOfBaskets + " baskets, then"); System.out.println ("the total number of eggs is " + totalEggs);...