The purpose of the program I've written is that it's supposed to load the input file "opcode.txt". My program works correctly when deleting the comment lines of the input file, but the professor disapproves of my editing his files. How can I extract the integers and strings needed without the comment lines interfering. Is there a specific delimiter I can use?
Presently, it gives the error "java.lang.NumberFormatException; For input string: "Accumulator" (in java.lang.NumberFormatException)"
My code:
import java.util.Scanner;
import java.io.File;
import java.io.PrintWriter;
import java.io.FileNotFoundException;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
public class vonNeumann
{
public static void main(String[] args) throws Exception
{
Scanner console = new Scanner(System.in);
JFileChooser chooser = new JFileChooser();
File inputFile = null;
String outputFileName = "";
File f = new File(new File(".").getCanonicalPath());
chooser.setCurrentDirectory(f);
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"Text Files", "txt");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
inputFile = chooser.getSelectedFile();
}
else
return;
returnVal = chooser.showSaveDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
outputFileName = chooser.getSelectedFile().getName();
}
else
return;
Scanner in = new Scanner(inputFile);
PrintWriter out = new PrintWriter(outputFileName);
int acc = 0; //accumulator
int m1 = 0;
int m2 = 0;
int m3 = 0;
String x = ""; //first input
int y = 0; //second input
String z = "";
while (in.hasNext())
{
x = in.next();
z = in.next();
y = Integer.parseInt(z);
And the input file "opcode.txt"
01 07 //load 7 to Accumulator
21 91 //store the Accumulator to M1
05 13 //add 13 to 7 and keep 20 in the Accumulator
99 99 //print the contents of the Accumulator
06 12 //subtract 12 from 20 and keep 8 in the Accumulator
21 91 //store 8 to M1
99 91 //print the contents of M1
01 19
05 13
0B 03
21 92
99 92
01 00 //Clear the Accumulator
06 19
0B 11
0C 05
99 99 //Print the Accumulator
05 31
05 12
06 11
0C 07
0B 11
06 71
99 99
01 00
05 07
05 09
0B 16
0B 03
0C 07
99 99
21 91
99 91
99 92
01 00
21 91
21 92
99 91
99 92
I can't seem to get it to ignore everything after //
Aucun commentaire:
Enregistrer un commentaire