Hi,
I am having issue with the Scanner class, it seems to fall through to the next line, depending on how I call it from my class. I cannot track find a pattern to this. Except it tend to happen more when calling the nextLine() from the Scanner class.
Some info - I decided to write my self a simple class to help me with my uni work. It is just to save me writing to screen then using the scanner class to read the input. It would be easier with a one line of code and a class.
anInt = PromptGetInput.typeInt("Please enter the first int: ");
Basically I call the class with the desired method (that related to the data type that i would like to read in from the keyboard). I pass in a string as an argument and the method displays the string to the screen and then reads in the relative data type from the scanner class. The input is then returned.
Here is the class -
import java.util.*;
public class PromptGetInput
{
private static Scanner keyb = new Scanner(System.in);
/**
* The method takes a String arguement and prints the string to screen.
* The method then uses a Scanner class object to read an integer in from
* the keyboard. <ul>Method has Input validation</ul> and loops until a
* valid input has been achieved.
* <p>
* The int is automatically initialised to 0 upon declaration
* The method returns the input integer.
*
* @param String - the message that is to be displayed to screen
* @return Int - is returned
*/
public static int typeInt(String message)
{
int userInputInt = 0;
boolean validInput = false;
do
{
try
{
System.out.println(message);
userInputInt = keyb.nextInt();
validInput = true;
}
catch (InputMismatchException e)
{
System.out.println("Invalid data type.. try again");
keyb.next(); // discard the bad input
validInput = false;
}
}while(!validInput);
//keyb.close();
return(userInputInt);
}
/**
* The method takes a String arguement and prints the string to screen.
* The method then uses a Scanner class object to read a String in from
* the keyboard. <ul>No input validation on this method</ul>.
* <p>
* The String is automatically initialised to a blankspace upon declaration
* The method returns the input String.
*
* @param String - the message that is to be displayed to screen
* @return String - is returned
*/
public static String typeString(String message)
{
//String userInputString = new String(" ");
boolean validInput = false;
System.out.println(message);
String userInputString = keyb.nextLine();
//keyb.close();
return(userInputString);
}
/**
* The method takes a String arguement and prints the string to screen.
* The method then uses a Scanner class object to read a char in from
* the keyboard. <ul>No Input validation</ul> the method, trims leading
* whitespace and returns the first character.
* <p>
* The char is automatically initialised to a blankspace upon declaration
* The method returns the input char.
*
* @param String - the message that is to be displayed to screen
* @return char - is returned
*/
public static char typeChar(String message)
{
//char userInputChar = ' ';
boolean validInput = false;
System.out.println(message);
char userInputChar = keyb.nextLine().trim().charAt(0);
validInput = true;
//keyb.close();
return(userInputChar);
}
/**
* The method takes a String arguement and prints the string to screen.
* The method then uses a Scanner class object to read a float in from
* the keyboard. <ul>Method has Input validation</ul> and loops until a
* valid input has been achieved.
* <p>
* The float is automatically initialised to 0 upon declaration
* The method returns the input float.
*
* @param String - the message that is to be displayed to screen
* @return float - is returned
*/
public static float typeFloat(String message)
{
float userInputFloat = 0;
boolean validInput = false;
do
{
try
{
System.out.println(message);
userInputFloat = keyb.nextFloat();
validInput = true;
}
catch (InputMismatchException e)
{
System.out.println("Invalid data type.. try again");
keyb.next(); // discard the bad input
validInput = false;
}
}while(!validInput);
//keyb.close();
return(userInputFloat);
}
/**
* The method takes a String arguement and prints the string to screen.
* The method then uses a Scanner class object to read a double in from
* the keyboard. <ul>Method has Input validation</ul> and loops until a
* valid input has been achieved.
* <p>
* The double is automatically initialised to 0.0 upon declaration
* The method returns the input double.
*
* @param String - the message that is to be displayed to screen
* @return double - is returned
*/
public static double typeDouble(String message)
{
double userInputDouble = 0.0;
boolean validInput = false;
do
{
try
{
System.out.println(message);
userInputDouble = keyb.nextDouble();
validInput = true;
}
catch (InputMismatchException e)
{
System.out.println("Invalid data type.. try again");
keyb.next(); // discard the bad input
validInput = false;
}
}while(!validInput);
//keyb.close();
return(userInputDouble);
}
}
/*Here is the main that i am testing it with - */
public class PromptGetTest
{
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
int testInt;
char testChar;
String testString;
float testFloat;
double testDouble;
testInt = PromptGetInput.typeInt("Give me an Int");
System.out.println(testInt);
testFloat = PromptGetInput.typeFloat("Give me an float");
System.out.println(testFloat);
testDouble = PromptGetInput.typeDouble("Give me an double");
System.out.println(testDouble);
testString = PromptGetInput.typeString("Give me an String");
System.out.println(testString);
testChar = PromptGetInput.typeChar("Give me a char");
System.out.println(testChar);
return;
}
}
Here is the output -
run:
Give me an Int
33
33
Give me an float
3.3
3.3
Give me an double
2.222
2.222
Give me an String <--- see how it fall straight through here
Give me a char
ssss
s
--
If I call the typeChar straight after the typeInt then I get an error which is this -
run:
Give me an Int
33
33
Give me a char <--- fell straight though here.
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(String.java:687)
at PromptGetInput.typeChar(PromptGetInput.java:102)
at PromptGetTest.main(PromptGetTest.java:30)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)
Ok hope someone can help..
Thanks guys..
Nate