JAVA- I want to edit the following code, taking out the parts involving entering
ID: 3804369 • Letter: J
Question
JAVA- I want to edit the following code, taking out the parts involving entering the filename. Data will only need to be read in using Putty using
"java Prog6 < in1.dat/ in2.dat/in3.dat" and so on. There are three different files that are to be read in and printed out, so I am not able to hard code in one specific filename. In previous assignments, I have used reader.next() to pull the correct info from the files. Is there any way to change the lines about reading in a file name into "reader.next()"? I don't need any specific user input asking for a filename since I will only read in data through java Prog6 < in1.dat in Putty.
package parking;
import java.io.File;
import java.io.FileNotFoundException;
import java.text.DecimalFormat;
import java.util.Scanner;
public class Arrays {
public static void main(String args[])
{
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);
System.out.println("enter the names of files with space in between");
while(keyboard.hasNext())
{
String filename=keyboard.next();
Scanner in;
try {
in = new Scanner( new File(filename) );
int size =in.nextInt();
names=new String[size];
marks=new double[size][2];
for(int i=0;i {
names[i]=in.next();
marks[i][0]=in.nextDouble();
if(highestscore {
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 {
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])));
}
}
catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}
}
}
Explanation / Answer
package parking;
import java.io.File;
import java.io.FileNotFoundException;
import java.text.DecimalFormat;
import java.util.Scanner;
public class Arrays {
public static void main(String args[])
{
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);
System.out.println("enter the names of files with space in between");
while(keyboard.hasNext())
{
String filename=keyboard.next();
Scanner in;
try {
in = new Scanner( new File(filename) );
int size =in.nextInt();
names=new String[size];
marks=new double[size][2];
for(int i=0;i {
names[i]=in.next();
marks[i][0]=in.nextDouble();
if(highestscore {
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 {
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])));
}
}
catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.