If statements not running [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 8 years ago.
Improve this question
The strings were parsed to integers, so the condition of the if statements are properly right set. Why are the if statements not running? Why there doesn't appear a MessageDialog with a response?
class process{
public static void whoIs(){
JFrame frame=new JFrame("The Oldest");
String a=JOptionPane.showInputDialog(frame, "Enter, please, the first name and age:", "QUIZ: Who is the Oldest", JOptionPane.QUESTION_MESSAGE);
String b=JOptionPane.showInputDialog(frame, "Enter, please, the second name and age:", "QUIZ: Who is the Oldest", JOptionPane.QUESTION_MESSAGE);
String age1=a.replaceAll("[^\\d]","");
String age2=a.replaceAll("[^\\d]","");
String name1=a.replaceAll("\\d","");
String name2=b.replaceAll("\\d","");
int age1int=Integer.parseInt(age1);
int age2int=Integer.parseInt(age2);
if (age1int>age2int){
JOptionPane.showMessageDialog(frame, name1+ " is the oldest!", "QUIZ: Who is the Oldest?", JOptionPane.INFORMATION_MESSAGE);
}
if (age2int>age1int) {
JOptionPane.showMessageDialog(frame, name2+ " is the oldest!", "QUIZ: Who is the Oldest?", JOptionPane.INFORMATION_MESSAGE);
}
}
}

Your both ages are same and your if condition will not match since you are not considering the equality. I think you missed this, Next time try with debugger then you can identify these kind of issues by your own.
String age1=a.replaceAll("[^\\d]","");
String age2=a.replaceAll("[^\\d]","");
This should change this to following.
String age1=a.replaceAll("[^\\d]","");
String age2=b.replaceAll("[^\\d]","");

Related

Java - "while" with using not equal object >> not working [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
Why is the below code not working when using "!" not equals; it gave me "while" statement has an empty body? and when I'm removing the "!" it works...
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String input = "";
while (!input.equals("quit")); {
System.out.print("Input : ");
input = scanner.next().toLowerCase();
System.out.println(input);
}
}
}
you have to remove the semicolon
while(!input.equals("quit")) {
...
}
Remove the ; in the loop before {

Switch Case statement or its alternate If Condition is not working in android [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 3 years ago.
Improve this question
I as using switch case for a condition check. It is not working. Here how I have implemented.
#Override
public void onReceiveNotification(String notificationType, String message, String flag) {
super.onReceiveNotification(notificationType, message, flag);
Log.d("tag", "==onReceive Notivation====1===" + notificationType);
if (notificationType.equalsIgnoreCase("MULTIPART_TYPES"))
{
Log.d("tag", "==onReceive Notivation===2====" + notificationType);
}
switch (notificationType) {
case "MULTIPART_TYPES":
Log.d("tag", "==onReceive Notivation=======" + message);
break;
}
}
this is my Log output
==onReceive Notivation====1=== MULTIPART_TYPES
However it is entering into the method with respected string message. But it is not entering to the either to switch or if conditions.
You have a space front of your notificationType.
Remove it or add trimming to your check. Something like this.
if (notificationType.trim().equalsIgnoreCase("MULTIPART_TYPES"))
{
Log.d("tag", "==onReceive Notivation===2====" + notificationType);
}

I can not get my code to run due to a run time error when trying to assign the String userAns to input [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 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'm writing a simple quiz program. The code on line 19 is giving me a run time error. Could someone please advise why?
import java.util.Scanner;
public class javaQuiz {
public static void main(String[] args) {
String questionOne = "Who is the best band member of the beatles?";
String questionOneAns = "John";
String questionTwo = "what is 1 + 1?";
int questionTwoAns = 2;
String questionThree = "What continent is China a part of?";
String questionThreeAns = "Asia";
String questionFour = "Who is the Turing Test named after?";
String questionFourAns = "Alan Turing";
Scanner userI = new Scanner(System.in);
String userAns = scan.NextLine();
System.out.println(questionOne);
if(userAns == questionOneAns) {
System.out.println("Correct!");
} else {
System.out.println("Wrong answer!");
}
}
}
Isnt it because your scanner is called userI instead of scan?
Line 19 should be:
String userAns = userI.nextLine();

Java String comparison is not working [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 7 years ago.
Improve this question
public static void main(String[] args) {
String a = "Hi I AM ";
String a1 = "Hi I AM";
if (a1.equalsIgnoreCase(a)) {
System.out.println("equal");
} else {
System.out.println("not equal");
}
}
Why above code is displaying not equal in console?
The first String has an additional space at the end, so it's not equal to the second String.
it will display "not equal" because you have added an extra space in a(which is not equal to a1).
String a="Hi I AM "; // has extra space in the end(8 characters)
String a1="Hi I AM"; //this string has 7 characters
Edit:
it actually works fine after your edit to the question.
Better to use trim() method for removing the unwanted white spaces in leading and tailing positions of the string.
public static void main(String[] args) {
String a="Hi I AM";
String a1="Hi I AM";
if(a1.equalsIgnoreCase(a.trim()))
{
System.out.println("equal");
}
else
{
System.out.println("not equal");
}
See the result:

String expected Error? [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
import java.util.Scanner;
public class Fernando {
public static void main(String[] args) {
Scanner skype = new Scanner(System.in);
System.out.print("What is your name ")?
String n = skype.nextLine(); // This is the line with the error
System.out.print("\n and how old are you?");
int y = skype.nextInt();
displayInfo(n,y);
}
public String displayInfo(String name, int age){
return name +" is "+ age+" years old.";
}
}
All it says is expected. I don't understand what's wrong at all.
Try changing
System.out.print("What is your name ")?
to:
System.out.println("What is your name?");
This should work, as you have a ? instead of a ;.

Categories

Resources