Simple question about using names in else-if statements: [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 2 years ago.
Improve this question
I'm new and I need some guidance on how to use names in Java. I tried to use names to trigger the else if statements but I don't know how they work.
public class Else_If_Statements {
public static void main(String[] args){
int flavor = Chocolate;
if (flavor = Vanilla)
system.out.println("That will be 3$!");
else if (flavor = Caramel)
system.out.println("That will be 6$!");
else if (flavor = Chocolate)
system.out.println("That will be 5$!");
else
system.out.println("Sorry, that flavor isn't available!");
}
}

In Java, the if else works this way. The named value to be assigned to the String datatype and equals() method has to be used for the comparison.
public class Else_If_Statements {
public static void main(String[] args){
String flavor = "Chocolate";
if (flavor.equals("Vanilla"))
System.out.println("That will be 3$!");
else if (flavor.equals("Caramel"))
System.out.println("That will be 6$!");
else if (flavor.equals("Chocolate"))
System.out.println("That will be 5$!");
else
System.out.println("Sorry, that flavor isn't available!");
}
}

Related

java.lang.Error am i missing something in the code? [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 1 year ago.
Improve this question
This is just a simple query if anyone knows why I am facing this error I would appreciate the help.
The problem is simple java if else if else loop problem .
so I tried to make syntax as correct as possible.
The code here below is the one I am executing.
public class ifelseloop {
public static void main(String[] args){
// int a = 20;
// int b = 20;
String city = "Delhi";
if (city == "madras") {
System.out.println("the city is not delhi its madras");
}else if(city == "chennai") {
System.out.println("the city is not delhi its chennai");
}else if(city == "bartar") {
System.out.println("the city is bartar");
}else {
System.out.println(city);
}
}
}
and the error that I am getting is this.
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
at ifelseloop.main(ifelseloop.java:2)

Cant print the value of scan.nextIn(); [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
I'm trying to delete the user integer input from an array. However, I can't get the value of the input.
while(keepGoing)
{
while (!scan.hasNextInt() )
{
scan.next();
System.out.print('\n'+"Choose a valid number: ");
}
int unitA = scan.nextInt();
if (unitA < 1)
{
System.out.print('\n'+"Choose one of the options: ");
keepGoing = true;
}
else if (unitA > 14)
{
System.out.print('\n'+"Choose one of the options: ");
keepGoing = true;
}
else
lengthValue.remove(unitA);
scan.close();
keepGoing = false;
}
//lengthValue.remove(int unitA);
System.out.println(unitA);
In my opinion, you forget to press "Enter" after entering input. A scanner can read the input if only you press the "Enter" key. Your solution seems correct to me. I was able to properly run it on my PC.
You can find a similar question here:
How to use Scanner to accept only valid int as input

Don't know why my while loop doesn't work (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 5 years ago.
Improve this question
The purpose of my code is to enter a pin, and it'll check if it's right or not. If it isn't, the question will loop.
For some reason, my code doesn't loop properly, and a lot of the code is underlined. Specifically the while loop itself and the second JOptionPane
// package loop;
import javax.swing.JOptionPane;
public class loop {
public static void main(String[] args) {
int correctPin = 3333;
int count = 0;
String maybePin = JOptionPane.showInputDialog("Please enter the PIN");
int sMaybePin = Integer.parseInt(maybePin);
while(correctPin != sMaybePin);{
maybePin = JOptionPane.showInputDialog("Please enter the PIN");
count = count-1;
}
JOptionPane.showMessageDialog(null, count);
}
}
while(correctPin != sMaybePin); <--
Look at that ; that terminates the loop right there. You need to remove that.
You never update sMabyPin which is the variable you are checking against. If you do what #John and #ANS suggested you'll be stuck in an infinite loop.
Remove the ; after the while statement and correct set the value of the variable sMaybePin to the input vlaue and ot works
public static void main(String[] args) {
int correctPin = 3333;
int count = 0;
String maybePin = JOptionPane.showInputDialog("Please enter the PIN");
int sMaybePin = Integer.parseInt(maybePin);
while(correctPin != sMaybePin){
sMaybePin = Integer.parseInt(JOptionPane.showInputDialog("Please enter the PIN"));
count = count-1;
}
JOptionPane.showMessageDialog(null, count);
}

Q: Whats wrong with this java code? [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 8 years ago.
Improve this question
package firstproject;
import java.util.Scanner;
public class walkthrough {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
String a = in.nextLine();
System.out.println("Welcome home!");
System.out.println("Would you like to go in?");
String door = a;
if(a.equals(door));
System.out.println("Your are now in the house what now?");
}
}
This:
if(a.equals(door));
System.out.println("Your are now in the house what now?");
is:
if(a.equals(door)) { }
System.out.println("Your are now in the house what now?");
The print statement will be executed regardless of the value of the expression in the if condition.
Remove the redundant ; after the if condition.
Also please follow the conventions and rename your class to begin with a capital letter.
Semicolon after if statement. Change ; in {
if(a.equals(door)); // semicolon - statement
System.out.println("Your are now in the house what now?");
Which means System.out.println("Your are now in the house what now?"); is executed irrespective of if statement.
Class names should start with a capital letter in Java: walkthrough -> Walkthrough

If statement not running: any ideas? [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 8 years ago.
Improve this question
I am trying to compare the third number to the last number and the fourth number to the fifth number. The if statement isn't running at all any help would be appreciated.
import java.util.*;
class Problem2{
public static void main (String[] args){
Scanner s = new Scanner(System.in);
System.out.println("Enter a number between 100000 and 999996: ");
int m = s.nextInt();
if(m < 100000 || m > 999996){
System.out.println("Out of range!");
}
else{
String j = Integer.toString(m);
for(int i=2;i<6,i++){
System.out.println(j.charAt(i));
if(j.charAt(i) == (j.charAt(i)+3)&& (j.charAt(i)+1) == (j.charAt(i)+2)){
System.out.println("Works!");
}
}
}
}
}
You are incrementing the value, not the index of the character.
Change this:
if (j.charAt(i) == (j.charAt(i)+3)&& (j.charAt(i)+1) == (j.charAt(i)+2))
To this:
if (j.charAt(i) == j.charAt(i+3) && j.charAt(i+1) == j.charAt(i+2))
// increment indexes --^ --^ --^
Move the additions inside of the charAt() calls.
if(j.charAt(i) == (j.charAt(i+3))&& (j.charAt(i+1)) == (j.charAt(i+2)))

Categories