Concept explainers
What is missing from the following code, which attempts to open a file and read an integer?
Want to see the full answer?
Check out a sample textbook solutionChapter 2 Solutions
Absolute Java (6th Edition)
Additional Engineering Textbook Solutions
Starting Out With Visual Basic (7th Edition)
Web Development and Design Foundations with HTML5 (9th Edition) (What's New in Computer Science)
Starting Out with C++: Early Objects
Starting out with Visual C# (4th Edition)
Concepts Of Programming Languages
- import java.util.Scanner;import java.io.File;import java.io.IOException;public class test2{public static void main(String [] args)throws IOException{File database = new File ("flights.txt");Scanner fileReader = new Scanner( database );Scanner input = new Scanner(System.in);// Displays the welcome screenint menuCheck = 0;String city = "";float oneWayCost = 0f;float roundTripCost = 0f;int numberOfSeats = 0;float total = 0f;while (menuCheck == 0){System.out.println("Welcome TO MEjia AIRLINES.");System.out.println("Please select a choice below [1-5]");System.out.println(" 1. Add flight");System.out.println(" 2. View trip");System.out.println(" 3. Manage Trip");System.out.println(" 4. Checkout");System.out.println(" 5. Exit Mejia airlines");int userInput = input.nextInt();if (userInput == 5){System.out.println("Thank You for using Mejia airlines");System.out.println("Stay safe! Stay Hrydrated! Happy Coding!");menuCheck = -5;}else if (userInput == 1){menuCheck = 1;}else if (userInput ==…arrow_forwardPlease verify that the file was created along with the content package pwriter2; import java.util.Scanner; import java.io.*; public class Pwriter2 { public static void main(String[] args) throws IOException { PrintWriter tofile2 = new PrintWriter( "tooutfile2.txt" ); int i = 0; while (i < 10){ tofile2.println( i ); i = i + 1; } tofile2.close(); } }arrow_forward*Java* Question: Is my code correct? Please answer if it’s correct or not and why? ———————————————- *CODE* import java.util.Scanner; import java.io.*; public class Assignment3{ public static void main(String[] args){ PrintWriter output = null; try{ Scanner input = new Scanner(new File("students.txt")); output = new PrintWriter("Result.txt"); while(input.hasNextLine()){ String fName = input.next(); String lName = input.next(); double test1 = input.nextDouble(); double test2 = input.nextDouble(); double finalTest = input.nextDouble(); double total = test1*0.2+test2*0.2+finalTest*0.6; if(total>=60) output.println(fName+" "+lName+" "+total+" PASS"); } output.close(); } catch(FileNotFoundException e){ System.out.println("Error: "+e.getMessage()); } } }arrow_forward
- import 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_forwardYou are required to implement a preprocessor in Java. Your preprocessor should be able to perform the following task:Identifying loops and methods Example of output: import java.util.Scanner public class String Scanner new Scanner(System.in) System.out.print.ln nextLine voidarrow_forwardimport java.util.*;public class TestAverage {public static void main(String[] args) {int test1, test2, test3, sum;Scanner reader = new Scanner(System.in);System.out.println("Enter test 1");test1 = reader.nextInt();System.out.println("Enter test 2");test2 = reader.nextInt();System.out.println("Enter test 3");test3 = reader.nextInt();sum = test1 + test2 + test3;System.out.println("The sum is " + sum);System.out.println("The average is " + sum / 3);}}a. Briefly explain what this code does.b. There is one logic error in this code explain what is wrong and how to fix it.arrow_forward
- Modify the code so that it writes the contents of String[] into a file named fileLoop1.txt CODE import java.util.Scanner; import java.io.*; public class Stringio { public static void main(String[] args) throws IOException { File loopfile = new File("fileLoop.txt"); Scanner getAll = new Scanner( loopfile ); String num; String[] items = new String[10]; int i = 0; while(getAll.hasNextLine()){ num = getAll.nextLine(); // square = num * num ; //System.out.println("The square of " + num + " is " + square); items[i] = num; System.out.println("items[i] is " + items[i]); i = i + 1; } getAll.close(); } }arrow_forwardI need help fixing this java program below: import java.util.Scanner;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.PrintWriter;import java.io.IOException; public class LabProgram { public static void main(String[] args) throws IOException { Scanner scnr = new Scanner(System.in); String fileName; String[] lastNames, firstNames; int[] midterm1, midterm2, finalScores; // Read a file name from the user and read the tsv file fileName = scnr.next(); try (Scanner fileScanner = new Scanner(new FileInputStream(fileName))) { int numStudents = Integer.parseInt(fileScanner.nextLine()); lastNames = new String[numStudents]; firstNames = new String[numStudents]; midterm1 = new int[numStudents]; midterm2 = new int[numStudents]; finalScores = new int[numStudents]; for (int i = 0; i < numStudents; i++) { lastNames[i] =…arrow_forwardQuestion 1 A programmer creates the Java class shown on the facing page, representing a numeric dataset read from a file. The class has a constructor that creates an object containing the data from the file, given the name of that file as a string. It also has a method called mean that returns the arithmetic mean value of the stored data. The questions that follow concern this class. You may include small fragments of code in your answers if appropriate, but you should not reimplement the class. Keep in mind that full marks can be obtained with purely descriptive answers. (a) The class as shown here will not compile. The compiler identifies two issues that need to be fixed, one occurring in the constructor and the other within the mean method. Explain what the issues are and describe how you would fix them. (b) Another programmer who is reviewing the code describes line 6 as 'dangerous'. What possible reason might there be for this comment, and how could you modify the code to fix the…arrow_forward
- ExampleTwo This program loops through fileLoop.txt, computation, and display results package charioloop; import java.util.Scanner; import java.io.*; // public class Charioloop { public static void main(String[] args) throws IOException { //Create a reference to the physical file File loopfile = new File("fileLoop.txt"); // connect a Scanner to the file Scanner getAll = new Scanner( loopfile ); int num = 0, square = 0; while(num != -1){ num = getAll.nextInt(); square = num * num ; System.out.println("The square of " + num + " is " + square); } getAll.close(); } } /* Create fileLoop.txt in I:\\Ajava\161\WPPractice\IO\charstream\charioloop My fileLoop.txt looks like this: 2 3\ 2 10 -1 Note:- Study this program and follow the given instruction and execute type the code and provide also output for this java program as soon as possible.arrow_forwardPlease help import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.Scanner; public class Main { static public double calcTax(double salary, double taxRate) { double taxOwed = 0; taxOwed = (salary * taxRate) / 100; return taxOwed; } public static void main(String[] args) throws IOException { ArrayList salary = new ArrayList(); ArrayList name = new ArrayList(); ArrayList company_name = new ArrayList(); // int lines = 1; try{ File myFile = new File("taxpayer.txt"); Scanner fileReader = new Scanner(myFile); while(fileReader.hasNextLine()){ String data = fileReader.nextLine(); System.out.println(data); String[] dataArr = data.split(", "); name.add(dataArr[0] + " "+ dataArr[1] );…arrow_forwardhow can i make the data exported to a file import java.util.Scanner;public class BMI { public static void main(String[] args) {Scanner scanner = new Scanner(System.in);String first_name;int kg;double height;double bmi; for(int i=1; i<4; i++) {System.out.print("NAME ");first_name=scanner.next();System.out.print("WEIGHT, kg");kg=scanner.nextInt();System.out.print("HEIGHT, m");height=scanner.nextDouble();bmi= kg / (height * height);System.out.printf("%s %20s %20s %20s", "NAME", "Weight", "Height", "BMI");System.out.println();System.out.printf("%-10s %12d %21.2f %22.2f",first_name,kg,height,bmi);System.out.println();} }}arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education