473,385 Members | 1,647 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,385 software developers and data experts.

Java data validation question

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


************************************************** ******************************************
Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. import java.text.*;
  3.  
  4. public class MReikowskyapp51
  5. {
  6.     public static void main(String[] args)
  7.     {
  8.         // welcome the user to the program
  9.         System.out.println("Welcome to the Factorial Calculator");
  10.         System.out.println();  // print a blank line
  11.  
  12.         // create a Scanner object named sc and intializ variables
  13.  
  14.  
  15.         Scanner sc = new Scanner(System.in);
  16.  
  17.         // perform Factorial application when user enters "y" or "Y"
  18.         String choice = "y";
  19.         while (choice.equalsIgnoreCase("y"))
  20.         {
  21.             //get input from user
  22.             int power = getIntWithinRange(sc,
  23.             "Enter number greater than 0 and less than 10: ", 0, 10);
  24.             long number = 1;
  25.             //use a for loop to calculate the power
  26.             for (int i = 1; i<= power; i++)
  27.             {
  28.  
  29.                    number = number * i;
  30.              }
  31.                    System.out.println("power " + number + "\n");
  32.                    String cont = "";
  33.                    System.out.println("Continue? (y/n)");
  34.                    cont = sc.next();
  35.                    sc.nextLine();
  36.                    System.out.println();
  37.  
  38.  
  39.          }
  40. }
  41. public static int getInt (Scanner sc, String prompt)
  42. {
  43.     int i = 0;
  44.     boolean isValid = false;
  45.     while (isValid == false)
  46.     {
  47.         System.out.print(prompt);
  48.         if (sc.hasNextInt())
  49.         {
  50.             i = sc.nextInt();
  51.             isValid = true;
  52.         }
  53.         else
  54.         {
  55.             System.out.println
  56.             ("Error! Invalid integer value. Try again.");
  57.         }
  58.         sc.nextLine(); //discard any other data entered on the line
  59.     }
  60.     return i;
  61. }
  62.     public static int getIntWithinRange (Scanner sc, String prompt, int min, int max)
  63.     {
  64.         int i = 0;
  65.         boolean isValid = false;
  66.         while (isValid == false)
  67.         {
  68.             i = getInt(sc, prompt);
  69.             if (i <= min)
  70.                 System.out.println("Error! Number must be greater than " + min + ".");
  71.  
  72.             else if (i >= max)
  73.             System.out.println("Error! Number must be less than " + max + ".");
  74.  
  75.             else
  76.             isValid = true;
  77.         }
  78.         return i;
  79.     }
  80.   }//end program
Feb 18 '07 #1
3 3897
horace1
1,510 Expert 1GB
the line
Expand|Select|Wrap|Line Numbers
  1.                    System.out.println("Continue? (y/n)");
  2.                    cont = sc.next();
  3.  
should be
Expand|Select|Wrap|Line Numbers
  1.                    System.out.println("Continue? (y/n)");
  2.                    choice = sc.next();
  3.  
because you test choice in
Expand|Select|Wrap|Line Numbers
  1.         while (choice.equalsIgnoreCase("y"))
  2.         {
  3.  
Feb 18 '07 #2
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?
Feb 18 '07 #3
Ganon11
3,652 Expert 2GB
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.
Feb 18 '07 #4

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

Similar topics

4
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...
0
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:...
2
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...
1
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...
2
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...
4
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...
1
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...
20
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.