3 Replies - 1921 Views - Last Post: 12 November 2009 - 10:38 PM Rate Topic: -----

#1 chengeto  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 23-September 09

Input validation using methods

Posted 12 November 2009 - 09:05 PM

This is a programs which basically validates the input put by a user. I don't understand why when l enter a number out of the desired range the program does not prompt me to enter again because my input as invalid ?
import javax.swing.*;

public class validation
{
public static void main ( String [] args )
{

String quantity = JOptionPane.showInputDialog("Enter the quantity");
}


public static int getIntInRange(String quantity){
			boolean keepGoing = true;
			int range=0;
			String input;

			while (keepGoing){
				input= JOptionPane.showInputDialog(quantity);
				range = Integer.parseInt(input);

				if (range>100 || range<0){
					JOptionPane.showMessageDialog(null,
					"Sorry--that's not in range.");
									input= JOptionPane.showInputDialog(quantity);
				range = Integer.parseInt(input);
				}
				else{
					keepGoing = false;
				}
			}//close while
			return guess;
		}//close getInt
	} 

This post has been edited by chengeto: 12 November 2009 - 09:06 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Input validation using methods

#2 pbl  Icon User is offline

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

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Input validation using methods

Posted 12 November 2009 - 09:31 PM

Sure you get your quantity as a String here

String quantity = JOptionPane.showInputDialog("Enter the quantity");

but where do you call getIntInRange() ?
Was This Post Helpful? 0
  • +
  • -

#3 NoobProgramer  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 191
  • Joined: 15-June 09

Re: Input validation using methods

Posted 12 November 2009 - 09:42 PM

View Postchengeto, on 12 Nov, 2009 - 08:05 PM, said:

This is a programs which basically validates the input put by a user. I don't understand why when l enter a number out of the desired range the program does not prompt me to enter again because my input as invalid ?
import javax.swing.*;

public class validation
{
public static void main ( String [] args )
{

String quantity = JOptionPane.showInputDialog("Enter the quantity");
}


public static int getIntInRange(String quantity){
			boolean keepGoing = true;
			int range=0;
			String input;

			while (keepGoing){
				input= JOptionPane.showInputDialog(quantity);
				range = Integer.parseInt(input);

				if (range>100 || range<0){
					JOptionPane.showMessageDialog(null,
					"Sorry--that's not in range.");
									input= JOptionPane.showInputDialog(quantity);
				range = Integer.parseInt(input);
				}
				else{
					keepGoing = false;
				}
			}//close while
			return guess;}//close getInt
	} 



im just wondering about that
return guess;

because there's no such word "guess" at ur codes i mean program.

This post has been edited by NoobProgramer: 12 November 2009 - 09:43 PM

Was This Post Helpful? 0
  • +
  • -

#4 chengeto  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 23-September 09

Re: Input validation using methods

Posted 12 November 2009 - 10:38 PM

View Postpbl, on 12 Nov, 2009 - 08:31 PM, said:

Sure you get your quantity as a String here

String quantity = JOptionPane.showInputDialog("Enter the quantity");

but where do you call getIntInRange() ?




Thanks for the help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1