Forcing Correct User Input
Nov 23, 2014
I need to force the user to enter an integer between 1 an 11. The code below works great.... It will work fine if the user inputs any letter or character and reprompt for a number. However once a number is entered(a number out of range-or it would just accept the number) and then the user enters a letter or character that isnt an int the program crashes with a input mismatch exception. I see WHY it is doing this but I cant figure out how to put my while loops to fix this! Here is the code. Someone suggested to Catch the exceptions, then prompt for retry if I encounter one.. however I havn't used the try - catch for exception handling before. Here is my code.
public static int getDecadeSelection() {
int decadeChoice = 0;
System.out.println( "
Choose your decade: " );
System.out.println( " 1 - 1900-1909 " );
System.out.println( " 2 - 1910-1919" );
System.out.println( " 3 - 1920-1929" );
System.out.println( " 4 - 1930-1939" );
System.out.println( " 5 - 1940-1949" );
[Code] ....
It is because I am setting decadeChoice to console.nextInt, so once it passes that if the user inputs a letter it will crash, just not sure how to make it work.
View Replies
ADVERTISEMENT
Feb 3, 2015
I'm focusing on the player1Turn() method. When I run this and say temp = A, it says pit = 0. I don't know why this is.
import java.util.*;
public class Mancala {
Scanner input = new Scanner(System.in);
public static void main(String[]args) {
[Code] .....
View Replies
View Related
Jun 17, 2014
I need the game to automatically proceed to the next game after the user has guessed the correct number. I don't know how to do this or what to add.
Here is my code:
import java.util.Random;
import java.util.Scanner;
public class HiLo
{
public static void main (String[] args)
[code]...
View Replies
View Related
Feb 15, 2015
My code compiles successfully, but when executing I never get an output, and then the message: ">Process failed to respond; forcing abrupt termination...". Is there an infinite loop in my ab method, is that why?
public class Test{
boolean ab(String s){
boolean seenAnA=false;
while(true){
if(s.charAt(0)=='a'){
seenAnA=true;
s=s.substring(1);
[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
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
Apr 9, 2015
Where do I input this segment?
Scanner user_input = new Scanner( System.in );// This line goes at the top with the other global variables
public void getUserInput(){
System.out.print("Enter litres: ");
litres = user_input.nextDouble( );
System.out.print("Enter mileage: ");
mileage = user_input.nextDouble( );
System.out.print("Enter speed: ");
speed = user_input.nextDouble( );
[Code] ....
This is my client file.
class Client{
public static void main(String []args){
Bike R1=new Bike(5.0, 60.0,30.0);//create bike object with params
Bike R2=new Bike();//without params
System.out.println(R1.increaseSpeed());//calling methods
System.out.println(R1.maxDistance());
System.out.println(R2.increaseSpeed());
System.out.println(R2.maxDistance());
}
}
View Replies
View Related
Nov 13, 2014
I have been coding in my class at school (Grade 11 Computer science) and i just downloaded the program on my computer at home, unfortunately i cannot access my computer notes at home and i also dont remember certian specifics of coding, so my question is how would i get user input to create a program. The comments are the parts i dont remember. (I am trying to slowly build my memory with this stuff)
Here is my code so far:
import java.util;
[highlight=java]
public class hello_world {
public static void main(String[] args) {
string name;
//WHAT DO I PUT HERE????
[code]....
View Replies
View Related
Apr 19, 2014
I'm working on creating a dice game and getting the users input is giving me a really hard time. I have the main game working but this part I'm stuck on. The problem is that it's not giving me the chance to enter the second input when I ask for the game.
//Create Scanner object
Scanner keys = new Scanner(System.in);
//Get chips
[Code]....
*****This is what I get when I run it
run:
How much money would you like to change? 50
You now have $50 in chips.
What game do you want to play? You did not pick dice.
View Replies
View Related
Oct 11, 2014
Is there a way you can control user input?
For example, is it possible to only allow the user to enter digits?
View Replies
View Related
Feb 4, 2014
I have been given a piece of work to do for College that requires me to format user input. For example: This is an example of text that will have straight left and right margins after formatting
will transform into:
This..is..an.example
of..text..that..will
have..straight..left
and...right..margins
after.....formatting
The user inputs the width, in the case above being 20. Each line has to therefore be 20 characters long. All spaces are to be replaced with full stops.
This.is.an.example.o
f.text.that.will.hav
e.straight.left.and.
right.margins.after.
formatting..........
public class Main {
public static void main ( String args[])
[code]....
View Replies
View Related
Jul 2, 2014
Goal is to get user input and sum together. I've tried it different ways. This is the one with the least amount of problems.
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: java.lang.Integer.parseInt
ArrayList<Integer> leftCU = new ArrayList<>(); {
System.out.println
("Please enter one at a time the number of CU's for each class that is left to complete. Enter Q when done.");
leftCU.add(in.nextInt());
[Code] .....
View Replies
View Related
Jan 9, 2015
I am trying to get user input for a char value and am having some difficulty getting input for a char value.
Java Code:
//imports packages
import java.io.*;
import java.text.DecimalFormat;
class ModuleCulminatingTask {
public static void main (String args []) {
//declares variables
float var3, var4;
long var5 = (int)(Math.random()*10);
[code]....
View Replies
View Related
Apr 10, 2014
I'm not sure what is wrong with my code:
import java.util.Scanner;
public class Main {
int age = 0;
System.out.println("Enter your age: ");
// Read in values
age = in.nextInt();
}
The in is underlined, telling me "cannot find symbol"
View Replies
View Related
Apr 19, 2014
I'm working on creating a dice game and getting the users input is giving me a really hard time. I have the main game working but this part I'm stuck on. The problem is that it's not giving me the chance to enter the second input when I ask for the game.
//Create Scanner object
Scanner keys = new Scanner(System.in);
//Get chips
System.out.print("How much money would you like to change? ");
int chips = keys.nextInt();
System.out.println("You now have $" + chips + " in chips.");
[code]...
This is what I get when I run it run/How much money would you like to change?
View Replies
View Related
Mar 25, 2014
Java Code:
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
public class SortTable {
public static void main(String args[]) {
Runnable runner = new Runnable() {
public void run() {
[Code]...
View Replies
View Related
Sep 4, 2014
I created a scanner object but it's not asking for any input. when I create a new scanner and then tell it to print it it just prints a bunch of weirdness when AFIK it should be asking me to type something and then it should repeat what i entered.
View Replies
View Related
May 26, 2014
I have a mysql database and I want to be able to search through a table for a key word in a column like:
words
my fat cat
the yellow canary
what blue cow
and if i put in the full string like "what blue cow" if finds a match, but I want to be able to put in "blue" and see if it finds a match
Java Code:
String keyword = t3.getText();
String sql1;
sql1= "SELECT * FROM table WHERE words ='"+keyword+"'";
rs = stmt.executeQuery(sql1);
while(rs.next()){
JOptionPane.showMessageDialog(null, "found match");
} mh_sh_highlight_all('java');
Im not sure how to do this using wild cards with user input (a JTextfield)
View Replies
View Related
Apr 13, 2014
I trying to get this code to get user input instead of reading from a hardcoded array. I'm getting compile errors while trying to get user input. Here's some of the code:
Java Code: // BubbleSort Java
// compile with: javac BubbleSort.java
// run with: java BubbleSort
[Code]....
View Replies
View Related
Apr 14, 2014
I would like to multiply the amount stated inside program by user input. I currently have
public static void setTicket(double ticket)
{
//if statement for tickets
if (ticket == 1)
Ticket = 10.00;
else if (ticket == 2)
Ticket = 20.00;
else if ( ticket == 3)
Ticket = 30.00;
The user should enter 2 and the value should show 20.00. This does work however im looking for a way to say enter 2 = 2*10 instead of stating each value individually.
View Replies
View Related
Jun 2, 2009
Suppose I write a program to multiply two numbers.
The user enters the numbers as follows:
2
3
How do I extract these integers inside main? [Explain the case where the integers are on two separate lines].
What to do for the newline inbetween?
View Replies
View Related
Jan 28, 2015
I have a program that asks you for your password and if it is incorrect it reduces your attempts until you are at zero, but I want to be able to tell the user what passwords they have attempted when they run out of attempts. I've tried a couple of things but how to get it to work. This is my code:
import javax.swing.JOptionPane;
public class PasswordV4 { //Begin Class
public static void main(String[] args) { //Begin main program
//initialize variable
int passwordAttempt = 5;
int value = 0;
[Code] ....
View Replies
View Related
Dec 12, 2014
I am new to java and I am trying to write a program where someone has to guess the number. The correct number is 3. If they are below three. It tells them that they are too low and to guess again. If they are too high, it tells them they are too high and to guess again.
However, I am having problems once they enter a number and they get the response "The number was too low. Try again" as it lets them try again but doesn't tell them if it's right, wrong, or too high. I'm not sure what piece of code I am missing. Here is what I have so far:
import java.util.Scanner;
public class Guessthenumber {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a number between 1-10.");
double value = input.nextDouble();
if(value == 3)
[Code] .....
View Replies
View Related
Sep 29, 2014
i need to output the unsorted array as well as the sorted array...the sorted array works but i can not get the unsorted array to work.
package Lab7;
import java.util.Scanner;
public class lab10 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner keyboard = new Scanner(System.in);
[Code]...
View Replies
View Related
Oct 16, 2014
My project is a program that uses methods to display strings in a variety of different ways. I already have all of that working, my problem is checking for user inputs.For example on the menu portion of my program i ONLY want the user to input 1-5.here is the method i developed for checking user input IT DOES CONTAIN ERRORS this is where i am having troubles.
public static int checkInput(String prompt, int lowerBound, int upperBound) {
int input = 0;
boolean done = false;
while ( !done )
{
System.out.println(prompt);
if (input.hasNextInt())
[code]....
here is the rest of my program which is working fine besides the checking input. i would like to implement this method for checking inputs into all places where the user is asked to input something.
import java.util.Scanner;
public class DisplayingStrings {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int getNum = checkInput("Enter #(1-5)", 1, 5);
System.out.println("--String Writer--");
System.out.print("Enter a string: ");
String word = input.next();
displayMenu(word);
[code]....
View Replies
View Related