aspose file tools*
The moose likes Java in General and the fly likes Case Statement Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Case Statement Problem" Watch "Case Statement Problem" New topic
Author

Case Statement Problem

Justin Mosley
Greenhorn

Joined: Sep 04, 2007
Posts: 4
Hey all,

I am working on an assignment and need to do the following, but the book really doesn't give a good explanation on how to do it. Any help would be much appreciated.

Case 1: If the user enters a 1, display a message that informs users they are correct, as any input can be saved as a String.
Case 2: If the user enters a 2, parse the value into tryInt. Display a message that informs users they are correct.
Case 3: If the user enters a 3, parse the value into tryDouble. Display a message that informs the user they are correct.
Case 4: Set done equal to true.


Here is what my code looks like right now. I would attach it as a zip file, but it looks like I can't.

/*
Chapter 4: Switch and Try Statements
Programmer: Justin Mosley
Date: Sept 12, 2007
Filename: MyType.java
Purpose: This program helps beginning programmers understand data types.
*/
import java.io.*;
import javax.swing.JOptionPane;

public class MyType
{
public static void main(String[] args)
{
//declare class variables
String strChoice, strTryString, strTryInt, strTryDouble;
int choice, tryInt;
double tryDouble;
boolean done = false;
}

//loop while not done
while (!done)
{
try
{
String message = "What's My Type?" + "\n\n1)String\n2)integer\n3)double\n4)Quit the program\n\n";

choice = Integer.parseInt(strChoice);

//test for valid codes 1, 2, 3, or 4
if (choice<1 || code>4) throw new NumberFormatException();
else done = true;
}
catch(NumberFormatException e)
{
JOptionPane.showMessageDialog(null, "Please enter a 1, 2, 3, or 4", "Error", JOptionPane.INFORMATION_MESSAGE);
}
switch(choice)
{
case 1:
break;

case 2:
break;

case 3:
break;

}
}
mohsen fakhari
Greenhorn

Joined: May 04, 2003
Posts: 25
Don't you want to tell us what exactly your problem is?
Beside that indentation and java style bracing make your code much easier to read
poorna prakash parvathala
Greenhorn

Joined: Dec 18, 2006
Posts: 20
Hi Justin
As per my understanding this forum will help you and assist you with problems/issues
and do not write the code for you.
you can reframe your question like for asking the ways to do it .


Poorna Prakash<br /> <br />SCJP 1.4 (90%), SCWCD (planning)
Jinny Morris
Ranch Hand

Joined: Apr 29, 2007
Posts: 101
Justin -

For a quick assist, try the Sun tutorial on the switch/case statement

It gives several examples of different things you can do with your "cases"
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
The general solution to your problem, as stated, is to code the problem using beginner methods. You posted in intermediate, so I coded the question using intermediate skills - which may not have been discussed in your book.

If you can make it through this, you will have discovered that programming in a strongly typed language makes some tasks harder, later you will discover that it makes many error avenues easier to detect early on in writing your program.

It's mostly a matter of syle and skill, but the skills have to be learned.

See: http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Java/Chapter09/consoleTextIn.html





If you get , just come back and post.

[ ps: read the Integer class to get the parse int stuff - my code was to address the narrow issue of using a case statement .... you will have to tediously walk through it to examine your loop logic --- this just gives you something to work with. ]

Also, be very careful with switch constructs, they require advanced skills to avoid several nasty pitfalls, but are my first choice for many challenges because of reasons you will learn later.

[ The exact done == true will have to be examined by you ]
[ September 18, 2007: Message edited by: Nicholas Jordan ]

"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
Jinny Morris
Ranch Hand

Joined: Apr 29, 2007
Posts: 101
Nicholas -

One of the cool things about wandering around this site is all the GREAT links to material that ranchers know! Thanks very much for a new one!
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
[Original Poster:]One of the cool things about wandering around this site is all the GREAT links to material that ranchers know! Thanks very much for a new one!

There are many who have helped me.

Just keep coding, compiling and fixing, you will get there.

Read up on break in switch statements and default in switch statements:

switch( val ) // val has to be readable by the compiler at compile time as a numeric.

switch(){

case 1:
case 2:
doSomething();


both go to the same place, and without a break just keep on going down the list of things that are in the statement. Sometimes it is very helpful to think of it as a jump, sometimes called a jump table - but without the breaks ( which are easy to leave out ) it just keeps on running down the jump table.

Now, what happens if you wanted the jump table to fall into a certain point and keep going like the energizer bunny, but some helpful Harvey comes along later and inserts break statements ?

Not only that, the numeric values of the switch cases do not have to be in numeric order, it is just that no two can evaluate to the same numeric value. In other words, I could have, according to compiler science, coded the first case as 0x20 or '\u0020\ or '   ' but I did not want you to get swamped thinking about ascii/dbcs/int conversion.

So which one is easiest to read ? It all depends on how much code you have written, and the switch could as well have coded the last case first - or in any order - but one thing at a time.

An extremely useful code snippet to place in your work at your skill level is:

char c = \u0020\;// numeric value of spacebar.
while( ++c < \u007f\ )System.out.println(new String(c));//

and from there, you can make your own ascii translator that gives numeric values for keyboard characters:





be sure to place loop controls in all loops
[ September 18, 2007: Message edited by: Nicholas Jordan ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Case Statement Problem