Skip to main content



      Home
Home » Newcomers » Newcomers » Not sure what to do with if in my situation(Can't figure out how to have two differnet if's)
icon5.gif  Not sure what to do with if in my situation [message #1715078] Wed, 18 November 2015 22:32 Go to next message
Eclipse UserFriend
Hello,
I cannot seem to have two if's without both appearing. I just want one to appear for each situation. So basically its text boxes and if entered age is above 45 it should say (Name) is very old. But if entered age if less than 45 it says (Name) is a (Age) year old. It ends up showing both dialogues. Pls help the noob

Program/Code (There is also a screen shot):




package first;

import javax.swing.JOptionPane;

import com.sun.org.apache.xerces.internal.util.SynchronizedSymbolTable;

public class Main
{
public static void main(String[] args)
{
String firstName;
String lastName;






JOptionPane.showMessageDialog(null, "Welcome");

firstName = JOptionPane.showInputDialog("Enter Your First Name");
lastName = JOptionPane.showInputDialog("Enter Your Last Name");

String s = JOptionPane.showInputDialog("Enter Your Age");
int age = Integer.parseInt(s);

if(age < 45){
JOptionPane.showMessageDialog(null, firstName + " " + lastName + " is" + " a " + age + " year old.");}else{
JOptionPane.showMessageDialog(null, firstName + " " + lastName + " is " + age + " year old.");}

if(age > 45){
JOptionPane.showMessageDialog(null, firstName + " " + lastName + " is" + " very old.");}
}
}


index.php/fa/23999/0/
  • Attachment: eclipse.PNG
    (Size: 56.21KB, Downloaded 834 times)
Re: Not sure what to do with if in my situation [message #1715090 is a reply to message #1715078] Thu, 19 November 2015 02:38 Go to previous message
Eclipse UserFriend
Kyle,

This is a basic programming question best asked on something like
Stackoverflow. Here the question should be about problem related to
using Eclipse specifically.

That being said, if you run this in the debugger---Eclipse has a very
good one---you can step through the code and see why this is happening.
I would start by formatting your code so it's less confusing. Then
you'll see your first block has an else that shows a message and then
the second condition also shows a message.

On 19/11/2015 5:09 AM, Kyle Weekly wrote:
> Hello,
> I cannot seem to have two if's without both appearing. I just want one to appear for each situation. So basically its text boxes and if entered age is above 45 it should say (Name) is very old. But if entered age if less than 45 it says (Name) is a (Age) year old. It ends up showing both dialogues. Pls help the noob
>
> Program/Code (There is also a screen shot):
>
>
>
>
> package first;
>
> import javax.swing.JOptionPane;
>
> import com.sun.org.apache.xerces.internal.util.SynchronizedSymbolTable;
>
> public class Main
> {
> public static void main(String[] args)
> {
> String firstName;
> String lastName;
>
>
>
>
>
>
> JOptionPane.showMessageDialog(null, "Welcome");
>
> firstName = JOptionPane.showInputDialog("Enter Your First Name");
> lastName = JOptionPane.showInputDialog("Enter Your Last Name");
>
> String s = JOptionPane.showInputDialog("Enter Your Age");
> int age = Integer.parseInt(s);
>
> if(age < 45){
> JOptionPane.showMessageDialog(null, firstName + " " + lastName + " is" + " a " + age + " year old.");}else{
> JOptionPane.showMessageDialog(null, firstName + " " + lastName + " is " + age + " year old.");}
>
> if(age > 45){
> JOptionPane.showMessageDialog(null, firstName + " " + lastName + " is" + " very old.");}
> }
> }
>
>
>
Previous Topic:Eclipse Sandboxed // Green border?
Next Topic:ABAP program execution through eclipse
Goto Forum:
  


Current Time: Wed May 07 08:44:40 EDT 2025

Powered by FUDForum. Page generated in 0.08233 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top