-1

I am in my first semester of college learning Java. This will be probably a pretty basic loop for you all but I am truly stuck. Now please I am taking suggestions/help but I do not want this code completed for me. I want to learn this code. Thank you!

Context: Write a routine that will read words from the keyboard until the word “done” is entered. For each word except “done”, report only if its first character is equal to its last character.

MY WHILE Loop Code:

while (word != "done") // Test Phase
 {

   if (word != "done")
   {
     System.out.println("End While Loop Version");
     break;
   }
   else
   {
     System.out.println("Next Word: " + word);
     System.out.println("Char " + first + " appears at the start and end of " + word);
   }

   count++;
 }

Now I know the break; is messing up my console output. But I do not know how else to stop the program OR move onto the else condition. I also have to use a simple While Loop. (I have to do all 3. Done with for.)

Nithin Kumar Biliya
  • 2,763
  • 3
  • 34
  • 54
  • 1
    We do not need more posts explaining the use of `==` on Strings. Instead of posting an answer, please just vote on the existing dupe. – Carcigenicate Sep 22 '19 at 20:18
  • I guess you didn't want to write `if (word != "done")` but `if (word == "done")`. After considering this semantic error, you should change to `if (word.equals ("done")))` as suggested by others. Then there is no new word read inside the loop. I would suggest using the Scanner class too, but not all other answers look right in that one suggest nextLine, while you shall read words (sc.next()). We don't see, how you get your initial word. The test for first/last char is missing, too. – user unknown Dec 17 '20 at 02:07

2 Answers2

0

Your honest is appreciated, as you are new to the StackOverflow, we can do something about this.

import java.util.Scanner;

public class WhileLoopUntilDone {
    // this would help you take user input
    private static final Scanner SCANNER = new Scanner(System.in);

    public static void main(String[] args) {

        String userInputText = null;
        // do while loop helps you take input atleast once before checking the 'userInputText'
        do{
            userInputText =  SCANNER.next(); 
        }while (!userInputText.contentEquals("done")); // if this condition is false, it would break the do-while loop
    }
}
  • Please try to check every error you get on Google (StackOverflow)
  • try to consider a tight feedback loop to your coding, as you get them you would pick up any mistakes as quickly as possible.
  • If you are new to java consider few tutorials (Practical Programming).
  • Java is a Typed language and good place to start if you are learning programming
  • Do little homework before asking a question, it would be rewarding in Stack-overflow,
  • Please help others when you can.
user unknown
  • 35,537
  • 11
  • 75
  • 121
Pankaj Nimgade
  • 4,529
  • 3
  • 20
  • 30
0

first of all remember: In java "==" and "!=" is use to check if 2 variables are pointing to same object. You should use .equals() method if you want to compare 2 objects.

Anyway, let's see you problem :D

You are using if-else construct but you need to read from keyboard UNTIL you don't get "done" so it's good idea use something like loop.

You can use one of 3 loop instructions present in java.

  • While loop
  • do-while loop
  • for/forEach loop

Let's see how your code looks like with first one:

Scanner scanner = new Scanner(System.in);

System.out.println("Insert your string: ");
String input = scanner.nextLine();
while(!input.equals("done")) {
    if(input.toCharArray()[] == input.toCharArray[input.length()-1])
        System.out.println("First character is equals to last character");
    System.out.println("Insert your string: ");
    input = scanner.nextLine();
}

now with do-while loop:

Scanner scanner = new Scanner(System.in);
do {
    String input = scanner.nextLine();
    if(input.toCharArray()[] == input.toCharArray[input.length()-1])
        System.out.println("First character is equals to last character");
} while(!input.equals("done"))

You can try to code same algorithm with for-loop :D

Luke
  • 516
  • 2
  • 10