Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

change wrong codes by looking at the comments package part1; import java.io.File

ID: 3922943 • Letter: C

Question

change wrong codes by looking at the comments

package part1;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;

public class FileRead {

   public static void main(String[] args) throws FileNotFoundException {
       // create a file scanner that reads from "part1.txt"
       Scanner fs = new Scanner("part1");
      
       // declare variable line1 as a float
       float line1;
       // declare variable line2 as a String
       String line2;
       // declare variable line3 as a String
       String line3;
       // declare variable liner as an integer
       int line4;
  
       // read in a float for line1
       line1 = fs.next();
       // read in a string for line2
       line2 = fs.next();
       // read in a string for line3
       line3 = fs.nextInt();
       // read in an integer for line4
       line4 = fs.next();
  
       // close the file scanner
       Scanner.close();
      
       // print the content of the file as one sentence
       System.out.println(line1+" "+line2+" "+line3+" "+line4);
   }

}

Part 1: Debugging the file scanner code Import the Lab08 project into eclipse. Debug the code in part 1 to get it to work. There are 5 errors in the code. When you code is working the following should print to the console: "4.5 truncated is 4"

Explanation / Answer

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;

import java.io.File;


class FileRead {
public static void main(String[] args) throws FileNotFoundException {

File file=new File("part1.txt");
// create a file scanner that reads from "part1.txt"
Scanner fs = new Scanner(file); //or use Scanner fs =new Scanner("part1.txt");
  
// declare variable line1 as a float
float line1;
// declare variable line2 as a String
String line2;
// declare variable line3 as a String
String line3;
// declare variable liner as an integer
int line4;
  
// read in a float for line1
line1 = fs.nextFloat();
// read in a string for line2
line2 = fs.next();
// read in a string for line3
line3 = fs.next();
// read in an integer for line4
line4 = fs.nextInt();
  
// close the file scanner
fs.close();
  
// print the content of the file as one sentence
System.out.println(line1+" "+line2+" "+line3+" "+line4);
}
}

part1.txt

4.5 truncated is 4

Output:

Success time: 0.08 memory: 711680 signal:0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote