0

I am stuck in this program that is string method, my issue is that I cannot get the loop to stop and the program to print the output that is currently stored after the keyword has been entered. I am not trying to compare strings, I am trying to input multiple strings and add a word, in this case, "not" to the strings until the word "stop" is entered. Once "stop" has been entered. the system will output the entire string stored.

Here is the question for the program: (StringConcat.java) This program asks the user to repeatedly enter a String. It ,should concatenate those Strings together, but insert spaces and the word “not” between every pair of words the user enters. Stop when the user enters the String “stop”. Display the final String. For instance, the program output might look like:

Please enter some Strings: "Such" "eyes" "you" "have" "stop"

"Such not eyes not you not have"

Here is my code so far:

import java.util.*;
public class StringConcat{

  public static void main(String [] args){

  Scanner sc = new Scanner(System.in);
  String s = new String();
     System.out.print("Please enter some Strings: ");
  for(int x=0; x<s.length(); x++){
     s = sc.nextLine();
     s = s + "not ";
     if(s == "stop"){
     System.out.println(s);
     break;
     }
     else{
     continue;
     }
   }
  }
}
Kevin Bebla
  • 23
  • 1
  • 1
  • 5
  • 3
    Possible duplicate of [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Turing85 Mar 02 '19 at 22:32

2 Answers2

0

Use while loop.

Why while loop?

Usually we have to use while loops always when we don't know the number of loops we will do. In this case only when the user inputs "stop".

So you need a String field to hold the user words. Also we can use a number field to track if is the first or the second word, thinkg in append the "not" word.

Then, take a look in this example:

Scanner s = new Scanner(System.in);
String currentAnswer = "";
String userWords = "";
int tracker = 0;

while (!currentAnswer.equals("stop")){
     currentAnswer = s.nextLine();
     userWords += currentAnswer + " ";

     if (tracker % 2 != 0) {
         userWords += "not ";
     }

     tracker++;
}

System.put.println(userWords);

This can be done using for loop too but I really recommend the while loop to this case.

EDIT: As you saw, I used equals() instead == to compare two Strings because we are wiling to check for its value, not for its object equality.

When we use == operator we are trying to check if two objects target to the same memory adress, but we only want to know if two Strings have the same value.

For this case is valid to know that we can compare it using other ways, such as Objects.equals() or even contentEquals().

Check this discussion to learn more about comparing strings.

Lucas Sousa
  • 192
  • 3
  • 14
0

Several issues with your code:
(1) Why do you use a for loop and iterate up to s.length() when the length of s (which is 0 at that point) has nothing to do with your problem?
You need a loop which has not predefined number of iterations like a while (true) from which you will exit with a break.
(2) In each iteration you get the user's input and store it in s, so you lose all previous values.
You need a separate variable to store the user's input.
(3) The continue statement is not needed as the last statement in a loop.
(4) Because at each iteration you append " not " at the end, after the loop has finished you must delete that last " not " from s
(5) Don't use == when you compare strings. There is the method equals() for this.
This is my solution:

Scanner sc = new Scanner(System.in);
String s = "";
System.out.print("Please enter some Strings: ");
while (true){
    String input = sc.nextLine();
    if(input.equalsIgnoreCase("stop"))
        break;
    s += input + " not ";
}
if (s.length() >= 5)
    s = s.substring(0, s.length() - 5);
System.out.println(s);
forpas
  • 160,666
  • 10
  • 38
  • 76