While statement isn't excuting inside if statement? [closed] - java

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
I am trying to make it print out a range of specified numbers. So if they select option 1 and put in 1 and 15, I want it to print out 1 to 15. Once it gets to the while statement though it just prints nothing.
import java.util.Scanner;
public class Lab4 {
public static void main(String[] args) {
Scanner in = new Scanner (System.in);
System.out.print("Please choose your choice from the following menu");
System.out.print("\n1) Print through all integer numbers between two given integers");
System.out.print("\n2) Display a right triangular pattern of stars");
System.out.println("\n3) Quit");
int userInput = in.nextInt();
if (userInput == 1) {
System.out.print("Enter the start number: ");
int firstInteger = in.nextInt();
System.out.print("Enter the second number: ");
int secondInteger = in.nextInt();
while (firstInteger < secondInteger);
System.out.print(firstInteger);
firstInteger++;
} else if (userInput == 2) {
System.out.print("Enter the height: ");
int triangleHeight = in.nextInt();
} else if (userInput == 3);{
System.exit(userInput);
}
in.close();
}
}

You should change :
while (firstInteger < secondInteger);
System.out.print(firstInteger);
firstInteger++;
to
while (firstInteger < secondInteger) {
System.out.print(firstInteger);
firstInteger++;
}

while (firstInteger < secondInteger);
This will be treated as two executable lines of instructions,
which is similar like
while (firstInteger < secondInteger)
;
Try to remove the ; after while statement

Related

Can do-while loops only run with boolean variables? Java [closed]

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
I am having some trouble with this small segment. Can do-while loops only run using boolean variables? I am trying to ask the "user" to enter "0" or "1" so that the program will either loop or end.
Error message:
Chapter4Practice.java:23: error: incompatible types: int cannot be
converted to boolean
} while (choice = 1);
^ 1 error
My code:
import java.util.Scanner;
public class Chapter4Practice
{
public static void main(String[] args)
{
int choice, num1, num2;
String input;
Scanner sc = new Scanner(System.in);
do {
System.out.print("Enter a number: ");
num1 = sc.nextInt();
System.out.print("Enter another number: ");
num2 = sc.nextInt();
System.out.println("The sum is " + (num1 + num2));
System.out.println("Do you want to do this again?");
System.out.println("(1 = yes, 0 = no)");
sc.nextLine();
choice = sc.nextInt();
} while (choice = 1);
} //End Main
} //End Class
Short answer: Yes, you can have simple or complex statements inside your do-while but at the end it will have to evaluate to either true or false
Also your statement should be == ( single = means assign, where == is evaluate)

How to fix Switch-case with String and Scanner [closed]

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
After entering gender (M or F), the programs end immediately without getting to the switch-case
I've tried changing 'string' to "string" and nothing still changed. I thought that would make a difference
import java.util.Scanner;
public class FemurHumerus {
public static void main(String[] args) {
String gender;
double FemHum;
double MaleHum;
double FemFem;
double MaleFem;
int FemHumR;
int MaleHumR;
int FemFemR;
int MaleFemR;
Scanner scan = new Scanner(System.in);
System.out.println("What is your gender? (M or F): ");
gender = scan.nextLine();
switch (gender) {
case "F":
System.out.print("Enter femur length: ");
FemFem = in.nextInt();
FemFemR = (int) (FemFem*1.94+28.7);
System.out.print("Enter humerus length: ");
FemHum = in.nextInt();
FemHumR = (int) (FemHum*2.8+28.2);
System.out.println("Height based on femur: "+FemFemR);
System.out.println("Height based on humerus: "+FemHumR);
break;
case "M":
System.out.print("Enter femur length: ");
MaleFem = in.nextInt();
MaleFemR = (int) (MaleFem*1.88+32);
System.out.print("Enter humerus length: ");
MaleHum = in.nextInt();
MaleHumR = (int) (MaleHum*2.9+27.9);
System.out.println("Height based on femur: "+MaleFemR);
System.out.println("Height based on humerus: "+MaleHumR);
break;
default:
System.out.println("Gender not sprecified.");
break;
}
}
}
The expected output should be the height for the specific gender based on femur and humerus
I agree; change your in.nextInt(); to scan.nextInt() unless your in is declared and you aren't showing what's its purpose.
you'll get your switch case then.
I think due to in.nextInt();, that cause in cannot be resolved compile error,
change all in.nextInt(); to scan.nextInt(); then should be ok.

Getting an integer to go to a different line [closed]

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
This is my code for reversing the digits of an integer,
but the result gives each digit reversed but in a new line,
like if you give 341 to it , it gives you:
1
4
3
But I want to make it all come to the same line , is there a way to do it without changing the main code?
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
System.out.println("Enter your number to reverse it ...");
int a=scan.nextInt();
while(a>0) {
i=a%10;
a/=10;
l++;
System.out.println(i);
}
System.out.println("Number of digits: "+l);
}
You could use print instead of println:
System.out.print(i);
Answer :
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
System.out.println("Enter your number to reverse it ...");
int a=scan.nextInt();
while(a>0) {
i=a%10;
a/=10;
l++;
System.out.print(i);
}
System.out.println("Number of digits: "+l);
}
I think your code has few errors. It should be
int i = 0;
while(a>0) {
i=a%10;
a/=10;
System.out.print(i+" ");
i++;
}
In your code you are just picking digits and printing it in reversed order instead of really reversing it.
public static void main(String[] args)
{
int i,l=0,num=0;
Scanner scan=new Scanner(System.in);
System.out.println("Enter your number to reverse it ...");
int a=scan.nextInt();
while(a>0)
{
i=a%10;
a/=10;
num=(num*10)+i;
l++;
}
System.out.println("Number of digits: "+l);
System.out.println("Reversed Number: "+num);
}
Try this code instead.....

How could I add a while loop into my code when there's already if statements [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Have seen other posts, but I don't know when to enter the while loop or for loop for my program run until it meets conditions, which is enter a number between 1 and 20 and the run the code.
import java.util.Scanner;
public class MultiplicationTable {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int number, total;
System.out.println("Please enter an integer from 1 to 20:");
number = scanner.nextInt();
if (number >= 21){
System.out.println("Your integer must be between 1 and 20.");
}
else if ( number <= 0){
System.out.println("Your integer must be between 1 and 20.");
}
else {
for(int i = 1; i<=20; i++){
total = number * i;
System.out.println(i + " X " + number + " = " + total);
}
}
}
}
Put a while loop around the scanner input
while(number < 0 || number > 20)
{
number = scanner.nextInt();
// if statements here
}
With this, you will need to initialize number so that you get into the loop
int number = -1;

Java Program Loop won't Execute and Prompt User? [closed]

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.
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
Okay so I am only part of the way through my array inventory program, but my while loop that is supposed to prompt a user to re-enter a number when they have entered an invalid one will not execute. When an invalid number is entered the program just ends...I have tried a couple methods and neither are working. this is where i am at now, any ideas??
thanks much!
public class InventorySystem {
public static void main(String[] args) {
String[] newItemInfo = new String[1000];
String[] itemDescription = new String[1000];
int[] itemPrice = new int[1000];
int choice;
boolean isValid;
int itemChoice;
Scanner inputDevice = new Scanner(System.in);
System.out.println("*****Raider Inventory System*****");
System.out.println("1. Enter a new item");
System.out.println("2. View Item Information");
System.out.println("3. View a list of all items");
System.out.println("9. End Program\n");
System.out.println("Please enter your selection: ");
choice = inputDevice.nextInt();
if (choice == 1 || choice == 2 || choice == 3 || choice == 9) {
isValid = true;
} else {
isValid = false;
}
** while (isValid = false) {
System.out.println("Invalid entry, please enter either 1, 2, 3, or 9 for menu options.");
** }
for (int x = 0; x < 1000; ++x) {
if (choice == 1) {
System.out.println("Please enter the name if the item: ");
newItemInfo[x] = inputDevice.nextLine();
System.out.println("Please enter the item description: ");
itemDescription[x] = inputDevice.nextLine();
System.out.println("Please enter item price: ");
itemPrice[x] = inputDevice.nextInt();
}
}
if (choice == 2) {
System.out.println("***Show item Description***");
System.out.println("0. ");
System.out.println("please enter the item number ot view details: ");
itemChoice = inputDevice.nextInt();
}
if (choice == 3) {
System.out.println("****Item List Report****");
}
if (choice == 9) {
System.exit(0);
}
}
}
In your line
while(isValid = false)
the = doesn’t do what you think it does. In Java, a single = means assign the expression on the right side to the variable on the left side. It does not mean to compare the two sides.
So you should rather write this:
while (isValid == false)
Note the double ==. This code works, but you can write it even more beautifully:
while (!isValid)
The ! means not.
Don't do while (isValid = false). You're setting it to false!
Instead do
while (!isValid) {
}
Also, don't do while (isValid == false) -- that's ugly code.
Next, change isValid inside the loop.
while (!isValid) {
// get input from user in here
// test input and check if is valid. If so, set isValid = true;
// something must set isValid to true in here, else it will
// loop forever
}
Otherwise you'll be stuck in an infinite loop. The lesson to be learned here is to mentally walk through your code as if you were running it in your brain. This way you catch logic errors like what you've got.

Categories