• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Setting a Range

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have an input that request a 3-digit number. I want it to be within a range. How can i set a range for the input?

Thanks,
ToDi1010
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You probably need come up with the logic to implement this. I can't think of anything in the Java API that will implement this directly. What do you want the program to do if the user enters an "invalid" number? From there you should come up with some sequence of steps to make this happen. At first, don't worry about writing it in Java. Most people find it more natural to explain the steps to take in their native language and then translating that into the programming language they are using.

Also, can you explain a little more about your program so we can assist you further? Mostly I am interested whether this is a console application or a GUI?

So once you've answered these questions, please post some more details about what you have done to try to solve this. I'm sure someone here will be more than happy to help fill in any details you need.

Keep coding!

Layne
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch, Tony!

Show us yer code, pardner. It'll help folks 'round these parts to nudge you in the right direction.

When posting code, please be sure to surround the code with the [code] and [/code] UBB Tags. This will help to preserve the formatting of the code, thus making it easier to read and understand.
 
Tony Spedaliere
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
package dawson111.labExercises;

import javax.swing.*;

public class TelephoneNumber
{
public static void main(String[] args)
{
//variable declarations
String inputString;
int firstThree > 221; //Beginning of Range
int lastFour < 993; //End of Range

//Accept the required data
inputString =
JOptionPane.showInputDialog("Enter the first three digits:");
firstThree = Integer.parseInt(inputString);
inputString =
JOptionPane.showInputDialog("Enter the last four digits:");
lastFour = Integer.parseInt(inputString);


//Display the output
System.out.println("************** Telephone Number ****************");
System.out.println();
System.out.println("Telephone Number: (514)" + firstThree +"-"+ lastFour);

}//end main method
}//end Average class
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(Clearing throat...)

Originally posted by Dirk Schreckmann:
When posting code, please be sure to surround the code with the [code] and [/code] UBB Tags. This will help to preserve the formatting of the code, thus making it easier to read and understand.



Note that you can edit your own posts. Just click on the icon that looks like a piece of paper above the post.
[ September 22, 2004: Message edited by: Dirk Schreckmann ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It occurs to me that you could be thinking about doing one of a couple different things.

First Scenario: After the user submits data through the dialog box, your program examines the input data. If the data is not of the proper format or not within the correct range, your program then perhaps lets the user know of the mistake, and requests new input. This process would repeat until valid data were input.

Second Scenario: The user is only able to submit proper data to begin with. The input component simply wouldn't let the user input invalid data.

So, which are you wanting to achieve? (Sometimes, I like to pick the easier-to-implement solution.)
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving this to the Swing / AWT / SWT / JFace forum...
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This might be one way (not sure how you wanted lastFour - is 0001 invalid?)

 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic