14 Replies - 3070 Views - Last Post: 03 October 2009 - 12:25 PM Rate Topic: -----

#1 Kairi  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 107
  • Joined: 02-October 09

Switch Case & char variables

Post icon  Posted 02 October 2009 - 12:32 PM

//import packages needed
import javax.swing.*;
import java.awt.*;

public class Math {
	public static void main (String args[]) {
		
		//decalre variables
		double message,S,A,M,D, choice = 4; 
		char character;
		int num, answer;
		
		
		//Shows a dialog for the user to input their choice
		choice = Double.parseDouble(JOptionPane.showInputDialog("A (for subtraction)" + "\nS (for Subtraction)" + "\nM (for multiplication)" +
				"\nD (for division)"));
			
		//Case statement to determine what is selected first
		JOptionPane.showInputDialog("Please enter a number: ");
		
		while (character != character)
		{
				JOptionPane.showInputDialog("Please enter a number: ");
		}
			switch (character)
			{
				case 1:
							if (choice == A)
							{
								answer = num + num;
							}
				break;
				case 2:
								if (character == S)
								{
									answer = num - num;
								}
				break;
				case 3:
									if (character == M)
									{
										answer = num * num;
									}
				break;
				case 4:
										if(character == D)
										{
											answer = num / num;
										}
				break;
					default:
						JOptionPane.showMessageDialog(null, "WARNING","You have not entered the value correctly", JOptionPane.WARNING_MESSAGE);
			}
		}
	}   


I hope someone can at least help me figure out what is wrong with this. I have been looking for a few hours now and i can't figure what i am doing wrong. All i know is i may need to assign the choices A, S, M, D but to what exactly. I'd much appreciated if you can point out what is needed and if i am doing it correctly. Yes i know i have to place an output message at the end but i'm trying to make the case better so it wuld at least compile. Hope i get some help. No, i'm not asking to fully complete my homework all i am doing here is asking for some help. I normally learn visually so it makes sense if my code looks less of a programmer typed.

Is This A Good Question/Topic? 0
  • +

Replies To: Switch Case & char variables

#2 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Switch Case & char variables

Posted 02 October 2009 - 12:39 PM

Kairi, eh? So I'm helping a Kingdom Hearts character with a program? ;)

Anyway...

When you assign the choice variable to the Double.parseDouble(), that is incorrect. You want...

character = JOptionPane.showInputDialog("A (for addition)" + "\nS (for Subtraction)" + "\nM (for multiplication)" +
                "\nD (for division)");


Next, you don't need a while loop to do anything...You just need 2 numbers to hold and then use the operation.

// I'd use double

double num1 = Double.parseDouble(JOptionPane.showInputDialog("Please enter a number:"));
double num2 = Double.parseDouble(JOptionPane.showInputDialog("Please enter a number:"));


Use those and remove the while loop and everything else should be fine. (you should move immediately from those 2 statements to the switch-case)

Hope this helps! :)

This post has been edited by Locke: 02 October 2009 - 12:40 PM

Was This Post Helpful? 0
  • +
  • -

#3 Kairi  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 107
  • Joined: 02-October 09

Re: Switch Case & char variables

Posted 02 October 2009 - 01:00 PM

View PostLocke, on 2 Oct, 2009 - 11:39 AM, said:

Kairi, eh? So I'm helping a Kingdom Hearts character with a program? ;)

Anyway...

When you assign the choice variable to the Double.parseDouble(), that is incorrect. You want...

character = JOptionPane.showInputDialog("A (for addition)" + "\nS (for Subtraction)" + "\nM (for multiplication)" +
                "\nD (for division)");


Next, you don't need a while loop to do anything...You just need 2 numbers to hold and then use the operation.

// I'd use double

double num1 = Double.parseDouble(JOptionPane.showInputDialog("Please enter a number:"));
double num2 = Double.parseDouble(JOptionPane.showInputDialog("Please enter a number:"));


Use those and remove the while loop and everything else should be fine. (you should move immediately from those 2 statements to the switch-case)

Hope this helps! :)


//import packages needed
import javax.swing.*;
import java.awt.*;

public class Math {
	public static void main (String args[]) {
		
		//decalre variables
		double message,S,A,M,D, choice = 4; 
		char character;
		int num, answer;
		
		
		//Shows a dialog for the user to input their choice
		choice = Double.parseDouble(JOptionPane.showInputDialog("A (for subtraction)" + "\nS (for Subtraction)" + "\nM (for multiplication)" +
				"\nD (for division)"));
			
		//Case statement to determine what is selected first
		JOptionPane.showInputDialog("Please enter a number: ");
		
		double num1 = Double.parseDouble(JOptionPane.showInputDialog("Please enter a number:"));
		double num2 = Double.parseDouble(JOptionPane.showInputDialog("Please enter a number:"));
			switch (character)
			{
				case 1:
							if (choice == A)
							{
								answer = num + num;
							}
				break;
				case 2:
								if (character == S)
								{
									answer = num - num;
								}
				break;
				case 3:
									if (character == M)
									{
										answer = num * num;
									}
				break;
				case 4:
										if(character == D)
										{
											answer = num / num;
										}
				break;
					default:
						JOptionPane.showMessageDialog(null, "WARNING","You have not entered the value correctly", JOptionPane.WARNING_MESSAGE);
			}
		}
	}

For some reason errors come up saying num, A,S,D,M have not been initialized. Dang, i've been at this for a long time and i still get mistakes. Is there a way of initializing those but i'm not exactly sure to what. Would setting them equal to zero help? Sorry for the bother but i keep losing track all my data at times. It's hard enough to remember them let alone doing the code :P
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Switch Case & char variables

Posted 02 October 2009 - 02:32 PM

you are swithcing oin a char type so

switch(character) {
	case 'A':
	   ....
	   break;
}


Was This Post Helpful? 0
  • +
  • -

#5 Kairi  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 107
  • Joined: 02-October 09

Re: Switch Case & char variables

Posted 02 October 2009 - 03:14 PM

View Postpbl, on 2 Oct, 2009 - 01:32 PM, said:

you are swithcing oin a char type so

switch(character) {
	case 'A':
	   ....
	   break;
}



Errm it keeps saying loss of percision or incomparable types: double and java.lang.String

//import packages needed
import javax.swing.*;
import java.awt.*;

public class Math {
	public static void main (String args[]) {
		
		//decalre variables
		double message,S,A,M,D, choice = 4; 
		char character;
		int  answer;
		
		
		//Shows a dialog for the user to input their choice
		choice = Double.parseDouble(JOptionPane.showInputDialog("A (for subtraction)" + "\nS (for Subtraction)" + "\nM (for multiplication)" +
				"\nD (for division)"));
			
		//Case statement to determine what is selected first
		
		double num1 = Double.parseDouble(JOptionPane.showInputDialog("Please enter a number:"));
		double num2 = Double.parseDouble(JOptionPane.showInputDialog("Please enter a number:"));
			switch (character){
				case 'A':
								if (character == "A")
								{
									answer = num1 + num2;
								}
				break;
				case 'S':
									if (character == "S")
									{
										answer = num1 - num2;
									}
				break;
				case 'M':			
										if (character == "M")
										{
											answer = num1 * num2;
										}
									
				break;
				case 'D':
											if (character == "D")
											{
												answer = num1 / num2;
											}
				break;
					default:
						JOptionPane.showMessageDialog(null, "WARNING","You have not entered the value correctly", JOptionPane.WARNING_MESSAGE);
			}
		} JOptionPane.showMessageDialog(null,"The conversion of number result you have selected is: " + (String.format(choice,2)));
	}  

Sigh... I must be really bothersome at this point but i find it hard to figure out what is wrong even if it looks pretty simple. Sorry for bothering you guys.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10595
  • View blog
  • Posts: 39,246
  • Joined: 27-December 08

Re: Switch Case & char variables

Posted 02 October 2009 - 05:32 PM

You are over-complicating your code a little bit. Let's break it down into words, then an algorithm. And then we'll jump to code. So basically, you want to get three pieces of user input: the operation, and the 2 operands. Based on the user input, you then want to perform the operation then display the result of the program.

So now, let's work on the algoirthm:

0. Begin
1. declare variable operation

2. while operation is not equal to 0, 1, 2 or 3
3.	 operation <-- get input
4. end while

5. num1 <-- get input
6. num2 <-- get input

7. if operation is equal to 0
8.	output num1 + num2
9. end if

10. else if operation is equal to 1
11.	output num1 - num2
12. end if

13. else if operation is equal to 2
14.	output num1 * num2
15. end if

16. else //the operation is then guaranteed to equal 3
17.	if num1 is not equal to 0 and num2 is not equal to 0
18.		  output num1/num2;
19.	end if
20. end else
21. end



Now, looking at this algorithm, try to re-design a class to perform the task outlined. It will be easier to scrap your code and start over because it is a lost cause (no offense) trying to save this code. You will feel torn about not deleting anything vs. implementing an efficient and effective algorithm if you work with your existing code.

Feel free to post if you have any more questions. Good luck!
Was This Post Helpful? 0
  • +
  • -

#7 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Switch Case & char variables

Posted 02 October 2009 - 07:21 PM

View Postpbl, on 2 Oct, 2009 - 03:32 PM, said:

you are swithcing oin a char type so


Blargh! :crazy:

How did I miss the case 1:?!?!
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Switch Case & char variables

