1. Engineering
  2. Computer Science
  3. my code has a semantic error i expect to receive...

Question: my code has a semantic error i expect to receive...

Question details

my code has a semantic error I expect to receive this output:

Typical expected output: Enter first phrase replays Enter second phrase parsley aelprsy are the letters of replays in order aelprsy are the letters of parsley in order replays is an anagram of parsley

But my output is

Enter furst phrase replays Enter second phrase parsley aelprsy are the letters of replays in order aelprsy are the letters of parsley in order

My java code is:

import java.util.Scanner;
public class Anagram{
public static void main(String[] args){
  
Scanner scan = new Scanner(System.in);
System.out.print("Enter first phrase");
String phrase_1 = scan.nextLine();
Scanner scan2 = new Scanner(System.in);
System.out.println("Enter second phrase");
String phrase_2 = scan.nextLine();
phrase_2 = phrase_2.toLowerCase();
String new_phrase_1 = "";
String new_phrase_2 = "";
for(char chr = 'a';chr<='z';chr++){
int position=0;
while(phrase_1.indexOf(chr,position) != -1){
position = phrase_1.indexOf(chr,position);
position++;
new_phrase_1 += chr;
}position=0;
while(phrase_2.indexOf(chr,position) != -1){
position = phrase_2.indexOf(chr,position);
position++;
new_phrase_2 += chr;
}
  
}
System.out.println(new_phrase_1 + " are the letters of " + phrase_1 + " in order");
System.out.println(new_phrase_2 + " are the letters of " + phrase_2 + " in order");
if (new_phrase_1 == new_phrase_2){
System.out.println(phrase_1 + " is an anagram of "+ phrase_2);
}
}
}

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution