1 Replies - 126 Views - Last Post: 07 February 2012 - 09:58 PM Rate Topic: -----

Topic Sponsor:

#1 monster92  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 10-May 11

Repeating Two Words Then Repeating Them Back

Posted 07 February 2012 - 09:37 PM

So I'm requesting for a string. When the person repeats the string I want to repeat it back for example,

Bob
Bob
Repeated: Bob

I also would like it so it would do it a third time

Bob
Bob
Repeated: Bob
Bob
Repeated: Bob

I've been trying to implement this however the output send something different from what I am expecting. For example,

Jim
Jim
RepeatedJim
Jim
RepeatedJim
Bob
RepeatedJim

How on earth do I implement the correct logic? Cheers for your help.






import java.util.Scanner;

public class Repeated {

    public static void main(String[] args) {

        String S1 = "noData";
        String S2 = "noData";

        Scanner userinput = new Scanner(System.in);

        System.out.println("Please enter lines of text: ");


        while (userinput.hasNextLine()) {

            S1 = userinput.nextLine();


            if (S1.contentEquals(S2)) {
                System.out.println("Repeated" + S1);
            }

            S2 = userinput.nextLine();


            if (S1.contentEquals(S2));
            {
                System.out.println("Repeated" + S1);

            } 
            
           if (S1.isEmpty() || S2.isEmpty())
           {
               System.out.println("Bye");
               System.exit(0);
           }

            }
        }
    }



Is This A Good Question/Topic? 0
  • +

Replies To: Repeating Two Words Then Repeating Them Back

#2 Sheph  Icon User is online

  • D.I.C Addict
  • member icon

Reputation: 294
  • View blog
  • Posts: 777
  • Joined: 12-October 11

Re: Repeating Two Words Then Repeating Them Back

Posted 07 February 2012 - 09:58 PM

Your logic is a little off. You get 2 user inputs per iteration. I suggest cutting that down to 1 user input per iteration

while true:
-get user input
-if it has already been entered (In the list of values), say it has been repeated.
-else, add it to a list of values already entered
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1