Ok this is homework, and I have wrestled with this for quite awhile now. My program is supposed to give an error message if the user enters any invalid entries. The program works great when the user enters an invalid integer, but my problem is trying to get it to give an error message when the user enters an invalid entry when asked if they want to continue. Currently right now it just runs again if an invalid entry is entered. But I need it to give an error message if the user enters anything other than y or n. Id appreciate any help and suggestions on this. Thanks
*************** *************** *************** *************** *************** *************** ** -
import java.util.*;
-
import java.text.*;
-
-
public class MReikowskyapp51
-
{
-
public static void main(String[] args)
-
{
-
// welcome the user to the program
-
System.out.println("Welcome to the Factorial Calculator");
-
System.out.println(); // print a blank line
-
-
// create a Scanner object named sc and intializ variables
-
-
-
Scanner sc = new Scanner(System.in);
-
-
// perform Factorial application when user enters "y" or "Y"
-
String choice = "y";
-
while (choice.equalsIgnoreCase("y"))
-
{
-
//get input from user
-
int power = getIntWithinRange(sc,
-
"Enter number greater than 0 and less than 10: ", 0, 10);
-
long number = 1;
-
//use a for loop to calculate the power
-
for (int i = 1; i<= power; i++)
-
{
-
-
number = number * i;
-
}
-
System.out.println("power " + number + "\n");
-
String cont = "";
-
System.out.println("Continue? (y/n)");
-
cont = sc.next();
-
sc.nextLine();
-
System.out.println();
-
-
-
}
-
}
-
public static int getInt (Scanner sc, String prompt)
-
{
-
int i = 0;
-
boolean isValid = false;
-
while (isValid == false)
-
{
-
System.out.print(prompt);
-
if (sc.hasNextInt())
-
{
-
i = sc.nextInt();
-
isValid = true;
-
}
-
else
-
{
-
System.out.println
-
("Error! Invalid integer value. Try again.");
-
}
-
sc.nextLine(); //discard any other data entered on the line
-
}
-
return i;
-
}
-
public static int getIntWithinRange (Scanner sc, String prompt, int min, int max)
-
{
-
int i = 0;
-
boolean isValid = false;
-
while (isValid == false)
-
{
-
i = getInt(sc, prompt);
-
if (i <= min)
-
System.out.println("Error! Number must be greater than " + min + ".");
-
-
else if (i >= max)
-
System.out.println("Error! Number must be less than " + max + ".");
-
-
else
-
isValid = true;
-
}
-
return i;
-
}
-
}//end program
3 3912 horace1 1,510
Recognized Expert Top Contributor
the line -
System.out.println("Continue? (y/n)");
-
cont = sc.next();
-
should be -
System.out.println("Continue? (y/n)");
-
choice = sc.next();
-
because you test choice in -
while (choice.equalsIgnoreCase("y"))
-
{
-
Ok thank you very much. My next question is where and how do I post the error message if something other than y or n is entered? Ive looked all over in my book and I couldnt find the command that will validate a string. I found plenty for numbers IE nextDouble but nothing that explains how to check the data they enter when they determine if they wish to continue or not?
Ganon11 3,652
Recognized Expert Specialist
You can use the .equals() function in the String class. No matter what they enter, you will be interpreting it as a String, whether it is actually a number, character, word, etc.
Once you have the input in a variable (choice?), use
choice.equals(" y");
to determine if it is y. You can use the same function to see if it is n. If it is neither y nor n (that is, !choice.equals( "y") && !choice.equals( "n")), then the user has entered a bad input.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: TP |
last post by:
Hi,
I have reached a situation where I have to create small pieces of xml
(around 8k characters) within my java app. Right now I am using
stringbuffer to create the xml which does not go against a dtd. This
is working for now, since I do not have to read in the xml again.
But if I wanted to use some other xml utility like xerces or jdom, is
it recommended for my use of xml or would it be an overkill. Please
let me know.
|
by: Brian |
last post by:
I am having alot of trouble getting a XML document validated with a
schema.
I got a sample document and schema off of w3schools.com, which passed
an online xml validator:
http://tools.decisionsoft.com/schemaValidate.html. I cannot, however,
get them validated programmatically.
The documents are:
|
by: Dnna |
last post by:
I have a table which is bound to an Internet Explorer XML
data island. I'm using ASP.NET's client-side validators
for an input field in the table. The problem is that if
the input fields are in a table that is bound to the
island (such that the rows are dynamically generated by IE
from the XML data island), the validators do not work. If
the table is not bound, the validators do work, but then I
don't have the table iterating through...
|
by: David Van D |
last post by:
Hi there,
A few weeks until I begin my journey towards a degree in Computer
Science at Canterbury University in New Zealand,
Anyway the course tutors are going to be teaching us JAVA wth bluej and
I was wondering if anyone here would be able to give me some tips for
young players such as myself, for learning the language.
Is this the best Newsgroup for support with JAVA?
|
by: Jobs |
last post by:
Download the JAVA , .NET and SQL Server interview with answers
Download the JAVA , .NET and SQL Server interview sheet and rate
yourself. This will help you judge yourself are you really worth of
attending interviews. If you own a company best way to judge if the
candidate is worth of it.
http://www.questpond.com/InterviewRatingSheet.zip
| |
by: Francois Stander |
last post by:
Hi, hope someone can help me. It seems imposible to read data from a server, however, I can read the validation data from the server and hold it in dataviews . datasets or data tables in my asp "load" page. I would like to use these data (which I presume is in some form of html or xml) in my HTML portion of the asp page. The data must be read using java script and after lots of calculations perform on it - be written to xml controls.
I can...
|
by: jaimemartin |
last post by:
hello,
I want to validate an xml by means of a schema (xsd). To do that first of all I´m using a SchemaFactory. The problem is that if I run the code in Windows all works fine, but If I run it in Linux there is an error. The code that fails is the following:
SchemaFactory factory =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
I´m sure that code is ok. In fact, I´ve found that in several...
|
by: hippomedon |
last post by:
Hello everyone,
I'm looking for some advice on whether I should break the
normalization rule. Normally, I would not consider it, but this seems
to be a special case.
I have created an "Outcomes Database" used to store response data
from measures/ questionnaires for a longitudinal health study. It is
essentially derived from Duane Hookom's Survey Database (thanks
Duane!!!), with many modifications added to fit the needs of my lab.
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |