my type program

doubles, ints, strings

Page 1 of 1

1 Replies - 1563 Views - Last Post: 06 March 2008 - 08:14 PM Rate Topic: -----

#1 trakan   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-March 08

my type program

Post icon  Posted 06 March 2008 - 06:27 PM

I am writing a program that differentiates among doubles, ints, strings and other data types. Beginning with a try statement the program should allow users to choose a data type, then based on a switch statement and several case statements, the program should prompt the user to enter a value that would fit that data type.

Errors:
59: 'catch' without 'try'
59: ')' expected
59: not a statement
59: ';' expected
65: reached end of file while parsing

the code:

import java.io.*;
import javax.swing.JOptionPane;

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

		while (!done)
		{
			try
			{
				String answer = JOptionPane.showInputDialog(null, "What's my type\n\n\n1) String\n2) integer\n3) double\n4) Quit the program");
				choice = Integer.parseInt(strChoice);

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

					case 2:
						JOptionPane.showMessageDialog(null, "Correct!");
						tryInt = Integer.parseInt(strChoice);
						break;

					case 3:
						JOptionPane.showMessageDialog(null, "Correct!");
						tryDouble = Integer.parseInt(strChoice);
						break;

					case 4:
						done = true;
						JOptionPane.showMessageDialog(null, "Exit.");
						System.exit(0);
						break;
					default:  throw new NumberFormatException();
		}

				  catch(NumberFormatException e)
			{
				JOptionPane.showMessageDialog(null, "Invalid, try again.");
			}
		}
	}
}




I am very new to java and the only prior programming experience I had is vb6.

My current java teacher is making us learn it out of the book and I am lost.

Is This A Good Question/Topic? 0
  • +

Replies To: my type program

#2 quim   User is offline

  • D.I.C Head
  • member icon

Reputation: 18
  • View blog
  • Posts: 182
  • Joined: 11-December 05

Re: my type program

Posted 06 March 2008 - 08:14 PM

it's saying that this catch block of code does not have it's try block of code
catch(NumberFormatException e)
            {
                JOptionPane.showMessageDialog(null, "Invalid, try again.");
            }

Delete this and write the catch block of code right after the try block of code like this:
try
{
    /*try this code if exception
     1 get input
     2 parse
     3 check if format is GOOD
     4 thrown exception if NOT
     5 switch( choice )
     { 
          // check type and 
          //as you can see the whole code goes inside the try block and if 
         // error exception is thrown and the catch block will handle it
     }*/
}
catch( ExceptionClass e )
{
    // error message
}


also check this section
strChoice is not initialized -- i think what you wanted to put parse is the answer

                
String answer = JOptionPane.showInputDialog(null, "What's my type\n\n\n1) String\n2) integer\n3) double\n4) Quit the program");
                choice = Integer.parseInt(strChoice);

This post has been edited by quim: 06 March 2008 - 08:26 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1