• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Case Statement Problem

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;

}
}
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 .
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Jinny Morris
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic