473,406 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

User I/O loop question

3
Hi! I'm trying to write a program that solicits the user for input and then asks them if they wish to run it again (y/n). I can't quite figure out how to work the loop though with the y/n response. Here is what I was trying to do, but obviously I'm having a problem:

import java.io.*;
import java.util.*;

public class LoopTest
{

// Begin Main

public static void main(String[] args) throws IOException
{
char response = 'y';
while (response == 'y')
{
Scanner stdin = new Scanner(System.in);
System.out.println("Please enter number of events to simulate: ");
int N = stdin.nextInt();
System.out.println("Please enter the probability percentage for an arrival: ");
double P = stdin.nextDouble();
System.out.println("You wanted " + N + " events tested.");
System.out.println("You chose a " + P + "% probability for an arrival");
System.out.println("Would you like to try again?(y/n) ");
response = stdin.nextChar();
} // end Main
}
}

I'm getting an error because there is no nextChar method this can use; but I couldn't figure out how to do it using a String for response - am I missing something very basic/easy?
Thanks!
Nov 2 '06 #1
2 2001
r035198x
13,262 8TB
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class LoopTest
  5. {
  6.  
  7. // Begin Main
  8.  
  9. public static void main(String[] args) throws IOException
  10. {
  11. String response = "y";
  12. while (response.equalsIgnoreCase("y"))
  13. {
  14. Scanner stdin = new Scanner(System.in);
  15. System.out.println("Please enter number of events to simulate: ");
  16. int N = stdin.nextInt();
  17. System.out.println("Please enter the probability percentage for an arrival: ");
  18. double P = stdin.nextDouble();
  19. System.out.println("You wanted " + N + " events tested.");
  20. System.out.println("You chose a " + P + "% probability for an arrival");
  21. System.out.println("Would you like to try again?(y/n) ");
  22. response = stdin.next();
  23. } // end Main
  24. }
  25. }
Whenever you are not sure of a method, use the api specs.
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html
Nov 3 '06 #2
Sudz28
3
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class LoopTest
  5. {
  6.  
  7. // Begin Main
  8.  
  9. public static void main(String[] args) throws IOException
  10. {
  11. String response = "y";
  12. while (response.equalsIgnoreCase("y"))
  13. {
  14. Scanner stdin = new Scanner(System.in);
  15. System.out.println("Please enter number of events to simulate: ");
  16. int N = stdin.nextInt();
  17. System.out.println("Please enter the probability percentage for an arrival: ");
  18. double P = stdin.nextDouble();
  19. System.out.println("You wanted " + N + " events tested.");
  20. System.out.println("You chose a " + P + "% probability for an arrival");
  21. System.out.println("Would you like to try again?(y/n) ");
  22. response = stdin.next();
  23. } // end Main
  24. }
  25. }
Whenever you are not sure of a method, use the api specs.
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html
Thanks much for your help! That's actually very close to what I started out trying to do (using 'String reponse = "y";'), but I couldn't get the test case (the 'while') to work right so that's why I started trying to us char instead. I appreciate your input, and will double-check the api specs again. I have to admit that I go there quite often in instances like these, but usually I end up more confused :-)
Cheers!
Sudz
Nov 3 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: John Wilson | last post by:
Hello, I have the following code which populates as table data from a SQL Server 2000 stored proc (RSByDemoID2). Below that is the view and stored procedure which takes @DemoID as input to match...
8
by: rikesh | last post by:
Hi I'm sure this is a very trivial problem! I have a combo bound to a recordset. I was wondering how I can show the value on the page, what the user has selected? The code that I'm using is...
8
by: Shamrokk | last post by:
My application has a loop that needs to run every 2 seconds or so. To acomplish this I used... "Thread.Sleep(2000);" When I run the program it runs fine. Once I press the button that starts the...
3
by: Danny Ni | last post by:
Hi, Is there a way to initialize an user control after it's loaded? I know this is a strange question, so allow me to elaborate, I have an user control which allow users to enter customer data and...
5
by: Chad Beckner | last post by:
First, sorry for crossposting, not sure exactly where to place this question... I can not seem to find a way to get a users (or my) tokenGroups from ADS using VB.NET. I have seen several...
15
by: Dan DeConinck | last post by:
Hello, I want to save the position of all the controls on my form. I would like to write them out to a preference file. I have done this in DOS BASIC like this: TO WRITE the preference...
9
by: chuck | last post by:
I need some help with validating user input. I am writing a C computer program for an intro to C course. Here is the situation. I am creating an application that will do currency conversions. ...
1
by: Alec MacLean | last post by:
Hi, Outline of problem: I've built a set of user controls that are used to output questions for a survey and gather the responses using simple radio buttons. I'm adding an optional textbox...
29
by: garyusenet | last post by:
I'm trying to investigate the maximum size of different variable types. I'm using INT as my starting variable for exploration. I know that the maximum number that the int variable can take is:...
1
by: steevan16 | last post by:
Hi, Not sure if I am in the right section. Just have a query about manipulating excel spreadsheet using vbscript. I am developing a vbscript which runs on client xp machines to retrieve certain...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.