0

I'm working on a Von Neumann Machine, I'm writing a java program that works like a simple machine, that follows instructions. It reads a textfile and depending on the OpCode in the file, it either stores, adds, substract, multilply, divide and load numbers. Should I use a switch statement or alot of if else statements?

  • 01 07 // load 7 to Accumulator
  • 21 91 // store Accumulator M1
  • 05 13 // add 13 to 7 and keep 20 in the Acc.
  • 99 99 // print Acc. contents
  • 06 12 // subtract 12 from Acc.
  • 21 91 // store 8 to M1

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

    {

      File file = new File("U:/op.txt");
      Scanner readfile = new Scanner(file);
    
        while(readfile.hasNextInt()){
          String read = readfile.nextLine();
    
          System.out.println(read);
        }
    
    
    }
    
daman
  • 5
  • 3
  • 1
    I think it's very opinion based. According to http://stackoverflow.com/questions/445067/if-vs-switch-speed , a switch is usually more efficient. – S.Klumpers Feb 08 '16 at 18:43
  • How to I get the second number in the file and instead of the first one (01 7) I want to store 7 to a variable called Acc instead of 01? @S.Klumpers `switch(read) { case 01: Acc = M1 + read; System.out.println(Acc); break; case 21: break; case 05: case 99: case 06: }` – daman Feb 08 '16 at 19:02

1 Answers1

0

Use this:

import java.io.*;
import java.util.Scanner;

public class Machine{
    public static void main(String[] args) throws FileNotFoundException
    {
        File file = new File("U:/op.txt");
        Scanner readfile = new Scanner(file);

        int acc = 0;
        int M1 = 0;

        while(readfile.hasNextInt())
        {
            String read = readfile.nextLine();

            switch(read.substring(0, 2))
            {
                case "01":
                    acc = M1 + Integer.parseInt(read.substring(3, 5));
                    System.out.println(acc);
                break;
            }
        }
    }
}

The read.substring(0, 2) gets the first two characters. The read.substring(3, 5) gets the other two and Integer.parseInt(intString) gets the integer value of these two characters.

This can be reused for all your examples by adding in the other cases in the switch.

S.Klumpers
  • 410
  • 3
  • 14
  • I tried that before it kept giving me "incompatible types - found java.lang.String but expexted int" @S.Klumpers – daman Feb 08 '16 at 19:47
  • Can you determine at which line the error occurs or attach the full code? – S.Klumpers Feb 08 '16 at 19:56
  • `File file = new File("U:/op.txt"); Scanner readfile = new Scanner(file); while(readfile.hasNextLine()){ String read = readfile.nextLine(); switch(read.substring(0,2)) { case "01": acc = m1 + Integer.parseInt(read.substring(3, 5)); System.out.println(acc); break; case "21": acc = m1; break; case "05": acc = m1 + Integer.parseInt(read.substring(3, 5)); break; }` – daman Feb 08 '16 at 20:01
  • how do i post the code correctly i used ``and it doesn't post correctly. Anyways it gives an error at the switch statement. I put the switch statement in the while loop. – daman Feb 08 '16 at 20:03
  • I edited it until it compiled for me, I can't run it though, it gives me some sort of exception about not being able to find the file. – S.Klumpers Feb 08 '16 at 20:21