14 Replies - 494 Views - Last Post: 26 December 2012 - 06:37 AM Rate Topic: -----

#1 ShawnaInMaine  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 05-May 11

input closes why?

Posted 20 December 2012 - 01:22 PM

When I run my program and enter in the data for age as a letter it closes?

I am using a do while loop to validate the age to ensure the input is between 0-120 and is a number.

I ran my program and entered the first time for testing purposes
125

The input dialog box opened again asking for age so i tried typing

hs

and the whole input dialog box closes.

Where did I go wrong?

    
        //panel2 age  input dialog
         do
         {
         strInput = JOptionPane.showInputDialog(null, "Enter the persons "
                 + "age.");
         age = Integer.parseInt(strInput);
        
         //validate age entry between 0-120  input dialog
         }
         while(age < 0 || age > 120 || strInput.isEmpty());{
           JOptionPane.showMessageDialog(null, "Please enter an age between "
                   + "0 and 120!");
      }
          


Is This A Good Question/Topic? 0
  • +

Replies To: input closes why?

#2 DarenR  Icon User is online

  • D.I.C Lover

Reputation: 387
  • View blog
  • Posts: 2,669
  • Joined: 12-January 10

Re: input closes why?

Posted 20 December 2012 - 01:31 PM

it is erroring out because it cant translate letters into numbers



you need a catch in there for those items so it wont shut down.

in the catch you will have to put code in so if it is a letter the prompt willl com back up.

This post has been edited by DarenR: 20 December 2012 - 02:05 PM

Was This Post Helpful? 0
  • +
  • -

#3 ShawnaInMaine  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 05-May 11

Re: input closes why?

Posted 20 December 2012 - 01:39 PM

What do you mean here.....

[s]int he[/s
] catch you will have to put code in so if it is a letter the prompt willl com back up.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2890
  • View blog
  • Posts: 9,599
  • Joined: 12-December 12

Re: input closes why?

Posted 20 December 2012 - 01:41 PM

This function looks suitable:

public boolean isInteger( String input )  
{  
   try  
   {  
      Integer.parseInt( input );  
      return true;  
   }  
   catch( Exception )  
   {  
      return false;  
   }  
}

Usage: if (isInteger(strInput)) {}

This post has been edited by andrewsw: 20 December 2012 - 01:43 PM

Was This Post Helpful? -1
  • +
  • -

#5 ShawnaInMaine  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 05-May 11

Re: input closes why?

Posted 20 December 2012 - 02:02 PM

I feel like a try catch statement is adding wayyy too much code I thought i had read that we could validate integers by using != "[A-Za-z]" but maybe I am wrong.
Was This Post Helpful? -1
  • +
  • -

#6 DarenR  Icon User is online

  • D.I.C Lover

Reputation: 387
  • View blog
  • Posts: 2,669
  • Joined: 12-January 10

Re: input closes why?

Posted 20 December 2012 - 02:05 PM

View PostShawnaInMaine, on 20 December 2012 - 03:39 PM, said:

What do you mean here.....

[s]int he[/s
] catch you will have to put code in so if it is a letter the prompt willl com back up.

in the
Was This Post Helpful? 0
  • +
  • -

#7 DarenR  Icon User is online

  • D.I.C Lover

Reputation: 387
  • View blog
  • Posts: 2,669
  • Joined: 12-January 10

Re: input closes why?

Posted 20 December 2012 - 02:15 PM

you can also do it in an if statement.

 do 

03    {  

04    strInput = JOptionPane.showInputDialog(null, "Enter the persons " 

05            + "age.");  

06    age = Integer.parseInt(strInput); 


If Not IsNumeric(strInput) Then
        
JOptionPane.showMessageDialog(null, "Input must be a number")


JOptionPane.showMessageDialog(null, "Please enter an age between +0 and 120!"); 


07      

08    //validate age entry between 0-120  input dialog  

09   else

10    while(age < 0 || age > 120 || strInput.isEmpty());{  

11      JOptionPane.showMessageDialog(null, "Please enter an age between " 

12              + "0 and 120!");  


}
13 }  




something like that might work--- havent had a chance to test it so you will have to do that but you get the idea where it is going from there.

This post has been edited by DarenR: 20 December 2012 - 02:15 PM

Was This Post Helpful? 0
  • +
  • -

#8 ShawnaInMaine  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 05-May 11

Re: input closes why?

Posted 20 December 2012 - 02:22 PM

Why cant I use something like this:

