Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > J2EE
|
J2EE General J2EE (Java 2 Enterprise Edition) discussions. Questions not specific to EE will be redirected elsewhere.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the J2EE section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 7th, 2003, 10:13 PM
Registered User
 
Join Date: Jul 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Get unlimited input from user WO using sentinel #

im writing a simple program in java SE, in this program i need to get input from the user in a unlimited numbers. At the moment i have two ways to do it, one is to use a "for i" loop and say i run it 10 times and it count down for every time the loop is run.

The other way i can do this is to use a sentinel number for example -1, this will allow me to take input unlimited of times from the user, till the user decide to stop the loop by typing -1, however this kind of loop is ok when you are working with positive numbers, but negative numbers wont work so good here(?)

...So i was wondering is it anybody here that can help me a little bit, how can i get unlimited numbers from a user and also enable the user to enter both positive and negative numbers unlimited of times.

Sincerely
Frank Jarle
Norway/Singapore

 
Old July 7th, 2003, 10:28 PM
Registered User
 
Join Date: Jul 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

One of mye friends came with 2 good solutions.

1: Check for a blank input. If the input is blank then exit the loop and calculate the numbers
2: Check for a character, if the character is not between 0x30 and 0x39, then exit/finish the loop and calculate the numbers.

However, im very new to java se, and im not sure how i would perform this check, is it anybody that could provide me with a simple check either for both examples or just one of them, i would be much thankful.

Sincerely
Frank Jarle
Norway/Singapore

 
Old July 8th, 2003, 11:27 PM
Registered User
 
Join Date: Jul 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Really nobody that can help me?
This is bad, i remember i used to get lots of help from the previouse Forum, anyway. i just wait some more...

Sincerely
Frank Jarle
Norway/Singapore

 
Old July 9th, 2003, 12:40 AM
Authorized User
 
Join Date: Jun 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

well,
 i hope u are using BufferedReader to take input from System.in
like:
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
when you say: String s=br.read()
it will return you the string read by this reader from console.

now you have to convert this String into a number. Use
int n=Integer.parseInt(s);
This will return the number if valid, otherwise throw an exception.
when exception is thrown, break your loop.

Altaf Malik
 
Old July 9th, 2003, 03:42 AM
Registered User
 
Join Date: Jul 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Altaf, i will take a look into it and get back to you to let you know how things are going.
But dont i still need to use some kind of loop to continue to get input fro the user.

I read in some book that they were converting the string to a number, it seems to work ok, but they were still using a for i-loop to count down, the other option they were using was a setinel value like -1.

Mainly its not the input that is my problem in this case, but to run the loop infinity times accepting both negative and positive numbers, however using a senitel value with -1, it will break the loop, not something i want....I think, correct me if im wrong.

Sincerely
Frank Jarle
Norway/Singapore






Similar Threads
Thread Thread Starter Forum Replies Last Post
Encoding User Input tectrix BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 5 February 6th, 2008 06:58 AM
Sentinel Loop Question JMOdom C# 3 April 12th, 2007 01:19 PM
Displaying user input macrocosm Beginning PHP 3 June 15th, 2006 09:30 PM
validating user input hosefo81 Javascript How-To 12 March 3rd, 2004 09:32 AM
Validating user input stu9820 VB.NET 2002/2003 Basics 2 January 15th, 2004 12:51 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.