4 Replies - 14122 Views - Last Post: 07 July 2008 - 10:37 AM Rate Topic: -----

#1 trayyrho  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 10-October 07

using switch and try statements to validate user input

Posted 16 October 2007 - 02:50 PM

Hi, I new to this stuff and have been working very hard to complete this assignment. I had it almost completed but screwed it up and can't fix what I believe to be a simple error in my code. When I compile, I get the following error: ....
MyType. Java:68: '}' expected } 1 error
total completed with exit code 1


Thanks in advance




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.");
			}
		}
	}
}
>>edit: code tags PB

This post has been edited by PennyBoki: 16 October 2007 - 03:04 PM


Is This A Good Question/Topic? 0
  • +

Replies To: using switch and try statements to validate user input

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4337
  • View blog
  • Posts: 12,137
  • Joined: 18-April 07

Re: using switch and try statements to validate user input

Posted 16 October 2007 - 03:48 PM

Delete the first "{" you come to from the bottom of your script. It is on the line above your JOptionPane. I wanted to also mention to you that your second catch statement is useless here because it is catching the same error type as the one above it. Java will only go to the first catch block that can handle the error, so your second one won't fire. It is just dead weight here.

This should solve your problems me thinkz. :)
Was This Post Helpful? 0
  • +
  • -

#3 trayyrho  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 10-October 07

Re: using switch and try statements to validate user input

Posted 26 October 2007 - 05:50 PM

I forgot to say thanks.




View PostMartyr2, on 16 Oct, 2007 - 03:48 PM, said:

Delete the first "{" you come to from the bottom of your script. It is on the line above your JOptionPane. I wanted to also mention to you that your second catch statement is useless here because it is catching the same error type as the one above it. Java will only go to the first catch block that can handle the error, so your second one won't fire. It is just dead weight here.

This should solve your problems me thinkz. :)

Was This Post Helpful? 0
  • +
  • -

#4 Rozi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 07-July 08

Re: using switch and try statements to validate user input

Posted 07 July 2008 - 10:09 AM

Seems like we are busy with the same thing!!! Seems like you doing much much better than I am, but mabe if you look at my horrible code you get your sorted! ...I am busy with studies using Shelly Cashman Series Java Programing...seems like you doing the same. Could you please e-mail me your details, and maybe we can do this together...I hope to have this one fixed before tonight...will post it for you if I get it done...the other { idea not taking care of all the errors.

my code at this stage (still full of errors) Sorry, could not attache my file, so copied it in.

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

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

//get choice
public static int getChoice()
{
//choice = Integer.parseInt(strChoice);
int choice = 0;
boolean done = false;

//loop while not done(cancel button)
while(!done)
{
String answer = JOptionPane.showInputDialog(null);
if (strChoice == null) System.exit(0);
else done = true;
}


//loop while not done
while (!done)
{
//String answer = JOptionPane.showInputDialog(null, "Enter the data type code\n click cansel or 4 to exit"

try
{
String message = " pick data type: " + "\n\n1) String\n2) integer\n3) double\n4) Quit the program\n\n";

choice = Integer.parseInt(JOptionPane.showInputDialog(null,message));

//test for valid codes
//if (choice == null||choice == 4) System.exit(0);
//else

if (choice<1 || choice>4) throw new NumberFormatException();
else done = true;
}
catch(NumberFormatException e)
{
JOptionPane.showMessageDialog(null, "Please Enter 1,2,3 or 4.", "Error",JOptionPane.INFORMATION_MESSAGE);
}
}
return choice;

choice = Integer.parseInt(strChoice);

while (!done)
try
{
switch(choice)
{

case 1:
JOptionPane.showMessageDialog("correct");
break;
case 2:
Integer.parseInt(tryInt);
JOptionPane.showMessageDialog("correct");
break;
case 3:
Integer.parseInt(tryDouble);
JOptionPane.showMessageDialog("correct");
break;
case 4:
done = true;
JOptionPane.showMessageDialog("Thank you for using My Type program");
break;
default:
throw new NumberFormatException();
}
}
catch(numberFormatException e)
{
JOptionPane.showMessageDialog(null,"Please try again, the data you have enterd is not of the corect data type","Error",JOptionPane.INFORMATION_MESSAGE);
}
}
}


/*

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 g)
{
JOptionPane.showMessageDialog(null, "Invalid, try again.");
}
}
}
}

This post has been edited by Rozi: 07 July 2008 - 10:17 AM

Was This Post Helpful? 0
  • +
  • -

#5 lordms12  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 30
  • View blog
  • Posts: 339
  • Joined: 16-February 08

Re: using switch and try statements to validate user input

Posted 07 July 2008 - 10:37 AM

View PostRozi, on 7 Jul, 2008 - 10:09 AM, said:

Seems like we are busy with the same thing!!! Seems like you doing much much better than I am, but mabe if you look at my horrible code you get your sorted! ...I am busy with studies using Shelly Cashman Series Java Programing...seems like you doing the same. Could you please e-mail me your details, and maybe we can do this together...I hope to have this one fixed before tonight...will post it for you if I get it done...the other { idea not taking care of all the errors.

my code at this stage (still full of errors) Sorry, could not attache my file, so copied it in.

1- Start your own tread after you get sure that your question was not asked before.
2- Use Posted Image to post your code.
3- Specify your question clearly.
4- Do not ask us to mail you or put deadlines to answer.

I will glad to help you but I do not know what is your problem and the code you posted is full with syntax errors!!!, you can use eclipse it is a good IDE.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1