CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2010
    Posts
    81

    Program not accepting input?

    Here we go again... I never should have taken C++ and Java at the same time. After 5 hours of trying to figure out what's wrong with this program... I'm asking for a second set of eyes. I'm sure its dumb, but please give me a hint as to where my problem is so I can fix it. The sickening part is, the book TELLS you the code. I swear I followed all instructions.... The book never mentions initializing the variables. It LITERALLY says: "Type String strChoice, strTryString, strTryInt, strTryDouble;" ... last time I checked, those need to be initialized. And, curiously enough, it has me declare strTryString, yet never use it.

    The problem is, it throws the exception, no matter what I enter, and never lets anything else happen... and I could swear that I have my switch case correct.

    PLEASE HELP!

    Code:
    /*
       Chapter 4:	Programming Assignment 2
       Programmer:	Ben ********
       Date:		04/01/11
       Filename:	MyType.java
       Purpose:
    */
    
    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
    			{
    				JOptionPane.showInputDialog("Ben ********   04/01/2011\n\n" +
    				"What's My Type?\n\n" +
    				"1) String\n" +
    				"2) Integer\n" +
    				"3) Double\n" +
    				"4) Quit the program\n");
    				choice = Integer.parseInt(strChoice);
    
    				switch(choice)
    				{
    					case 1:
    						JOptionPane.showMessageDialog(null,"Ben ********  04/01/11\n\n" +
    						"The data type you entered is correct.");
    						break;
    					case 2:
    						tryInt = Integer.parseInt(strTryInt);
    						JOptionPane.showMessageDialog(null,"Ben ********  04/01/11\n\n" +
    						"The data type you entered is correct.");
    						break;
    					case 3:
    						tryDouble = Double.parseDouble(strTryDouble);
    						JOptionPane.showMessageDialog(null,"Ben ********  04/01/11\n\n" +
    						"The data type you entered is correct.");
    						break;
    					case 4:
    						done = true;
    						break;
    					default:
    						throw new NumberFormatException();
    				}
    			}
    			catch (NumberFormatException e)
    			{
    				JOptionPane.showMessageDialog(null,"Ben ********   04/01/11\n" +
    				"You entered an incorrect data type.");
    			}
    		}
    	}
    }

  2. #2
    Join Date
    Oct 2008
    Posts
    50

    Re: Program not accepting input?

    First, doing:
    Type String strChoice, strTryString, strTryInt, strTryDouble;
    Is fine. They do need to be initialised before being used, but they don't NEED to be initialised the moment they are declared.
    The problem here is probably that you had an error about using a string without it being initialised and thought "hmm... I'll just initialise them to empty string, that'll solve the problem!" when the real problem was that you never actually put anything in those strings! So you initialise them to "", but since you never change the value, you try to call Integer.parseInt() on "".

    JOptionPane.showInputDialog() returns a string. It doesn't magically put the result in a string, you have to specify where to put its return value. So you should be doing:
    Code:
    strChoice = JOptionPane.showInputDialog("Ben Brotsker   04/01/2011\n\n" +
    				"What's My Type?\n\n" +
    				"1) String\n" +
    				"2) Integer\n" +
    				"3) Double\n" +
    				"4) Quit the program\n");
    Similarly, when does the person ever enter a value for strTryInt and strTryDouble? It looks like your program is missing a pretty big chunck.

  3. #3
    Join Date
    Nov 2010
    Posts
    81

    Re: Program not accepting input?

    Thanks! I actually just realized that same thing myself and was coming back here to change my post. But yea... Java is pretty different than c++ (which I've gotten much better at than java) and it keeps screwing me up!

    I see that I need to assign all that stuff now, if I have any other problems I'll post them up! Thanks again.

  4. #4
    Join Date
    Nov 2010
    Posts
    81

    Re: Program not accepting input?

    OK... I got it. It runs, but I'll submit it here so that it can help others.

    Code:
    /*
       Chapter 4:	Programming Assignment 2
       Programmer:	Ben B*******
       Date:		04/01/11
       Filename:	MyType.java
       Purpose:
    */
    
    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
    			{
    				strChoice = JOptionPane.showInputDialog("Ben B*******   04/01/2011\n\n" +
    				"What's My Type?\n\n" +
    				"1) String\n" +
    				"2) Integer\n" +
    				"3) Double\n" +
    				"4) Quit the program\n");
    				choice = Integer.parseInt(strChoice);
    
    				switch(choice)
    				{
    					case 1:
    						strTryString = JOptionPane.showInputDialog("Ben B*******   04/01/2011\n\n" +
    						"Please enter a string data type.");
    
    						JOptionPane.showMessageDialog(null,"Ben B*******  04/01/11\n\n" +
    						"The data type you entered is correct.");
    						break;
    					case 2:
    						strTryInt = JOptionPane.showInputDialog("Ben B*******   04/01/2011\n\n" +
    						"Please enter an integer data type.");
    
    						tryInt = Integer.parseInt(strTryInt);
    						JOptionPane.showMessageDialog(null,"Ben B*******  04/01/11\n\n" +
    						"The data type you entered is correct.");
    						break;
    					case 3:
    						strTryDouble = JOptionPane.showInputDialog("Ben B*******   04/01/2011\n\n" +
    						"Please enter a double data type.");
    
    						tryDouble = Double.parseDouble(strTryDouble);
    						JOptionPane.showMessageDialog(null,"Ben B*******  04/01/11\n\n" +
    						"The data type you entered is correct.");
    						break;
    					case 4:
    						JOptionPane.showMessageDialog(null,"Ben B*******  04/01/11\n\n" +
    						"Thank you for using the data type validation program.");
    						done = true;
    						break;
    					default:
    						throw new NumberFormatException();
    				}
    			}
    			catch (NumberFormatException e)
    			{
    				JOptionPane.showMessageDialog(null,"Ben B*******   04/01/11\n\n" +
    				"You entered an incorrect data type.\n" +
    				"Please try again.");
    			}
    		}
    	}
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured