Check User Input Is Integer
Dec 18, 2014
System.out.print("To begin, please enter 1 to choose stand tickets or 2 to choose terrace tickets for your party. ");
int standOrTerraceTickets = aScanner.nextInt();
while (standOrTerraceTickets != 1 && standOrTerraceTickets != 2)
{
System.out.print("Invalid input. Please enter 1 for stand tickets or 2 for terrace tickets. ");
standOrTerraceTickets = aScanner.nextInt();
}
Okay I thought I had this working properly so that when the user entered anything other than 1 or 2 they would keep getting an error invalid input, however, that only works when the user enters an integer. If the user enters anything other than a number, the program crashes. How does one prevent this from happening?
View Replies
ADVERTISEMENT
Dec 18, 2014
I have to ask how many children's tickets you want to order. When I apply the code below, it just accepts whatever I input, including letters. I imagine it is to do with setting childrensTickets = 0? If I input a letter using the below it accepts - shouldn't it print the error given the input is not >=0?
System.out.print("How many children's tickets do you require? ");
int childrensTickets = 0;
boolean validChildrenValue = false;
while (validChildrenValue == false) {
if(aScanner.hasNextInt()) {
[Code] ....
View Replies
View Related
Apr 22, 2015
Is it possible to wait for an input for a user and check in the meantime if something else is true?
Now if the server in the meantime says: there is no input needed and tells it the client.
Could the client now somehow change from
input.readLine() to another Method say: quit() ?
I am stuck there because I know the server sends: quit to the client1 but the client2 cannot react because it still waits for an input for the user.
Maybe something with Thread.sleep ?
View Replies
View Related
Aug 26, 2014
The code for my button is below - I know it is wrong and that I need to change line 8 at least,so I am technically asking the property object if there is a key_name there but I dont quite get how to do that
//code for button
JButton btnSearch = new JButton("Search");
btnSearch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//find selected command
String key_name = textFieldSearch.getText();
[Code] ....
I basically just want the user typed in word to be checked against a keyword in a proeprties file and if it exists, pull the key and the value back into a panel ....
View Replies
View Related
Apr 13, 2014
Write a program that prompts the user for an input number and checks to see if that input number is greater than zero. If the input number is greater than 0, the program does a multiplication of all the numbers up to the input number starting with the input number. For example if the user inputs the number 9, then the program displays the following sum:
9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 = 362880
That's the question I'm getting and so far all I've got is
import java.util.Scanner;
public class Lab4Q3
{
public static void main (String[] args)
{
int keyboard;
[Code] .....
View Replies
View Related
Sep 7, 2014
I am new to java and programming in general. I figured out how to convert an integer input to binary however I am having issues doing the opposite of converting a user input binary number to a decimal.
I need to do this with basic math (or string depending on how I represent the binary) and no functions.
I know how to convert binary to integer on paper but I am having a hard time working it out in java.
View Replies
View Related
Apr 15, 2015
I decided to code this quiz I took in class about asking the user to input a string and the code is suppose to check for upper case letters. If a upper case letter is found, it should increase a count by one. Once the check is done, it should display the number of uppercase letters. For some reason I am getting this weird compile error stating that symbols can't be found...
Java Code:
import java.util.*;
import java.lang.*;
public class StringCheck{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("please enter a string: " );
String s = input.nextLine();
[Code] ......
View Replies
View Related
Nov 15, 2014
Write a program that reads from the user an integer and reduce it by multiplying its non-zero digits. The result of the multiplication is a number which is to be reduced as the initial one. This process continues until an integer of one single digit is obtained. For example:
64734502 (6 * 4 * 7 * 3 * 4 * 5 * 2) 20160 (2 * 1 * 6) 12 (1 * 2) 2
Your program should display the number obtained in every iteration.
Sample run1
Enter an integer: 64734502
After iteration 1: 20160
After iteration 2: 12
After iteration 3: 2
Sample run2
Enter an integer: 97737999
After iteration 1: 6751269
After iteration 2: 22680
After iteration 3: 192
After iteration 4: 18
After iteration 5: 8
View Replies
View Related
Jun 7, 2015
Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits.
Now I have a code for spacing out the integers, and I have a separate code for adding the digits. But I am not sure how to merge them together. Both of them work independently
Spacing code:
import java.util.*;
public class SumoftheIntegers
{
static Scanner console=new Scanner(System.in);
public static void main(String []args) {
int num1, test, rem;
int counter = 0;
[Code]...
Now the sum of the integers code:
import java.util.Scanner;
public class sum
{
public static void main(String[] args)
{
// Create a Scanner
Scanner input = new Scanner(System.in);
// Enter amount
System.out.print("Enter an integer: ");
int integer = input.nextInt();
[Code]...
View Replies
View Related
Jan 20, 2015
so i'm following a java tutorial from the book and it has a few challenge questions. and i'm stucked on one. i think i just don't understand what is it that its asking me. heres the question, Write a statement that reads a user's input integer into the defined variable, and a second statement that prints the integer. assuming scanner is given, and i checked my heading code is ok.
Scanner scnr = new Scanner(System.in);
int userNum = 0;
System.out.println("What is the product of 8 time 2");
userNum = scnr.nextInt();
[code]....
View Replies
View Related
Feb 2, 2015
Goal is to: Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits.
First I don't know where I made mistakes here, and the only error it finds right now is that str2 was not initialized and I cannot figure out where/when to initialize it.
import javax.swing.JOptionPane;
public class DigitsAndSum {
public static void main (String[] args) {
String str1;
String str2;
int int1 = 0;
int int2 = 0;
[Code] ....
View Replies
View Related
May 11, 2015
I've been working on a question using parallel arrays where the user inputs an integer 1-12 and the output will be the name of the month and the number of days in that month. This is what I have so far
import java.util.*;
public class DaysMonth
{
public static void main (String args[]) {
Scanner keyIn = new Scanner(System.in);
int[] days = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
[Code] ....
After I enter the user int it just ends.
View Replies
View Related
Nov 29, 2014
We were given the assignment to program a lotto game. in our game we should do follow the following steps below, and we are supposed to use atleast tree methods. the first one to receive the array of integers and an integer and check if the int was not entered before. the user should not repeat the same number. the other method will draw all numbers of a game (quantity of numbers to draw is based on the number parameter) and returns them in an array. the last method will display the result of the game. It receives a list of all lines played and a list of all numbers draw.
these are the steps to follow:
1. Ask the user how many lines (games) the user would like to play. Each line is made of five numbers between 1 and 45 (inclusive).
2. Ask the user to enter five numbers between 1 and 45 (inclusive) for each line. The user must NOT be allowed to enter a number more than once for each line.
3. When all the numbers for all the lines are entered, the program should display all lines and asks the user to confirm the game. If the user does NOT confirm the game, the program should start all over again.
4. Draw (generate) five numbers between 1 and 45 (inclusive). The numbers should be unique within the draw.
5. Draw (generate) one number (the bonus number) between 1 and 45 (inclusive). The number should be unique within the draw.
6. Display all the lines played and the result of each line.
7. Ask the user if the person would like to play again, if so the program should start the process all over again.
View Replies
View Related
Oct 8, 2014
Basically I need to make a program prompts the user for an integer, check to make sure the length entered by the user is a power of 2 and at least 2. Then I need to convert from base e to base 2 then I need to print the tick marks of a ruler based of the value of the length.
I got the whole converting thing working and the check for a power of 2, that wasn't an issue because it didn't require any recursion. I just don't know how to print out the proper tick mark values.
The way it is supposed to work is that it needs to look like this. Say the user enters 8;
012131210
012345678
Or if the user enters 16;
01213121412131210
01234567890123456
The bottom row is pretty much just the index value, that I print fine.
The top row though is supposed to be the length of the ticks on a ruler, with the middle most value being the value of the conversion from base e to base 2 from above. I can get that printed and what I get just looks like this.
For 8;
000030000
012345678
For 16;
00000000400000000
01234567890123456
As you can see I can get the middle value and the index values below but I don't know how to use recursion to get the right numbers.
Here's my current code.
import java.util.*;
public class TickMarks {
public static void main (String args[]){
Scanner input = new Scanner(System.in);
boolean looping = true;
while(looping == true){
System.out.print("Please enter a length: ");
[Code]...
Now the methods isPowerOfTwo() and printLength() both work fine. However, I'm not sure how to get the left and right side working.
My thoughts were to split the length in half and get a left and right side. I gave both of them arrays so I can keep track of the values. Now as you've seen above I get all zeros and I know it's because that's the default value in an array, I just don't know how to get the proper values.
View Replies
View Related
Jan 24, 2014
I am using form based, declarative security approach. And, when some user, on login form enters user credentials (username and password), he is being redirected to certain/secured jsp page.
Part of that page content is:Hello <%=request.getUserPrincipal().getName().toString()%> You are able to view this page because you are authorized user.You can see that this is implemented using JSP scriptlet. And this works. I would like to use JSTL instead of scriptlet.
So, instead of scriptlet, I put this JSTL code: <c:out value="${requestScope.userPrincipal.name}"/>, but not getting user's username with it. Basically, I don't clearly understand where and how are these objects/user credentials being stored with Form based JAAS.
I would like on JSP page to check if some user is already logged in. So, if there is logged user to display user's name (also with JSTL).Something like this:
<c:if test="${not empty sessionScope.userPrincipal}">
User <c:out value="${sessionScope.userPrincipal.name}" />
</c:if>
Here I tried with sessionScope but still not getting anything.
View Replies
View Related
Dec 16, 2014
My program here asks for an unit to choose from (fl.oz, gal, oz, lb, in, ft, or mi), asks how much of it they have, and asks for the unit they wish to convert to (mL, l, g, kg, mm, cm, m, or km).
My program works, refusing to convert from silly conversions such as gal to cm, telling you to re-input if they enter anything other than fl.oz, gal, etc.
The only thing I cannot figure out is if the user inputs something like "foo" when the program prompts the user for how much of the unit they have. My goal is to have the program say something like "That is not a number! Please enter a numerical value."
My current dilemma right now is that if the user inputs something other than a number, it will catch the exception and print a line telling the user it's not a number, except, it does it infinitely (stuck in a loop). Here is my code:
import java.util.InputMismatchException;
import java.util.Scanner;
public class UnitConversions {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
double result = 0;
[Code] ....
View Replies
View Related
Mar 18, 2014
I have a question about how to check for a session as the user goes through different pages. I am using Java beans and EL to pass data from page to page but I cannot figure out how to check for a session on the pages without the use of a scriptlet with an if else statement in it.
I do not want the user to be able to access any pages unless they are logged in. With a scriptlet, I can create a statement that says if session exists show name else redirect to login page.
From what I have read and what some of the wonderful members here have told me, scriptlets are a big no no these days. So, how do I do these without the use of a scriptlet?
View Replies
View Related
May 1, 2014
I am trying to write a simple program that checks if a user's input has a specific letter using the ".contains" method but its not doing what i wanted to do. here is my code below.
import java.util.Scanner;
public class secret
{
public static void main(String args[]) {
Scanner sc;
char hidden='a';
String guess;
[Code] ....
View Replies
View Related
Jun 10, 2014
/**
* Auto Generated Java Class.
*/
import java.util.*;
public class Hangman {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
int guess;
boolean revealed[] = {false, false, false, false, false};
String word [] = {"c", "a", "n", "a", "d", "a"};
[Code] ....
I am not sure how to make the program check if the letter entered by the user matches the one in the array. also i am not sure how to make the program run again with a new word.
View Replies
View Related
Apr 9, 2014
Write a java program that will ask a user to input grades until the user inputs the sentinel value -1. The program should test each input to determine whether 0<=grade<=100. If the grade entered is within this range, the program should print "Grade accepted;" if not, the program should print "Invalid input".
View Replies
View Related
Nov 4, 2014
I have been struggling with this program for weeks. This program is supposed to take a user's inputted odd number and then print out all prime numbers lower than that number.
public class PrimeNumber
{
Scanner scan = new Scanner(System.in);
int userNum;
String neg;
public void getUserNum()
[code]...
View Replies
View Related
Dec 19, 2013
The project is to develop the game Translate the Word .... It is asking user to translate a word proposed to and check if the input response is correct. At the end of the game score will be calculated and displayed.
Game Play :
1 - Ask the user to specify , through the console , its name and the number of words to offer . It is up to you to handle exceptions (eg number of words greater than the number you provided )
2 - Recover user response ( the word translated ) and check whether to continue . (eg you want to continue (y / n)) after each proposal.
3 - compare the response of the user with that which is preset for the word in question .
4 - Show the score at the end ( or at the breakpoint ) .
5 - Save the file in a user name , the score , the number of questions and the start date and end of the game played .
Some notes to consider :
1 - The language (eg, English - French , English - Arabic , etc. . ): It is up to you to specify the language adopted in the game and inform the user of your choice.
2 - The word bank to offer : It is up to you to develop the appropriate means to get the words to propose to the user. That said , the words and their translations can be retrieved :
a. a TXT file
b . an XML file . ( Tutorials DOM and SAX )
c . CSV file ( OpenCSV Tutorial )
d. a database ( Tutorial Access)
e . through APIs (eg Wordnet and google translate etc . ) .
f . a combination of the previous options a, bc , d and / or e . (eg words stored in a txt file and answers retrieved from the api google translate)
g . etc. .
Examples of files and databases are attached to the project statement . You will need to add one or more external libraries to your project. Click here for details on adding external libraries to Netbeans .
3 - A user will be associated with the question score if he can translate the word correctly. The score for each question can be calculated based on the number of words / questions to be proposed .
Development : In this project you will need at least a class called Question to encapsulate the word and its translations and provide all necessary methods to manipulate the object type Question.
An interface called IParser to make extensible project. Any class that implements IParser is a parser file (XML , TXT , CSV , etc.). / Database. In your project there will be a single class that implements IParser and will be used to retrieve words and their translations.
Add the ability to store the questions and answers of the user on the hard disk. Make the class Serializable Question
View Replies
View Related
Jul 21, 2014
I have a program in which I take some characters input from User
using
String inputString=in.next();
How can i restrict user that input should be only char 0 or 1?
If the answer is regular expressions then What Should be Its Regular expressions?
View Replies
View Related
Jul 5, 2014
I have a GUI with a JTextField. I need to check that the input entered by the user is made up of only integers.
For example I accept: 1, 10, 530, ...
I do not accept: -10, 1.5, -6 ...
I would like to implement this control by using a regular expression so I would have a code like that.
JTextField text = new JTextField();
String input = text.getText();
boolean bool = checkInteger(input);
if(bool)
System.out.println("Ok");
else
System.out.println("Error");
The checkInteger(String input) method should check if the input string is correct or not.
How can I do to implement this control with regular expressions?
Is there an easier way to do it?
View Replies
View Related
Jun 12, 2014
So I clipped this out of my Jeopardy game code and made it into its own project for testing. using this code I want to check to see if the input for wage is an integer?
import java.util.Scanner;
public class test {
/**
* @param args
*/
public static void main(String[] args)
{
Scanner input = new Scanner (System.in);
Obviously right now if you enter "aflwkj" or some such for the wager, the program terminates. How to make a loop that will keep asking the user for a value for wage until the input is an integer?
View Replies
View Related
Apr 14, 2014
This program is supposed to accept an integer as an input and display the message that the number is even or odd. The main method calls a Boolean method. Write a method private static boolean iseven(int number and the message is printed from the Main method. This is what I have.
import java.util.Scanner;
public class OddorEven {
int number;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
[Code] ....
I know the message is not being printed from the Main method. I'm not sure how to do that.
View Replies
View Related