do
         { 
         strInput = JOptionPane.showInputDialog(null, "Enter the persons "
                 + "age.");
         age = Integer.parseInt(strInput);
         
       
     
         }
         
         
         //validate age entry between 0-120  input dialog
         while(age < 0 || age > 120 || strInput.isEmpty() || 
                 strInput == "[A-Za-z]");{

           JOptionPane.showMessageDialog(null, "Please enter an age between "
                   + "0 and 120!");
      }


View PostDarenR, on 20 December 2012 - 02:15 PM, said:

you can also do it in an if statement.

 do 

03    {  

04    strInput = JOptionPane.showInputDialog(null, "Enter the persons " 

05            + "age.");  

06    age = Integer.parseInt(strInput); 


If Not IsNumeric(strInput) Then
        
JOptionPane.showMessageDialog(null, "Input must be a number")


JOptionPane.showMessageDialog(null, "Please enter an age between +0 and 120!"); 


07      

08    //validate age entry between 0-120  input dialog  

09   else

10    while(age < 0 || age > 120 || strInput.isEmpty());{  

11      JOptionPane.showMessageDialog(null, "Please enter an age between " 

12              + "0 and 120!");  


}
13 }  




something like that might work--- havent had a chance to test it so you will have to do that but you get the idea where it is going from there.




If THEN??? I do not think an If Then statement is even compatible with java?! :o
Was This Post Helpful? 0
  • +
  • -

#9 DarenR  Icon User is online

  • D.I.C Lover

Reputation: 387
  • View blog
  • Posts: 2,669
  • Joined: 12-January 10

Re: input closes why?

Posted 20 December 2012 - 02:22 PM

did you try it?
Was This Post Helpful? 0
  • +
  • -

#10 ShawnaInMaine  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 05-May 11

Re: input closes why?

Posted 20 December 2012 - 02:23 PM

View PostDarenR, on 20 December 2012 - 02:15 PM, said:

you can also do it in an if statement.

 do 

03    {  

04    strInput = JOptionPane.showInputDialog(null, "Enter the persons " 

05            + "age.");  

06    age = Integer.parseInt(strInput); 


If Not IsNumeric(strInput) Then
        
JOptionPane.showMessageDialog(null, "Input must be a number")


JOptionPane.showMessageDialog(null, "Please enter an age between +0 and 120!"); 


07      

08    //validate age entry between 0-120  input dialog  

09   else

10    while(age < 0 || age > 120 || strInput.isEmpty());{  

11      JOptionPane.showMessageDialog(null, "Please enter an age between " 

12              + "0 and 120!");  


}
13 }  




something like that might work--- havent had a chance to test it so you will have to do that but you get the idea where it is going from there.




If THEN??? I do not think an If Then statement is even compatible with java?! :o

Yes I did try using "[A-Za-z]" but the program still closes.
Was This Post Helpful? 0
  • +
  • -

#11 DarenR  Icon User is online

  • D.I.C Lover

Reputation: 387
  • View blog
  • Posts: 2,669
  • Joined: 12-January 10

Re: input closes why?

Posted 20 December 2012 - 02:27 PM

you are correct--- my forums was showing this in a vb.net forum-- weird..

let me re-evaluate your program give me a few seconds

use this type of catch


public boolean isInteger( String input )   
{   
   try  
   {   
      Integer.parseInt( strInput );   
      return true;   
   }   
   catch( Exception e)   
   {   
      return false;   
   }   
}  




the above will catch all non numbers
Was This Post Helpful? 0
  • +
  • -

#12 ShawnaInMaine  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 05-May 11

Re: input closes why?

Posted 20 December 2012 - 02:36 PM

