This question already has an answer here:
- How do I compare strings in Java? 23 answers
The file that I am reading in is a single line of text that reads: M=1;JMP
I know the three variables (computation, destination, and jump) are getting the correct values, as on the line before the if statements I print them out. Even so, the if statements are not triggering. Any help would be great.
class test7
{
public static void main(String [] args)
throws IOException
{
Scanner file=new Scanner(new FileReader("assembly.txt"));
String line= file.nextLine();
String computation="";
String destination="";
String jump="";
int firstPosition=0;
int secondPosition=0;
firstPosition=(line.indexOf('='));
secondPosition=(line.indexOf(';'));
computation=line.substring(firstPosition+1, secondPosition);
destination=line.substring(0,firstPosition);
jump=line.substring(secondPosition+1);
System.out.println(computation+" "+destination+" "+jump+"\n");
if (computation=="1") System.out.println("has computation");
if(destination=="M") System.out.println("has destination");
if(jump=="JMP") System.out.println("has jump");
}
}
Aucun commentaire:
Enregistrer un commentaire