JAVA- How would I edit this code as minimally as possible to take out the file n
ID: 3804743 • Letter: J
Question
JAVA- How would I edit this code as minimally as possible to take out the file name that needs to be read in and replacing it with something like reader.next()? Specifically, I want to take out the line:
try {
in = new Scanner( new File(filename) );
I need to read in numbers from various .dat files and come up with an ouput that looks similar to the following:
When I run the following code in Putty while reading in a .dat file, I get the output:
5 (No such file or directory)
Johnson (No such file or directory)
93.5 (No such file or directory)
Dirks (No such file or directory)
76.0 (No such file or directory)
Timm (No such file or directory)
87.0 (No such file or directory)
Faulk (No such file or directory)
55.5 (No such file or directory)
Miser (No such file or directory)
69.0 (No such file or directory)
CODE-----
import java.text.DecimalFormat;
import java.util.Scanner;
public class Prog6 {
public static void main(String args[]) {
Scanner reader = new scanner(System.in);
double highestscore=0;
double offset=0;
String[] names;
double[][] marks;// holds raw marks in first column and curve marks in second column
Scanner keyboard=new Scanner(System.in);
while(keyboard.hasNext()){
String filename=keyboard.next();
Scanner in;
int size =in.nextInt();
names=new String[size];
marks=new double[size][2];
for(int i=0;i<size;i++)
{
names[i]=in.next();
marks[i][0]=in.nextDouble();
if(highestscore<marks[i][0])
{
highestscore=marks[i][0];
}
offset=100-highestscore;
System.out.println("Student Names Raw score With Curve");
DecimalFormat df = new DecimalFormat(".##");
for(int i=0;i<size;i++)//assigning with curve marks and printing simultaneously
{
marks[i][1]=marks[i][0]+offset;
System.out.println(String.format("%s %s %s",names[i],marks[i][0],df.format(marks[i][1])));
}
}
}
}
}
Explanation / Answer
import java.io.File;
import java.io.FileNotFoundException;
import java.text.DecimalFormat;
import java.util.Scanner;
/**
*
* @author Sam
*/
public class Prog6 {
public static void main(String args[]) throws FileNotFoundException {
// Scanner reader = new Scanner(System.in); // <- useless line
double highestscore = 0;
double offset = 0;
String[] names;
double[][] marks;// holds raw marks in first column and curve marks in second column
Scanner keyboard = new Scanner(System.in);
while (keyboard.hasNext()) {
String filename = keyboard.next(); //<<- you had not used the filename. How did you open the file?
Scanner in = new Scanner(new File(filename)); //you need to link your file name like this
int size = in.nextInt();
names = new String[size];
marks = new double[size][2];
for (int i = 0; i < size; i++) {
names[i] = in.next();
marks[i][0] = in.nextDouble();
if (highestscore < marks[i][0]) {
highestscore = marks[i][0];
}
offset = 100 - highestscore;
} // 1st for loop should end here.
System.out.println("Student Names Raw score With Curve");
DecimalFormat df = new DecimalFormat(".##");
// next for loop is not nested, but independent
for (int j = 0; j < size; j++)//assigning with curve marks and printing simultaneously
{
marks[j][1] = marks[j][0] + offset;
System.out.println(String.format("%s %s %s", names[j], marks[j][0], df.format(marks[j][1])));
}
}
}
}
Here you go. You can read the comments to understand what problems you had in the code earlier. In case you don't understand the code, please comment back in the comment section.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.