if else statement help usin javax.swing

  • (2 Pages)
  • +
  • 1
  • 2

28 Replies - 6458 Views - Last Post: 21 September 2009 - 05:00 PM Rate Topic: -----

#1 kilowog  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 16-September 09

if else statement help usin javax.swing

Posted 16 September 2009 - 07:05 AM

Hello everyone,
Right now this java programming is really bringing me down. I've tried at least 20 different set ups for what i want to do. I am trying to build an if else state using the javax.swing.JOptionPane . Unfortunately its not working. Every time in input something it leads me to the next thing on the list and then quits on me. IT doesnt even matter what i put really b/c it still goes through the same routine. I want the user input to be a yes or no thing and i have tried using string and boolean and even int values to achieve this.
import javax.swing.JOptionPane;

public class Lab3
{
  public static void main(String[] args)
  {
   
	System.out.println("Enter yes or no");
	String userInput = JOptionPane.showInputDialog("Is it important?");
	String s = ("");
	
	if (s == ("yes"))
	{
	 userInput = JOptionPane.showInputDialog("Is it urgent?");
	}
	if (s == ("yes"))
	{
	 userInput = JOptionPane.showInputDialog("Is it worth your time?");
	 }
	 if (s == ("yes")
	 {
	 userInput = JOptionPane.showInputDialog("Will it take more than 15 minutes?");
	 }
	else if (s == ("no"))
	{
	   userInput = JOptionPane.showInputDialog("Is it a Want?");
	}
	else if (s ==("yes"))
	{
	 System.out.println("Maybe Someday");
	{
	 else
	   {
		 System.out.println("It has been put in the trash");
	   }
  }
}


Before i had boolean on there but i am just not sure what to do at this point. i am following along a flow chart that i have. for instance if i were to choose yes i would want it to lead to the next line of yes material and if no so forth. Any help on how i can get this to work is much appreciated thank you.[code]

This post has been edited by kilowog: 16 September 2009 - 07:25 AM


Is This A Good Question/Topic? 0
  • +

Replies To: if else statement help usin javax.swing

#2 nelson64  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 16-September 09

Re: if else statement help usin javax.swing

Posted 16 September 2009 - 07:12 AM

First of all, when comparing strings you need to use .equals. So you would say s.equals("yes") There is also .equalsIgnoreCase which is helpful in case the user uses capital letters when your code handles lower case letters so then you'd type s.equalsIgnoreCase("yes")

Also, you if blocks are a little messy. I've added comments below to show you.


System.out.println("Enter yes or no");
String userInput = JOptionPane.showInputDialog("Is it important?");
String s = ("");

if (s == ("yes"))
{
userInput = JOptionPane.showInputDialog("Is it urgent?");
}
if (s == ("yes")) // this condition is the same as the last if block so why not put both lines in the same block
{
userInput = JOptionPane.showInputDialog("Is it worth your time?");
}
if (s == ("yes") // * enters here if yes
{
userInput = JOptionPane.showInputDialog("Will it take more than 15 minutes?");
}
else if (s == ("no"))
{
userInput = JOptionPane.showInputDialog("Is it a Want?");
}
else if (s ==("yes")) // this block will never be reached because if s is yes then it went into the if two blocks above this one - I put an * at that one to show you
{
System.out.println("Maybe Someday");
{
else
{
System.out.println("It has been put in the trash");
}

This post has been edited by nelson64: 16 September 2009 - 07:21 AM

Was This Post Helpful? 0
  • +
  • -

#3 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 729
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: if else statement help usin javax.swing

Posted 16 September 2009 - 07:17 AM

read the rules before posting guys..please edit your posts and add the code tags
Was This Post Helpful? 0
  • +
  • -

#4 kilowog  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 16-September 09

Re: if else statement help usin javax.swing

Posted 16 September 2009 - 07:30 AM

import javax.swing.JOptionPane;

public class Lab3
{
  public static void main(String[] args)
  {
   
	System.out.println("Enter yes or no");
	String userInput = JOptionPane.showInputDialog("Is it important?");
	String s = ("yes");
	
	if (s == ("yes"))
	{
	 userInput = JOptionPane.showInputDialog("Is it urgent?");
	 System.out.println("Yes");
	}
	if (s == ("yes"))
	{
	  userInput = JOptionPane.showInputDialog("Is it the best use of my time to do this myself?");
	  System.out.println("Yes");
	}
	if (s == ("yes"))
	{
	  userInput = JOptionPane.showInputDialog("Will it take more than 15 minutes?");
	  System.out.println("Yes");
	}
	if (s == ("yes"))
	{
	  System.out.println("Project List");
	}
	
	else if (s == ("no"))
	{
	   userInput = JOptionPane.showInputDialog("Is it a Want?");
	   System.out.println("No");
	}
	
	 else if (s == ("no"))
	   {
		 userInput = JOptionPane.showInputDialog("Is it actionable?");
		 System.out.println("No");
	   }
	 else if (s == ("no"))
	 {
	   userInput = JOptionPane.showInputDialog("Delegate it");
	   System.out.println("No");
	 }
	 else if (s == ("no"))
	 {
	   userInput = JOptionPane.showInputDialog("Can I do it now?");
	   System.out.println("No");
	 }
  }
}



Ok here is more of the program i am trying to make. as for the s.equals thing i must have the worst basic java programming book in the world b/c that is not in there and also i cannot seem to find much about that. but i guess do i at least have the string part right? or any of this right? lol i am trying pretty hard and was NOT able to sleep b/c this stuff was constantly running through my head. i am still trying to get it to go from when a person inputs yes it takes them to a yes option to where they can enter more input and the same if they put a no it takes them to the "no" branch of choices. all i have ever had as far as programming training was a pseudocode class and cobol class =(
Was This Post Helpful? 0
  • +
  • -

#5 nelson64  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 16-September 09

Re: if else statement help usin javax.swing

Posted 16 September 2009 - 07:36 AM

Okay. Are you trying to make it so if the user enters yes, there is a question that follows that, and then when they answer that, there's another question following?

if so you need to nest your if's which means...
if(....)
{
if(....)
{ ....}
else
{ .... }
}
else
...

also, you're never reading the user input

to do that you can use a BufferedReader, which you can find a lot online about in the Java API

so say...
BufferedReader stdin = new BufferedReader(new InputStream(System.in));
then s = stdin.readLine();
everytime the user enters and answer you need to update s
so call stdin.readLine() again

This post has been edited by nelson64: 16 September 2009 - 07:39 AM

Was This Post Helpful? 0
  • +
  • -

#6 kilowog  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 16-September 09

Re: if else statement help usin javax.swing

Posted 16 September 2009 - 07:41 AM

so then the new code i put up was not nested ifs?
do u mean like within each of the ifs there would be a yes or a no choice leading to more questions?
and i googled the s.equals thing and it seems like a pretty neat thing to use but b/c of my inexperience in this stuff i probably shouldnt use it b/c i might make an even bigger mess of things lol.....this stuff is making me show my age =(
Was This Post Helpful? 0
  • +
  • -

#7 nelson64  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 16-September 09

Re: if else statement help usin javax.swing

Posted 16 September 2009 - 08:07 AM

[quote name='kilowog' date='16 Sep, 2009 - 06:41 AM' post='768945']
so then the new code i put up was not nested ifs?
do u mean like within each of the ifs there would be a yes or a no choice leading to more questions?

well you need to return the s.equals for strings. == will only compare that both of the items are of the same type, in this case it will return true because they are both strings. but it will not declare if it equals yes or no.

the new code is not nested if's because it is...
	if (s == ("yes"))
	{
	 userInput = JOptionPane.showInputDialog("Is it urgent?");
	 System.out.println("Yes");
	}
	if (s == ("yes"))
	{
	  userInput = JOptionPane.showInputDialog("Is it the best use of my time to do this myself?");
	  System.out.println("Yes");
	}


if you wanted it to be nested then the second if would have to be within the brackets of the first if
nested ifs would make it so that you ask the question 1 then if yes you ask question 2, if no you ask question three
so if question 1 is yes and question 2 is yes then it will ask question 4
Was This Post Helpful? 0
  • +
  • -

#8 kilowog  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 16-September 09

Re: if else statement help usin javax.swing

Posted 16 September 2009 - 08:25 AM

oh wow ok i understand the nested ifs then sorry
but for the buffer reader thing you posted.......i researched through google and all the pages i kept seeing involved importing java.io or some such thing......i was wondering if it were possible to do it another way.....perhaps a more simpler way without the importing of anything other than the java.swing i had already imported. Also the buffer reader pages i found involved a lot more intense coding then i think at this point i would be capable of. thank you for the quick replies =)
Was This Post Helpful? 0
  • +
  • -

#9 nelson64  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 16-September 09

Re: if else statement help usin javax.swing

Posted 16 September 2009 - 08:28 AM

buffered reader is the only way I'm familiar with reading user input
the only other way i can think of that would avoid a buffered reader is using buttons that are yes and no but that's up to you
Was This Post Helpful? 0
  • +
  • -

#10 syfran  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: if else statement help usin javax.swing

Posted 16 September 2009 - 09:14 AM

View Postnelson64, on 16 Sep, 2009 - 07:28 AM, said:

buffered reader is the only way I'm familiar with reading user input
the only other way i can think of that would avoid a buffered reader is using buttons that are yes and no but that's up to you

Scanner tends to make things really easy.
Was This Post Helpful? 0
  • +
  • -

#11 NeoTifa  Icon User is offline

  • NeoTifa Codebreaker, the Scourge of Devtester
  • member icon





Reputation: 4142
  • View blog
  • Posts: 18,283
  • Joined: 24-September 08

Re: if else statement help usin javax.swing

Posted 16 September 2009 - 09:27 AM

What I saw from the first post (which is all I read) was that you were assigning the input to userInput, yet testing an empty String s for the input. Plus it would be userInput.equals("yes") or even better userInput.equalsIgnoreCase("yes") just in case they type Yes, YES, YeS, yEs, etc...
Was This Post Helpful? 0
  • +
  • -

#12 kilowog  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 16-September 09

Re: if else statement help usin javax.swing

Posted 16 September 2009 - 11:07 AM

I'm sorry i am not familiar w/ the scanner syntax.....but if it does make things easier than that would be totally awesome lol....easy is always good.
As for the .equals.....not familiar w/ that either ....hopefully i dont have to import anything else to get that kind of thing to work.
Was This Post Helpful? 0
  • +
  • -

#13 JAVADDICT  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 10-September 09

Re: if else statement help usin javax.swing

Posted 16 September 2009 - 11:20 AM

View Postkilowog, on 16 Sep, 2009 - 10:07 AM, said:

I'm sorry i am not familiar w/ the scanner syntax.....but if it does make things easier than that would be totally awesome lol....easy is always good.
As for the .equals.....not familiar w/ that either ....hopefully i dont have to import anything else to get that kind of thing to work.



NeoTifa was right your code should be

if(userInput.equalsIgnoreCase("yes"))
{
	//do the action u want
}



good luck java programming :) keep it up and one day you could be a good programmer and remember google is your best friend you can always try and search it for thing u want to know about :)
Was This Post Helpful? 0
  • +
  • -

#14 kilowog  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 56
  • Joined: 16-September 09

Re: if else statement help usin javax.swing

Posted 16 September 2009 - 11:42 AM

import javax.swing.JOptionPane;

public class Test8
{
  public static void main(String[] args)
  {
   
	System.out.println("Enter yes or no");
	String userInput = JOptionPane.showInputDialog("Is it Important?");
	String s = ("");
	
	if (userInput.equalsIgnoreCase("yes"))
	{
	 userInput = JOptionPane.showInputDialog("Is it urgent?");
	 System.out.println("Yes");
	 
	if (userInput.equalsIgnoreCase("yes"))
	{
	  userInput = JOptionPane.showInputDialog("Is it the best use of my time to do this myself?");
	  System.out.println("Yes");
	}
	if (userInput.equalsIgnoreCase("yes"))
	{
	  userInput = JOptionPane.showInputDialog("Will it take more than 15 minutes?");
	  System.out.println("Yes");
	}
	if (userInput.equalsIgnoreCase("yes"))
	{
	  System.out.println("Project List");
	}
	
	else if (userInput.equalsIgnoreCase("no"))
	{
	   userInput = JOptionPane.showInputDialog("Is it a Want?");
	   System.out.println("No");
	}
	
	 else if(userInput.equalsIgnoreCase("no"))
	 {
		 userInput = JOptionPane.showInputDialog("Is it actionable?");
		 System.out.println("No");
	 }
	 else if(userInput.equalsIgnoreCase("no"))
	 {
	   userInput = JOptionPane.showInputDialog("Delegate it");
	   System.out.println("No");
	 }
	 else if(userInput.equalsIgnoreCase("no"))
	 {
	   userInput = JOptionPane.showInputDialog("Can I do it now?");
	   System.out.println("No");
	 }
  }
}
}



Ok when the first box pops up for user input and i put a no in it it does nothing but if i put a yes in it then it moves down the list of yes boxes i have but it wont switch to any of the no boxes i have when i input no. Other than that how is my syntax looking? also lets so for one of the boxes i want a split off of 2 other choices for that specific box how would i do that? Like for my "Is it actionable?" box if i choose yes i would want somethin like "Act on it!" and if i choose to input no then "Throw it away"
Was This Post Helpful? 0
  • +
  • -

#15 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 89
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: if else statement help usin javax.swing

Posted 16 September 2009 - 11:52 AM

Take a look at this code, and edit it as you prefer.
I've added an else if down there...
public static void main(String[] args)
	  {
	   
		System.out.println("Enter yes or no");
		String userInput = JOptionPane.showInputDialog("Is it Important?");
		String s = ("");
		
		if (userInput.equalsIgnoreCase("yes"))
		{
		 userInput = JOptionPane.showInputDialog("Is it urgent?");
		 System.out.println("Yes");
		 
		if (userInput.equalsIgnoreCase("yes"))
		{
		  userInput = JOptionPane.showInputDialog("Is it the best use of my time to do this myself?");
		  System.out.println("Yes");
		}
		if (userInput.equalsIgnoreCase("yes"))
		{
		  userInput = JOptionPane.showInputDialog("Will it take more than 15 minutes?");
		  System.out.println("Yes");
		}
		if (userInput.equalsIgnoreCase("yes"))
		{
		  System.out.println("Project List");
		}
		
		else if (userInput.equalsIgnoreCase("no"))
		{
		   userInput = JOptionPane.showInputDialog("Is it a Want?");
		   System.out.println("No");
		}
		
		 else if(userInput.equalsIgnoreCase("no"))
		 {
			 userInput = JOptionPane.showInputDialog("Is it actionable?");
			 System.out.println("No");
		 }
		 else if(userInput.equalsIgnoreCase("no"))
		 {
		   userInput = JOptionPane.showInputDialog("Delegate it");
		   System.out.println("No");
		 }
		 else if(userInput.equalsIgnoreCase("no"))
		 {
		   userInput = JOptionPane.showInputDialog("Can I do it now?");
		   System.out.println("No");
		 }
	  }
		else if(userInput.equalsIgnoreCase("no")){
			//do whatever you want here
		}
	}


This post has been edited by AntonWebsters: 16 September 2009 - 11:53 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2