Related questions
I need help with this java problem so it can output like this in the image attached:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Scanner;
public class LabProgram {
public static void main(String[] args) throws IOException {
Scanner scnr = new Scanner(System.in);
// Initialize variables for calculating exam averages
int count = 0;
double sumMid1 = 0, sumMid2 = 0, sumFinal = 0;
// Initialize FileOutputStream for output
FileOutputStream fos = new FileOutputStream("report.txt");
// Prompt for the filename
System.out.print("Enter the name of the TSV file: ");
String fileName = scnr.nextLine();
FileInputStream fis = null;
try {
fis = new FileInputStream(fileName);
// Read the file and build the content string
StringBuilder sb = new StringBuilder();
int ch;
while ((ch = fis.read()) != -1) {
sb.append((char) ch);
}
// Split the content by lines
String[] lines = sb.toString().split("\n");
// Process each line
for (String line : lines) {
// Make sure the line contains sufficient data
if (line.isEmpty()) {
continue;
}
String[] tokens = line.trim().split("\t");
// Check the number of tokens
if (tokens.length < 5) {
System.out.println("Skipping invalid line: " + line);
continue;
}
try {
// Read student information
String lastName = tokens[0];
String firstName = tokens[1];
int mid1 = Integer.parseInt(tokens[2].trim());
int mid2 = Integer.parseInt(tokens[3].trim());
int finalExam = Integer.parseInt(tokens[4].trim());
// Calculate the average and grade
double average = (mid1 + mid2 + finalExam) / 3.0;
String grade;
if (average >= 90) grade = "A";
else if (average >= 80) grade = "B";
else if (average >= 70) grade = "C";
else if (average >= 60) grade = "D";
else grade = "F";
// Create report string
String reportString = lastName + "\t" + firstName + "\t" + mid1 + "\t" + mid2 + "\t" + finalExam + "\t" + grade + "\n";
// Write to report.txt using FileOutputStream
fos.write(reportString.getBytes());
// Update exam sum and count for average calculation
sumMid1 += mid1;
sumMid2 += mid2;
sumFinal += finalExam;
count++;
} catch (NumberFormatException e) {
System.out.println("Skipping invalid line with incorrect number format: " + line);
}
}
// Calculate and write the averages to the file
DecimalFormat df = new DecimalFormat("##.00");
String averageString = "Averages: Midterm1 " + df.format(sumMid1 / count) + ", Midterm2 " + df.format(sumMid2 / count) + ", Final " + df.format(sumFinal / count);
fos.write(averageString.getBytes());
} finally {
// Close the FileInputStream and FileOutputStream
if (fis != null) {
fis.close();
}
if (fos != null) {
fos.close();
}
}
}
}
Step by stepSolved in 4 steps with 4 images
- in java Write code that prints: firstNumber ... 2 1Print a newline after each number. Ex: If the input is: 3 the output is: 3 2 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 import java.util.Scanner; public class ForLoops { publicstaticvoidmain (String [] args) { intfirstNumber; inti; Scannerinput=newScanner(System.in); firstNumber=input.nextInt(); for () { System.out.println(i); } } }arrow_forwardMy java code keeps giving me back StringIndexOutOfBounds for the following code. Need help dealing with this Movie.java: import java.io.File;import java.io.FileNotFoundException;import java.util.ArrayList;import java.util.Scanner;public class Movie{public String name;public int year;public String genre;public static ArrayList<Movie> loadDatabase() throws FileNotFoundException {ArrayList<Movie> result=new ArrayList<>();File f=new File("db.txt");Scanner inputFile=new Scanner(f);while(inputFile.hasNext()){String name= inputFile.nextLine();int year=inputFile.nextInt();inputFile.nextLine();String genre= inputFile.nextLine();Movie m=new Movie(name, year, genre);//System.out.println(m);result.add(m);}return result;}public Movie(String name, int year, String genre){this.name=name;this.year=year;this.genre=genre;}public boolean equals(int year, String genre){return this.year==year&&this.genre.equals(genre);}public String toString(){return name+" ("+genre+") "+year;}}...arrow_forwardimport java.util.Scanner; public class CircleAndSphereWhileLoop{ public static final double MAX_RADIUS = 500.0; public static void main(String[] args) { Scanner in = new Scanner(System.in); // Step 2: Read a double value as radius using prompt // "Enter the radius (between 0.0 and 500.0, exclusive): " // Step 3: While the input radius is not in the ragne (0.0, 500.0) // Display a message on one line (ssuming input value -1) // "The input number -1.00 is out of range." // Read a double value as radius using the same promt double circumference = 2 * Math.PI * radius; double area = Math.PI * radius * radius; double surfaceArea = 4 * Math.PI * Math.pow(radius, 2); double volume = (4 / 3.0) * Math.PI * Math.pow(radius, 3); // Step 4: Display the radius, circle circumference, circle area, // sphere surface area, and...arrow_forward
- StringFun.java import java.util.Scanner; // Needed for the Scanner class 2 3 /** Add a class comment and @tags 4 5 */ 6 7 public class StringFun { /** * @param args not used 8 9 10 11 12 public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Please enter your first name: "); 13 14 15 16 17 18 System.out.print("Please enter your last name: "); 19 20 21 //Output the welcome message with name 22 23 24 //Output the length of the name 25 26 27 //Output the username 28 29 30 //Output the initials 31 32 33 //Find and output the first name with switched characters 34 //All Done! } } 35 36 37arrow_forwardimport java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.util.Scanner;public class romeoJuliet {public static void main(String[]args) throws Exception{Scanner keyboard=new Scanner(System.in);System.out.println("Enter a filename");String fN=keyboard.nextLine();int wLC=0, lC=0;String sW= keyboard.nextLine();File file=new File(fN);String [] wordSent=null;FileReader fO=new FileReader(fN);BufferedReader buff= new BufferedReader(fO);String sent;while((sent=buff.readLine())!=null){if(sent.contains(sW)){wLC++;}lC++;}System.out.println(fN+" has "+lC+" lines");System.out.println("Enter some text");System.out.println(wLC+" line(s) contain \""+sW+"\"");fO.close();}} Need to adjust code so that it reads all of the words that it was tasked to find. for some reason it isn't finding all of the word locations in a text. for example the first question is to find "the" in romeo and juliet which should have 1137, but it's only picking up 1032?arrow_forwardI need help with this java problem described below: import java.util.Scanner; import java.util.InputMismatchException; public class ExpirationMonth { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); int expirationMonth; boolean valueFound = false; while (!valueFound) { try { expirationMonth = scnr.nextInt(); valueFound = true; System.out.println("Expiration month: " + expirationMonth); System.out.println("Processed one valid input value"); } /* Your code goes here */ } } }arrow_forward
- I need help fixing this java program as shown below: import java.util.Scanner;import java.util.InputMismatchException; public class NameAgeChecker { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); String inputName; int age; inputName = scnr.next(); while (!inputName.equals("-1")) { // FIXME: The following line will throw an InputMismatchException. // Insert a try/catch statement to catch the exception. age = scnr.nextInt(); System.out.println(inputName + " " + (age + 1)); inputName = scnr.next(); } }}arrow_forwardI need help with this Java Problem as described in the image below: import java.util.Scanner; public class LabProgram { public static int fibonacci(int n) { /* Type your code here. */ } public static void main(String[] args) { Scanner scnr = new Scanner(System.in); int startNum; startNum = scnr.nextInt(); System.out.println("fibonacci(" + startNum + ") is " + fibonacci(startNum)); }}arrow_forwardHow do I fix the result? import java.lang.*;import java.util.Scanner; // import the Scanner class public class Main { public static Scanner keyboard = new Scanner(System.in); public static void main(String[] args) { boolean doItAgain = false;do{doItAgain = false;calcType();System.out.println("Do you want to start over? (Y/N)");//Scanner keyboard = new Scanner(System.in);String decision = keyboard.next();if(decision.equals("Y")) {doItAgain = true;}}while(doItAgain);System.out.println("Goodbye");} public static void calcType() {String mode;System.out.println("Enter the calculator mode: Standard/Scientific?");//Scanner keyboard = new Scanner(System.in);mode = keyboard.next();if(mode.equals("Standard")) {standard();}else if (mode.equals("Scientific")) {scientific();}elseSystem.out.println("Unknown calculator type: " + mode);} public static void standard() {System.out.println("The calculator will operate in standard mode.");boolean doItAgain;do{doItAgain = false;System.out.println("Enter...arrow_forward
- So this is my code but it give me error where did i do wrong? import java.util.Scanner;public class equationy {public static void main(String args[]){Scanner input=new Scanner(System.in);int m,x,c,y;System.out.println("Enter m");m=input.nextInt();System.out.println("Enter x");x=input.nextInt();System.out.println("Enter c");c=input.nextInt();y=m*x+c;System.out.printf("y = %d" , y);}}arrow_forwardPlease look at my JAVA code. I need the output to be a HOLLOW BOX consisting of * with the size being user defined. The box in my output however is solid. It should look something like this: * * * * ** ** ** ** * * * * import java.util.*;public class question5 { public static void main(String []args) { int size; Scanner kb = new Scanner(System.in); //user input System.out.println("Please enter a number: "); size = kb.nextInt(); //outer loop for(int i=0; i<size; i++) { //inner loop to size for(int j=0; j<size; j++) System.out.print("*"); System.out.print("\n"); } }}arrow_forward
- Text book imageDatabase System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationText book imageStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONText book imageDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- Text book imageC How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONText book imageDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningText book imageProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education