Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Java programming algorithm?

Below is a program that I wrote but I kind of don;t get one thing. I put a while loop in the beginning, and pretty much it says that while response's first character is y, then the program will run. I don;t quite understand then why the program initially runs, if the whole condition is WHILE response's first character is y, THEN the program will run. I am fairly new at this so be kind, thank you!

import java.util.Scanner;

public class methods {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

String response = "yes";

while(response.charAt(0)=='y'){

System.out.println("Enter password:");

String answer;

answer = input.nextLine();

System.out.println("That's" + " " + answer.equalsIgnoreCase("Noah") + ",");

System.out.println("do you wish to try again?");

response = input.nextLine();

}

}

}

1 Answer

Relevance
  • 7 years ago
    Favorite Answer

    I do not know java, but I think I can help you.

    You have String response="yes"; So the first character of Response is set to 'y'.

    while (response.charAt(0)=='y' must check the first character or response, which is 'y'. So you will enter the loop.

    At the end of the loop, you ask for a new value if response. If the first character is 'y', it will loop, otherwise the loop will end.

Still have questions? Get your answers by asking now.