Posted 02 October 2009 - 10:30 PM

 switch (character){
				case 'A':
								if (character == "A")
								{
									answer = num1 + num2;
								}


no need to test again in a if if it is a
 switch (character){
				case 'A':
					answer = num1 + num2;
					break;


Was This Post Helpful? 0
  • +
  • -

#9 Kairi  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 107
  • Joined: 02-October 09

Re: Switch Case & char variables

Posted 03 October 2009 - 06:15 AM

Alright if this will help what i am try to get at. I'm doing lab sheets in college so here is the question we are asked

Quote

9. Switch case, and char variables. Write a program that reads in the following values:-
(a) operation desired - A (for addition)
S (for subtraction)
M (for multiplication)
D (for division)
(B) a number
© another number
The program should carry out the operation on the numbers e.g. if the input is M 1.5 3 then the output would be 1.5*3 i.e. 4.500 (The program should display the result of the operation correct to 3 decimal places). Use a switch statement here and have a default case to handle any exceptional circumstances. Use dialogs for all input and output.


Now can ya see where i am trying to get at. I'm just doing a similiar structure as what i did last year. Yet i still get issues. Meaning you declare variables and then message correct which you want to output to user so they know what selection to choose from. For that you need a case statment right. hence my code. Is it that i have to

Here are where my errors are;
1.
choice = Integer.parseInt(JOptionPane.showMessageDialog("A (for subtraction)" + "\nS (for Subtraction)" + "\nM (for multiplication)" +
				"\nD (for division)"));


2Loss of percision is within each of my cases. That is the only issues i got so far. I can see that there is something out in them but i can't be sure as to what. Hope this all helps and hopefully i can get some sort of solution. It's just i find it hard enough juggling my studies in other subjects.

//import packages needed
import javax.swing.*;
import java.awt.*;

public class Math {
	public static void main (String args[]) {
		
		//declare variables
		double message,S,A,M,D; 
		char  character;
		int  answer, choice;
		
		
		//Shows a dialog for the user to input their choice
		choice = Integer.parseInt(JOptionPane.showMessageDialog("A (for subtraction)" + "\nS (for Subtraction)" + "\nM (for multiplication)" +
				"\nD (for division)"));
			
		//Case statement to determine what is selected first
		
		double num1 = Double.parseDouble(JOptionPane.showInputDialog("Please enter a number:"));
		double num2 = Double.parseDouble(JOptionPane.showInputDialog("Please enter a number:"));
			switch (character){
				case 'A':
									answer = num1 + num2;
				break;
				case 'S':
										answer = num1 - num2;
				break;
				case 'M':			
											answer = num1 * num2;
									
				break;
				case 'D':
												answer = num1 / num2;
				break;
					default:
						JOptionPane.showMessageDialog(null, "WARNING","You have not entered the value correctly", JOptionPane.WARNING_MESSAGE);
			}
			JOptionPane.showMessageDialog(null,"The conversion of number result you have selected is: " + (String.format(choice,2)));
		}
	} 

Was This Post Helpful? 0
  • +
  • -

#10 Trilation  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 13-May 06

Re: Switch Case & char variables

Posted 03 October 2009 - 06:54 AM

The loss of precision in your switch statement is caused by the variable called answer.

Since you are working with two double type variables, answer should be of type double not int (unless you plan on type casting all of your results to int) .

Also, what are you using the variable choice for?
It is getting the user's option but in your switch statement you are comparing the variable character(which you assigned nothing to).

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#11 Kairi  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 107
  • Joined: 02-October 09

Re: Switch Case & char variables

Posted 03 October 2009 - 07:33 AM

//import packages needed
import javax.swing.*;
import java.awt.*;

public class Math {
	public static void main (String args[]) {
		
		//decalre variables
		//double answer = " ";
		num1 = 0.0, num2 = 0.0, answer = 0.0; 
	//declares your two numbers and sets them to a sane initial state you weren't doing this last time.
		char mode;
			
		//input statements, you got these right	
		double num1 = Double.parseDouble(JOptionPane.showInputDialog("Please enter number 1:"));
		double num2 = Double.parseDouble(JOptionPane.showInputDialog("Please enter number 2:"));
		
		mode = JOptionPane.showInputDialog("Please enter the mode, \nA for Addition\nS for Subtraction\nM for Multiplication\nD for Division").charAt(0);
		
		
			switch (mode){
				case 'A':
									answer = num1 + num2;
						  break;
				case 'S':
									answer = num1 - num2;
						  break;
				case 'M':			
									answer = num1 * num2;					
						  break;
				case 'D':
									answer = num1 / num2;
						  break;
				default:
							  JOptionPane.showMessageDialog(null, "WARNING","You have not entered the value correctly", JOptionPane.WARNING_MESSAGE);
							  break;
			}
			
			JOptionPane.showMessageDialog(null,"The conversion of number result you have selected is: " + String.format("%.3f The result of the numbers you entered is: " + answer));
		}
	}


For some reason all i am getting now is ';' expected
Was This Post Helpful? 0
  • +
  • -

#12 Trilation  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 13-May 06

Re: Switch Case & char variables

Posted 03 October 2009 - 08:08 AM

You will need to declare num1, num2 and answer to assign it a value.

Change:

		//decalre variables
		//double answer = " ";
		num1 = 0.0, num2 = 0.0, answer = 0.0;



to

		//declare variables
		double num1 = 0.0, num2 = 0.0, answer = 0.0;



and since you're declaring your variables above, remove the word double from:

		double num1 = Double.parseDouble(JOptionPane.showInputDialog("Please enter number 1:"));
		double num2 = Double.parseDouble(JOptionPane.showInputDialog("Please enter number 2:"));



Hope this helps you so far. Good luck!
Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10595
  • View blog
  • Posts: 39,246
  • Joined: 27-December 08

Re: Switch Case & char variables

Posted 03 October 2009 - 08:13 AM

//import packages needed
import javax.swing.*;
import java.awt.*;

public class Math {
	public static void main (String args[]) {

				 /*
					 I added the datatype double next to the variable
					 declarations.  As you had it before, you were assigning
					 values to variables that didn't exist yet.
				  */
		double num1 = 0.0, num2 = 0.0, answer = 0.0;  
		char mode = ''; //initialize char
			
		//Removed datatype from calls to num1 and num2	
		num1 = Double.parseDouble(JOptionPane.showInputDialog("Please enter number 1:"));
		num2 = Double.parseDouble(JOptionPane.showInputDialog("Please enter number 2:"));
		
		mode = JOptionPane.showInputDialog("Please enter the mode, \nA for Addition\nS for Subtraction\nM for Multiplication\nD for Division").charAt(0);
		
		
			switch (mode){
				case 'A':
					answer = num1 + num2;
						break;
				case 'S':
					answer = num1 - num2;
					break;
				case 'M':			
					 answer = num1*num2;					
					 break;
				case 'D':
					answer = num1 / num2;
						 break;
				default:
							  JOptionPane.showMessageDialog(null, "WARNING","You have not entered the value correctly", JOptionPane.WARNING_MESSAGE);
	//you don't need a break in your default case				
			}
			
			JOptionPane.showMessageDialog(null,"The conversion of number result you have selected is: " + String.format("%.3f The result of the numbers you entered is: " + answer));
		}
	}


I've made a few changes to your code and commented them. Try using these adjustments.
Was This Post Helpful? 0
  • +
  • -

#14 Kairi  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 107
  • Joined: 02-October 09

Re: Switch Case & char variables

Posted 03 October 2009 - 08:49 AM

View Postmacosxnerd101, on 3 Oct, 2009 - 07:13 AM, said:

//import packages needed
import javax.swing.*;
import java.awt.*;

public class Math {
	public static void main (String args[]) {

				 /*
					 I added the datatype double next to the variable
					 declarations.  As you had it before, you were assigning
					 values to variables that didn't exist yet.
				  */
		double num1 = 0.0, num2 = 0.0, answer = 0.0;  
		char mode = ''; //initialize char
			
		//Removed datatype from calls to num1 and num2	
		num1 = Double.parseDouble(JOptionPane.showInputDialog("Please enter number 1:"));
		num2 = Double.parseDouble(JOptionPane.showInputDialog("Please enter number 2:"));
		
		mode = JOptionPane.showInputDialog("Please enter the mode, \nA for Addition\nS for Subtraction\nM for Multiplication\nD for Division").charAt(0);
		
		
			switch (mode){
				case 'A':
					answer = num1 + num2;
						break;
				case 'S':
					answer = num1 - num2;
					break;
				case 'M':			
					 answer = num1*num2;					
					 break;
				case 'D':
					answer = num1 / num2;
						 break;
				default:
							  JOptionPane.showMessageDialog(null, "WARNING","You have not entered the value correctly", JOptionPane.WARNING_MESSAGE);
	//you don't need a break in your default case				
			}
			
			JOptionPane.showMessageDialog(null,"The conversion of number result you have selected is: " + String.format("%.3f The result of the numbers you entered is: " + answer));
		}
	}


I've made a few changes to your code and commented them. Try using these adjustments.


Thanks everyone so much for helping me. I'll be sure to ask for more help in my excercises when i come across some problems. So sorry for bothering you guys, i'm so thankful for each of your help B) B) B) B) :D :D :D :D :^: :^: :^: :^:
Was This Post Helpful? 0
  • +
  • -

#15 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Switch Case & char variables

Posted 03 October 2009 - 12:25 PM

View PostKairi, on 3 Oct, 2009 - 09:49 AM, said:

So sorry for bothering you guys,


It's absolutely no trouble. That's what we're here for. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1