Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I am making a text based game, and my code works fine until this line when i type in "why" it doesn't print "test".
System.out.println( "Fallout: Master's dialogue");
System.out.println(" ");
System.out.println( " So, what shall it be? Do you join the Unity or do you die here? Join! Die! Join! Die! ");
System.out.print( "> ");
Go = keyboard.nextLine();
if (Go.equalsIgnoreCase("join"))
{
System.out.println("Excellent. Your talents will be useful. But first you must tell me everything about your vault.");}
System.out.print("> ");
Look = keyboard.nextLine();
if (Go.equalsIgnoreCase("why"))
{System.out.println("TEST");}
System.out.print("> ");
Look = keyboard.nextLine();
}
I assume Go and Look are string variables.
At the start of the code, you read the user input and stored it in Go:
Go = keyboard.nextLine();
And then you checked whether Go is join:
if (Go.equalsIgnoreCase("join"))
This is all fine and good.
Then, you read user input a second time, and stored it in Look:
Look = keyboard.nextLine();
But you incorrectly checked Go instead:
if (Go.equalsIgnoreCase("why"))
Go has not been changed. Go is still "join", so the condition is never true.
You should check Look instead:
if (Look.equalsIgnoreCase("why"))
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
What is the chronology of executing lines inside a method in Kotlin?
fun main()
{
testExecutionOrder()
}
fun testExecutionOrder()
{
for (item in 1..2)
{
print("hey, how are you?")
loopingInfinitely()
}
}
fun loopingInfinitely()
{
while(true)
{
print("I am fine.")
}
}
I am expecting it to give output like;
//hey, how are you? I am fine. I am fine....(infinitely)
But the output is something like following;
// am fine. I am fine. I am fine. I am fine. I am fine. (Infinitely)
Why is it excluding 'I' while printing the first time and why is it not printing "hey, how are you?" at all.
Here is the original console output:
well, your output is in fact like you are expecting, starts with "hey, how are you?", then printing "I am fine." again and again
there is a limit of capacity in log and you are printing "im fine" so often (while(true)) that text exceeds text size limit in split second and beggining of printed text ("how are you?") get lost
if you are using Android framework then you should use logcat and Log.i() method. you will notice that your code is printing new "im fine" line probably multiple times in one milisecond, thats too much text for showing in console/logcat window
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
This might be a bit of a stupid question, but I can't figure this out! I'm working on a minigame that involves one player being randomly selected as the terminator, and all the rest as weaklings. I have an ArrayList of players, and I can get it to choose the terminator, and then I need it to copy players into weaklings, but skip terminator. The code I'm using doesn't copy anything at all into weaklings. Here's the code I'm using, could someone a bit more experienced help me out please:
for (int i = 0; i < players.size(); i++ ) {
if (!players.get(i).contentEquals(terminator)) {
players.add(players.get(i));
}
}
System.out.print("The other players are: " + weaklings + ".");
}
You add to the players again: players.add, instead of weaklings.add.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I'm setting up this do- while loop . Where do i need to correct this code
so when 000000 is given , the loop ends.
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
String am ;
{
do
{ System.out.println("give am number");
am = kb.next();
if (am.matches("[0-9]+") && am.length() <= 6)
{System.out.println("am = "+am);
{break;}}
else
{System.out.println("wrong try again");
am = kb.next();
}
} while(!"000000".equals(am));
Right now, you are checking for equality, not matching. "000000" is not equal to "[000000]", so the loop keeps going.
It's not completely clear what you want, but I think just ditching the square braces is probably the answer.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
When my application compiles, it will not accept the input for roomNum on the same line in which it asks, "Please enter a room to search for:"
System.out.println();
if(roomNum < 0);
{
System.out.println("Please enter a room to search for: ");
roomNum = input.nextInt();
}
If I just use next instead of nextInt, it doesn't compile correctly.
The code above works, but will not accept the input on the same line which is the functionality I need.
Two things: Remove semi-colon after if condition and use System.out.print() instead if you want input on the same line.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
the System.out.print("\n\n\t\t Do you want to see the next record ? [y/n]"); keeps repeating and repeating how can i stop it when the Records becomes 0.
case 3: //Previous
if (recno!=0) {
String pre;
System.out.print("\n\n\t\t\t\t Previous Record");
System.out.print("\n\n\t\t\t Employee Number: EMP-"+EmpNo[recno]);
System.out.print("\n\t\t\t Employee Name: "+EmpName[recno]);
System.out.print("\n\t\t\t Salary: "+Salary[recno]);
System.out.print("\n\t_________________________________________________________________");
do {
System.out.print("\n\n\t\t Do you want to see the next record ? [y/n]");
pre = reader.readLine();
if(pre.equals("y")) {
recno--;
System.out.print("\n\n\t\t\t Employee Number: EMP-"+EmpNo[recno]);
System.out.print("\n\t\t\t Employee Name: "+EmpName[recno]);
System.out.print("\n\t\t\t Salary: "+Salary[recno]);
}
menu = display.charAt(0);
System.out.print("\n\t_________________________________________________________________");
} while(menu=='n');
System.out.println("Thank You for Using this Program!");
}
else {
System.out.print("\n\n\t\t\t\tRecord Not Found!");
}
break;
To stop the loop use if (condition) break; with the proper condition; put this code in the proper place inside the loop.
You use the key-word "break".
In any loop, whether it is a while, do-while, or for-loop, you can always break the loop with the keyword as a statement "break".
So like this:
do {
if(condition) {
break;
}
while(condition);
In your code, the do-while loop condition is determined by the first character of display being "n". In your loop, display is never updated, so the condition that "n" is the first letter of display is never met. You can also choose to change your logic as the other answers have suggested by using the break statement.
I think you may be looking for a break statement.
This question shows the break in action. The accepted answer shows how to break out of two loops.