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();
        }

}

Edited 4 Years Ago by Johannady2

I don't know if it's assigned in str1 or str2.

Add some printlns to print out the values of those two variables after they are read in so you can see what their values are. Be sure to put ID strings with the println so you know what variable was printed:
System.out.println("str1="+str1+"<");

Also it would help if you copied the full contents of the console from when you test the program and show what the problem is.

To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.

Edited 4 Years Ago by NormR1

Comments
in commant 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

in commant 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

Looks like you are having a problem with the Scanner class. Scanner methods can be tricky. The Scanner buffers input and can block waiting for input.

For example if you enter: A word to the wise <PRESS ENTER>
and use next() only "A" is read, and "word to the wise" is left in the buffer.
Your next attempt to get something from Scanner will be to get "word".
nextInt() will fail here.
To clear the buffer, use the nextLine() method.
To test if the next token is an int, use the hasNextInt() method.

After calling next() (which will leave a newline char in the buffer) you need to call nextLine() to clear out the buffer.

Comments
WOW! COOL! I just changed next(); to nextLine(): and it works now! THANKS ^^

My impression was that this is caused by the difference between next() and nextLine(). When you read the "y", you also read a newline token with it. This stays in the buffer and is assigned when you then call nextLine() afterwards. So str1 becomes a special character (like System.NewLine) that you cannot see.

I found a similar example by someone else and that was the solution he was given: Just add another nextLine() to clear the buffer after reading the "y" and you should be good to go.

Edit: Got beaten by a minute.

Edited 4 Years Ago by Cebraio: wrong language

Thanks A LOt Your Exmple It's Very Usefully To Me Write in A code In My Programme...
Thanks Again Bye.......

This question has already been answered. Start a new discussion instead.