Checking To See If Input From Scanner Is Integer

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


ADVERTISEMENT

Reading Input With Scanner And Checking If User Entered Empty String?

Apr 7, 2015

The project is a program that allows the user to enter students and enter grades for each student. One of the requirements is that if there is already a grade stored for the student that it will display the previous grade. IF the user then enters a new grade the new grade will be stored. IF the user simply presses enter (enters an empty string) nothing is done. I have everything working except for the requirement of doing nothing if the user enters an empty string. If I just press enter at this point I get a NumberFormatException.

The below code is a method "setTestGrades" from the student class. This method iterates through each student object stored in an array list and then checks if there is a previous grade (Requirement# unset grades have to default to -1) before allowing the user to set a new grade.

public void setTestGrades(int testNumber) { //Sets the grade for the specified test number for each student in the arraylist.
testNumber -= 1;
Scanner input = new Scanner(System.in);
for (int i = 0; i < studentList.size(); i++) {
System.out.println("Please enter the grade for Test #" + (testNumber + 1) + " For Student " + studentList.get(i).getStudentName());

[code]....

View Replies View Related

Terminate Scanner At Specific Integer

Jan 18, 2015

I have the following code:

while (scan.hasNextInt()) {
list.add(scan.nextInt()); }

It asks the user to input integers and passes them to an ArrayList of integers named list. Without any termination it works fine, but I am not sure about how to terminate the scanner at a specific value. I am trying to terminate the scanner once the user enters 0. Is there a method in the Scanner class that terminates at a specific value?

I've been trying while (scan.hasNextInt() && scan.nextInt() != 0 ) but it seems to only work on the second zero entered. For ex, if I enter 2 3 6 8 0 0 it stops on the second zero, not the first

View Replies View Related

Checking If Any Input Has Been Entered

Jan 12, 2015

I just can't find a way to check if user puts any input or not? The line is Employee Name and I need to validate that he puts something there.

import java.util.Scanner;
public class JavaMartInventorySystem {
public static void main(String[] args) {
String empName;
Scanner keyboard = new Scanner(System.in);

[Code] ....

View Replies View Related

Checking If Input Is Digit?

Sep 28, 2014

which method i can use so that the program checks if the input value is a digit or else outputs a message saying that `the value entered is not a number.

View Replies View Related

Checking If User Input Value And Value In Properties File Is Same

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 properties file and if it exists, pull the key and the value back into a panel

View Replies View Related

If Statement - Checking / Comparing Two Strings From User Input

Dec 7, 2014

I am trying to do is get a user to input their name into the system. Once the user has inputted the name into the system, the system should check to see if the name entered matches "alice". If this is true then the system should print "welcome to the system alice". If it is not alice it should not do anything.

Here is the code I have so far... The fault/ error seems to be with the sInput string in the IF statement.

import java.io.*;
public class Hellohuman
{
public static void main (String[] args) {
try {
InputStreamReader isr = new InputStreamReader(System.in);

[Code] ....

View Replies View Related

Checking If User Input Matches Any Number In Same Column

Feb 14, 2014

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package firstgui;
import javax.swing.*;
import java.awt.event.*;
public class TicTacToe {

[Code] ......

Program is 10x10 board, I need to check if the user's input is a duplicate of any number in that same column, but I can't figure it out. When I tried it, it always check the same box. For example, if I entered 4 in [1][1] (going by 10x10 grid), it automatically checks right after I entered that [1][1] is the same as my input and erases it. My professor wants me to check it with the "CheckWinner" method.

I tried the following when someone told me to pass the reference of the JButton being clicked to ignore it.

private boolean CheckWinner(JButton source, String inplayer) {
//...
if (EventBoard[i][j] != source &&
EventBoard[i][j].getText().equals( inplayer )){
JOptionPane.showMessageDialog(null, "copy");
EventBoard[i][j].setText("");
}

View Replies View Related

Scanner Taking Input Twice

Oct 29, 2014

I have a small bug in my program. The user is asked what person(s) information they want to access but lets say they want captain they must enter "captain" twice. I think it will make more sense to you with the code. I have searched all over to see what is causing the bug but still have found no resolution. I even tried making two different scanners but that didn't work either.

I know the while loop (line 16) I am using is causing the bug because it works fine without that but then I cannot validate the input.

package myproject;
import java.util.Scanner;
public class Enterprise {
public static void main (String[]args){
String userInput;

[Code]......

View Replies View Related

Console Input Of Strings Using Scanner

May 9, 2015

I am having a great deal of frustration trying to use the scanner to get user input from the eclipse console. Here is what I am trying to do.

Print a menu for the user with options, take that (1) char input and run it into a switch statement for processing. I have that done and it is working fine.

If the user chooses to enter strings for storage, They are instructed to enter their string and press enter to complete that string entry. Then enter the next string and press enter, etc.

So I have a While loop for this. Get the scanner input, store it in the LinkedList, get the next scanner input, etc. I get the scanner string and store it in a Linked list. That works fine. The user is instructed to simply enter a blank string to end the entry procedure, like just press Enter on the new line without typing a new string.

The problem is the scanner doesn't seem to return anything for me to Test to close this procedure. How do I TRAP the fact that the user just pressed enter so I can end my procedure? I have tried next() and nextLine() and reset(), etc. And I am getting knowhere.

View Replies View Related

Parsing Input From A File Using Scanner

Mar 26, 2014

I'm having some kind of weird problem reading input from a file. It says that my scanner object I'm using to hold an item of information isn't initialized, when I do try to initialize it, it says error variable already initialized. I'm using the scanner to read input from a file whose contents are this

10
150.4
88.4
-3.14
499.4
799.4
1299.8
0
1900.2
901.7
4444.4

This is my program

import java.util.*;
import java.io.*;
public class QudratullahMommandi_3_07 {
  Toolkit_General toolKit = new Toolkit_General();
  public static void main (String[]args)throws IOException

[Code] .....

This is the error message

QudratullahMommandi_3_07.java:34: error: variable holder2 might not have been initialized
String holder2 = holder2.trim();
^
1 error

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.

View Replies View Related

Use Scanner To Get Input And Distinguish Between Int And Text?

Sep 13, 2014

​Is there any way to use scanner to get input and distinguish between int and text? I'm trying to make a scanner method I can call whenever I need user input no matter if I need int or text. Using two methods (one for int, other for text) at the moment.

import java.util.Scanner;
public class Application {
public static void main(String[] args) {
Person person1 = new Person();
System.out.println("What is your name?");

[Code] .....

View Replies View Related

Creating A Scanner And Putting Input Into Arrays

Apr 15, 2014

I'm trying to read user input from the terminal and separate the input into separate arrays depending on if the user input is an integer, scanner, or a string. The terminal should keep asking the user for input until the user types "quit".

import java.util.*;
public class arrayScanner {
public static void main(String[] args) {
ArrayList<Integer> intList = new ArrayList<Integer>();
ArrayList<Double> doubleList = new ArrayList<Double>();
ArrayList<String> otherList = new ArrayList<String>();

[code].....

View Replies View Related

Polynomials Into Array Using User Input With Scanner

Feb 19, 2014

Assignment: Your task is to implement a similar scheme to store polynomials of any number of terms, such that the number of terms and the components (coefficient, variable and exponent) of every term are entered from the keyboard. To implement the interactive input we will using the Java class Scanner, defined in the java.utils standard package. The Scanner class can be used in Java to read data types from a file.

Since the input console (keyboard) is treated as the file called System.in, we can create a Scanner for that input stream as new Scanner (System.in), as shown below. Once you define a Scanner object, using its method next() you can read Strings from the file/keyboard. The incomplete program below is your assignment. You are supposed to complete without changing the existing code. Your output should be the terms of the polynomial entered by the user, separated by + signs. Additional instructions in the code below, that you will change to achieve the requested functionality.

import java.util.Scanner;
public class Polynomials {
public static void storeTerm (int coeff, String var, int exp, String poly[][], int
where){
//ENTER THE COEFFICIENT, VARIABLE AND EXPONENT INTO THE
//ARRAY POLY THAT REPRESENTS THE POLYNOMIAL, AT POSITION "where"
//THAT RANGES BETWEEN INDEX 0 AND POLY.LENGTH-1

[Code] ....

Anyways, I'm not looking for straight answers. I just want to know which section I should start on first because I was advised to tackle programs one problem at a time.

View Replies View Related

Failing To Read Input With Scanner NextLine

Jan 29, 2014

I Just made a simple code, but in any case it's not taking the input I'm trying to enter for the city.

View Replies View Related

Scanner Object Never Waits For User To Input Anything

Jun 13, 2014

The method below is called within a large loop in another method. The code has been boiled down to easily show the problem I'm having. I found that the method worked fine (prompts user for input, gets input, returns input) until I added a statement at the end of the method that closes the Scanner object. When the kb.close() statement is present, the method works the first time through, but subsequent calls to this method only display the prompt and the Scanner object never waits for the user to input anything thereafter.

public int scanIt()
{
int i;
Scanner kb = new Scanner(System.out);
System.out.print("==> "); // prompt for input
if(kb.hasNextInt())
{
i = kb.nextInt();
System.out.println("User entered " + i);
}
kb.close();
}

View Replies View Related

Scanner That Scans Console For Input To Fetch String Word

Jul 28, 2014

I'm having an issue, I have a scanner (Scan.nextLine();) that scans the console for input to fetch the string "word". Then I want to fetch a character using Scan.findInLine(word).charAt(number);. The problem is that the console requires me to write 2 lines in order for the program to move on. I only want the program to scan for a word, and then move on with what it has instead of requiring 2 inputs.

View Replies View Related

Checking If Matrix Is Upper Or Lower Triangular Matrix Based On User Input

Dec 5, 2014

java program that will determine if the matrix is a lower or upper triangular matrix. I only need to use java.util.Scanner;.

View Replies View Related

Code Not Running Scanner NextLine After Using Scanner NextInt?

Feb 14, 2015

package jdbc;
import java.sql.*;
import javax.sql.*;
import java.util.*;
public class Jdbc {
public static void main(String[] args) {

[code]....

View Replies View Related

How To Check Input Is Integer In Java

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

How To Use Regular Expressions For Integer Input

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

JTextField That Accept Only Integer Input?

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

Accept Integer As Input - Display Even Or Odd

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

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 View Related

Converting User Input Binary To Integer

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

How To Limit Users To Enter Only 12 Digit Input And Integer Value

Feb 8, 2014

I want to limit the program to only accept a 12 digit input and an integer value but I don't know how to program it,

import java.util.Scanner;
public class testing4
{
public static void main(String []args) {
Scanner reader = new Scanner(System.in);
String input;

[Code]...

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved