line break left over after scanner.nextInt?

M

Mark

hi..so i started writing my own mini IO library because i got
frusterated with java's crappy input. anyways.. after calling my
getInt() function and then my getString() function..it seems to detect
that no string was inputted (it throws my EmpyStringException).. i'm
assuming this is from a left over \n after reading an integer..but I
don't know how to fix the problem. any suggestions?

here's the class

import java.io.*;
import java.util.*;

public class IO
{
Scanner scanner;

IO()
{
scanner = new Scanner(System.in);
}

public int getInt(String prompt)
{
int i; // integer to be returned

while( true ) // ends when integer is returned
{
try
{
System.out.print(prompt);
return scanner.nextInt();
}
catch( InputMismatchException ioe )
{
System.out.println("Please enter an integer.");
scanner.nextLine(); // discard input so user can try again
}
}
}

public boolean getBool(String prompt)
{
String s;

if( scanner.hasNextLine() ) // sometimes there is a new line
character left over from getting an integer...
scanner.nextLine(); // let's get rid of it!!

while( true ) // ends when integer is returned
{
s = getString(prompt);
s = s.toLowerCase();
if( s.equals("yes") || s.equals("y") || s.equals("true") ||
s.equals("t") || s.equals("1") )
return true;
if( s.equals("no") || s.equals("n") || s.equals("false") ||
s.equals("f") || s.equals("0") )
return false;
System.out.println("Please enter \"yes\" or \"no\".");
}
}

public int getIntRange(String prompt, int min, int max)
{
int i; // integer to be returned

if( min > max ) // switch min and max
{
int temp = min;
min = max;
max = temp;
}

while( true ) // ends when integer is returned
{
try
{
System.out.print(prompt);
i = scanner.nextInt();
if( i<min || i >max ) throw new OutOfRangeException();
return i;
}
catch( InputMismatchException ioe )
{
System.out.println("Please enter an integer.");
scanner.nextLine(); // discard input so user can try again
}
catch( OutOfRangeException oore )
{
System.out.println("Integer must be between "+min+" and "+max+"
(inclusive).");
}
}
}

public int getPosInt(String prompt)
{
int i; // integer to be returned

while( true ) // ends when integer is returned
{
try
{
System.out.print(prompt);
i = scanner.nextInt();
if( i<=0 ) throw new LessThanOrEqualToZeroException();
return i;
}
catch( InputMismatchException ioe )
{
System.out.println("Please enter an integer.");
scanner.nextLine(); // discard input so user can try again
}
catch( LessThanOrEqualToZeroException ltoetze )
{
System.out.println("Please enter a positive enteger.");
}
}
}

public String getString(String prompt)
{
String s; // string to be returned

while( true )
{
try {
System.out.print(prompt);
//if( scanner.hasNextLine() ) // sometimes there is a new line
character left over from getting an integer...
//scanner.nextLine(); // let's get rid of it!!
s = scanner.nextLine();
s = s.trim();
if( s.equals("") ) throw new EmptyStringException();
return s;
}
catch( EmptyStringException ese )
{
System.out.println("You must type something!");
}
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,083
Messages
2,570,591
Members
47,212
Latest member
RobynWiley

Latest Threads

Top