Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
when I call the method "getUnknownsAccel" with the problem1 object, for some reason the 'if' statement in the method is not executed to retrieve the value of the variable:
PhysicsProblem problem1 = new PhysicsProblem(accel, vI, vF, t, deltaX);
System.out.println("Which variable are you solving for? ");
String solveFor = scan.next();
// after receiving solveFor input, assesses data accordingly
if (solveFor.equalsIgnoreCase("acceleration"))
{
System.out.println("Solving for Acceleration!");
System.out.println("Are there any other unknowns? (enter 'none' or the name " +
"of the variable)");
missingVar = scan.next();
problem1.setMissingVar(missingVar);
do
{
problem1.getUnknownsAccel();
System.out.println("Are there any other unknowns? (enter 'none' or the name " +
"of the variable)");
missingVar = scan.next(); //// change all these in the program to scan.next, not scan.nextLine
}
while (!missingVar.equalsIgnoreCase("none") || !missingVar.equalsIgnoreCase("acceleration"));
if (missingVar.equals("none"))
{
// Write code for finding solutions
System.out.println("Assuming you have given correct values, the solution is: ");
}
}
After the do/while loop used to retrieve the name of the other variables that are unknown, I call the getUnknownsAccel method from this class file:
public void getUnknownsAccel()
{
//-----------
// checks for another unknown value that is not accel
//-----------
if (missingVar.equalsIgnoreCase("time"))
{
System.out.println("Please enter the value for time: ");
t = scan.nextDouble();
while (t <= 0 || !scan.hasNextDouble())
{
System.out.println("That is not an acceptable value!");
t = scan.nextDouble();
}
}
}
Let's assume for the sake of this problem, that the user WILL enter "time" as the unknown when prompted. Any idea why my code isn't executing the scan function to retrieve the time variable value? Instead, the program just repeats the system.out function "Are there any other unknowns..."
After scanning, you set missingVar to scan.next(), but you do not do anything. The loop continues.
After
missingVar = scan.next();
add the line
getUnknownsAccel();
Note, another issue is that you will need to handle later is that missingVar is local - to access it in getUnknownsAccel(), you should change the declaration to
public void getUnknownsAccel(String missingVar){
}
and instead use
getUnknownsAccel(missingVar);
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
Improve this question
I'm trying to get this loop down but I don't know how to break from it.
System.out.printf("Please enter your given name and surname (Enter 0 to return to main menu)%n");
String name = sc.nextLine();
while (name.equals("0")) {
System.out.printf(MENU_TEMPLATE);
name = sc.nextLine();
if the user enters their name then the program will carry on as normal, but I'm having trouble doing this.
you use a conditional, when you want to break the loop. Then you use the break command.
like
while (name != Integer.toString(0)) {
if (name == "Salami") {
break;
}
}
another way to break the loop is to use a counter.
int i = 0;
while (i < 10) {
//do some code here.
i++;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
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.
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.
Improve this question
I can't figure out why my code isn't working.
It appears to be breaking around the if not equal to yes or no area.
Here's my code:
public static void main(String[] args) {
// TODO code application logic here
Scanner user_input = new Scanner(System.in);
String name;
System.out.println("Hello, what is your name?");
name = user_input.next();
System.out.println("");
String name_answer;
System.out.println("Your name is " + name + ". Is this correct? (yes/no)");
name_answer = user_input.next();
System.out.println("");
if (!name_answer.equals("yes" + "no")) {
System.out.println("Answer not valid. Please input again.");
name_answer = user_input.next();
while (!name_answer.equals("yes" + "no")) {
System.out.println("Answer not valid. Please input again.");
name_answer = user_input.next(); } }
if (name_answer.equals("yes")) {
System.out.println("Thank you, " + name + ". Please proceed to the next question.");
} else if (name_answer.equals("no")) {
System.out.println("Please reinput your name correctly.");
while (name_answer.equals("no")) {
String name_again;
System.out.println("");
System.out.println("What is your correct name?");
name_again = user_input.next();
System.out.println("");
System.out.println("Your name is " + name_again + ". Is this correct? (yes/no)");
name_answer = user_input.next(); }
If i comment out the not-equals block of code (displayed below), the program works. However, with the block of code in, the program breaks.
if (!name_answer.equals("yes" + "no")) {
System.out.println("Answer not valid. Please input again.");
name_answer = user_input.next();
while (!name_answer.equals("yes" + "no")) {
System.out.println("Answer not valid. Please input again.");
name_answer = user_input.next(); } }
My goal is to have any answer not equal to "yes" or "no" be reinputted while a "yes" or "no" brings the program to another step. Thanks for the help.
The technical problem with your code is that you're using concatenation instead of logical operators. "yes" + "no" evaluates to "yesno", which will probably never match your input string.
More fundamentally, the problem is that you're trying to bundle two boolean evaluations into one. Logically, you want to proceed if the answer is not "yes" and the answer is not "no". In Java syntax:
if (!name_answer.equals("yes") && !name_answer.equals("no")) {
If you want to test multiple values at a time, you can use this shortcut:
if (!Arrays.asList("yes", "no", "foo", "bar").contains(name_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 5 years ago.
Improve this question
Hi here is just a fun number guesser where someone inputs a number and my while loop will run until it hits the number inputed. Its just a while loop practice because I am relatively new to Java and wanted more practice. Here is my code. (I am using IntelliJ IDEA)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
boolean password = false;
int passwordGuess = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a Password to be guessed");
int Password = sc.nextInt();
while (password) {
System.out.println("Test");
if (passwordGuess == Password){
System.out.println("Your Password is " + passwordGuess);
password = true;
}
else{
System.out.println(passwordGuess);
passwordGuess++;
}
}
}
}
It prints Enter a Password to be guessed, then I input a number and it prints "Process finished with exit code 0". Any Ideas, Thanks
You have initialized your password variable as false:
boolean password = false;
and then you try to use it in your while
while (password){/*...*/}
It won't work if password is false.
You have a variable password which you set to false.
You never set it to true, so your while loop reads "while (false)" which means it exits before running the block.
I think I must point to the fact that integer casting to boolean is false for any value. Check this question and answer.
So you might try the solution as orhtej2 posted
while (!password)
while loop only loops if expression is true, and you initialize password as false. Hance your loop should read:
while (!password)
set 'boolean password = true' if you want your while loop to start.
while(false) won't start and currently your password is set to false.set boolean password = true if you want your while loop to start.
Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 5 years ago.
Improve this question
I get only two errors of .class expected I am getting this error in my program
String[] email= {"Sarangmemon8","Alimutaba626","Kali_denali"};
String[] pass= {"Sarang","Mujtaba","Kali"};
System.out.println("What is your name?");
String name = input.nextLine();
System.out.println("Hello! "+name +"Would you like to Login? y/n");
String ans = input.nextLine();
if(ans=="y"){
System.out.println("Enter your Email: ");
String username = input.nextLine(email[0][1][2]);
System.out.println("Enter your Password: ");
String password = input.nextLine(pass[0][1][2]);
if(username == email[]) {
if (password == pass[]) {
System.out.println("Hello Mr. " +name);
}
else
System.out.println("Wrong password");
First of all i would highly recommend you to go through your concept of
Arrays and Objects
1st Error:
Your index should be specified while trying to retrieve a value from a array.
You need to mention the index of the array if you want to compare.
Second Error: String is treated as a object in java, and while comparing with object equals() method is used, that's why while equating string use .equals() instead of == .
If you have anymore problem you can comment.
(username == email[] - incorrect syntax. You should specify index value for email array. E.g. email[0].
The same applies to password == pass[]
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
i am getting "illegal character '\u600b' in my return statement for the following code:
public static int getNum() {
Scanner in = new Scanner(System.in);
int number;
boolean goodInput = true;
do {
goodInput = true;
try {
System.out.print("Please enter a positive number: "); // prompts the user
number = Integer.parseInt(in.nextLine()); // Tries to make the next input a number
} catch (Exception e) { // if it breaks
System.out.println("The number you entered was invalid."); // it tells the user it was wrong
goodInput = false; // and runs the loop again
}
if(number <= 0) { // makes sure that the number entered was valid
System.out.println("The number you entered was invalid.");
goodInput = false; // or it re runs the loop
}
}while (!goodInput)
return number;
}
any one know how to fix this?
In looking at the Markdown source of your post, I found a stray non-printable character right before the return. You need to delete that entire line and retype it (or delete that character itself).
If you place the cursor between the r and the e, and press <- a few times, you will see that the cursor does not move one of those times.