When you enter "y" and try again. it doesn't asks you to "enter a String: " and "enter another String: " again.
It just prints those lines out but you don't get to enter.
Well~ you can enter 1 string again when it does the loop but I don't know if it's assigned in str1 or str2.
It's supposed to do the same thing as the first time;
System.out.println("Enter a string");
str1 = keyboard.nextLine(); //and
System.out.println("Enter a string");
str2 = keyboard.nextLine();
THIS IS HOW IT LOOKS IN THE COMMAND PROMPT WINDOW:
Enter a string:
Hello
Enter another string:
Hola
str1: Hello
str2: Hola
Hello < Hola
Hello and Hola are not the same.
Do you want to try again? y/n
y
Enter a string:
Enter another string:
Computer
str1:
str2: Computer
< Computer
and Computer are not the same.
Do you want to try again? y/n
/* This code uses equals and compareto method to compare 2 strings and asks you if you want to try again*/
import java.io.*;
import java.util.*;
public class comparingStrings
{
static Scanner keyboard = new Scanner(System.in);
static String str1, str2;
static String yesorno;
public static void main (String[]args)
{
do{
getData();
}while (yesorno.equals("y"));
}
public static void getData()
{
getStringValues();
compareStrings();
equalsStrings();
doYouWantTo();
}
public static void getStringValues()
{
System.out.println("Enter a string:" );
str1 = keyboard.nextLine();
System.out.println("Enter another string: ");
str2 = keyboard.nextLine();
System.out.println( "str1: " + str1 + "\n str2: " + str2 );
}
public static void compareStrings()
{ ;
if(str1.compareToIgnoreCase(str2) == 0)
{System.out.println( "Both Strings are equal"); }
if (str1.compareToIgnoreCase(str2) > 0)
{System.out.println( str1 + " > " + str2 );}
if (str1.compareToIgnoreCase(str2) < 0)
{System.out.println (str1 + " < " + str2 );}
}
public static void equalsStrings()
{
if(str1.equalsIgnoreCase(str2) == true )
{System.out.println(str1 + " and " + str2 + " are the same."); }
if (str1.equalsIgnoreCase(str2) == false)
{System.out.println(str1 + " and " + str2 + " are not the same." );}
}
public static void doYouWantTo()
{
System.out.println("Do you want to try again? y/n ");
yesorno = keyboard.next();
}
}