Still not working :(

Maybe I missed something somewhere

Heres my code

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package contactinformationprogram;

import java.io.*;
import javax.swing.*;
import java.util.regex.Pattern;
/**
 *
 * @author T-Ali
 */
public class ContactInformationProgram {

    /**
     * Part 1
     * Design, implement, test, and debug a program with a JFrame that allows 
     * the user to enter a series of contacts’ names, ages, e-mail addresses,
     * and cell phone numbers, and creates a file from the entered data.
     * Validate the age entry to ensure that it is numeric and between 0 and 
     * 120. Include information for three to five contacts.
     * Part 2   --pg 831 of chapter 12
     * Design, implement, test, and debug a program that reads the file you 
     * created by the list in Part 1 and displays the records in a JFrame. You
     * may either display all entries in the list at once or display them one
     * at a time; the user interface is up to you. Protect against not being
     * able to open the file.

     */
    

        String strInput;
     
    public ContactInformationProgram()
    {
                 //jframe
            final int width = 1400,
                    height = 900;
            
            JFrame window = new JFrame ("Contact Information Program");
            
            //set size
            window.setSize(width, height);
            
            //specify what happens when x is clicked
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            //display the window
            window.setVisible(true);
            
        // TODO code application logic here
            //variables
      String name;
      String phone ="";

   
      int repeat;       
      int age;  
      String email ="";
      BufferedWriter out;
       
          
    do {
   

        // name input dialog
      
         name = JOptionPane.showInputDialog(null, "Enter name of  person."
                 + "");
        
       
    
        // age  input dialog
         do
         { 
         strInput = JOptionPane.showInputDialog(null, "Enter the persons "
                 + "age.");
         age = Integer.parseInt(strInput);
         if (isInteger(strInput)) {}

         }
         
         
         //validate age entry between 0-120  input dialog
         while(age < 0 || age > 120 || strInput.isEmpty());{

           JOptionPane.showMessageDialog(null, "Please enter an age between "
                   + "0 and 120!");
      }
          
   
                
        // email address  input dialog
        email = JOptionPane.showInputDialog(null, "Enter Email address.");    
        
        // phone numbers  input dialog
        phone = JOptionPane.showInputDialog(null, "Enter Phone Number.");
        
        
      
        
      
         //did the user need another person added?
              repeat = JOptionPane.showConfirmDialog(null, "Would you like to"
                 + " enter another person's data?", "Please Confirm.", 
                 JOptionPane.YES_NO_OPTION);
              
                  //create file from entered data
    }while (repeat==JOptionPane.YES_OPTION);
      
    
    
     try {
           out = new BufferedWriter(new FileWriter("ContactData.txt", true));
               
           out.write(name + " ");
           out.write(age + " ");
           out.write(email + " ");
           out.write(phone + " " );
           out.newLine();
           out.close();
 } catch (IOException e) {
                System.out.println("There was a problem:" + e);
    }

        
    

    
                

   
}

      public boolean isInteger( String input )   
{   
   try  
   {   
      Integer.parseInt(strInput);   
      return true;   
   }   
   catch( Exception e)   
   {   
      return false;   
   }   
}  
  
  
         public static void main(String[] args) {   
       
        
       new ContactInformationProgram ();
        
        
        
    }
}


Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: input closes why?

Posted 23 December 2012 - 09:25 PM

because Integer.parseInt() has throwed an Exception well before you do the check

This post has been edited by pbl: 24 December 2012 - 10:12 AM

Was This Post Helpful? 0
  • +
  • -

#14 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2560
  • View blog
  • Posts: 10,690
  • Joined: 20-September 08

Re: input closes why?

Posted 25 December 2012 - 01:55 PM

Catching an Exception is a perfectly good way of ensuring that numeric input is not malformed, e.g.

     age = -1;
            do {
                strInput = JOptionPane.showInputDialog(null,
                        "Enter the persons " + "age.");

                try {
                    age = Integer.parseInt(strInput);
                } catch (NumberFormatException e) {
                    JOptionPane.showMessageDialog(null,
                        "Please enter an age between " + "0 and 120!");

                    // Ignore and loop
                }
            } //validate age entry between 0-120  input dialog
             while ((age < 0) || (age > 120));

This post has been edited by g00se: 25 December 2012 - 01:56 PM

Was This Post Helpful? 0
  • +
  • -

#15 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: input closes why?

Posted 26 December 2012 - 06:37 AM

Methods are your friends.
int getAge() {
	final int minAge = 0, maxAge = 120; // no magic numbers.  we use these a few times.
	int age;
	while(true) { // loop until logic inside says we're done
		String s = JOptionPane.showInputDialog(null, "Enter the persons age.");
		try {
			age = Integer.parseInt(s);
			// got this far, it's an int
			if (age < minAge || age > maxAge) {
				JOptionPane.showMessageDialog(null,
					"Please enter an age between " + minAge + " and " + maxAge + "!"
				);
			} else {
				break;  // correct answer entered, leave
			}
		} catch (NumberFormatException e) {
			JOptionPane.showMessageDialog(null, "An integer is required");
		}
	}
	return age;
}



That said, it looks like you're failing the assigment. If you are supposed to use a JFrame then you're probably not supposed to use a JOptionPane.showInputDialog for input.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1