[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

core java. isInteger methods. Finding bug.

Posted on 2016-08-04
10
Medium Priority
?
129 Views
Last Modified: 2016-08-05
public static int isInputInteger(){
		@SuppressWarnings("resource")
		Scanner input = new Scanner(System.in);   

		int correctNumber;
		
			while(true){   
			input.nextLine();
				    if(input.hasNextInt()){   
				    	correctNumber = input.nextInt();
				        break;
				    }
				    else{
				    	System.out.println("Your input isn't integer.Please, try again. ");
				    }
			}
			return correctNumber;
			}

Open in new window


class PrintFromInput {

	protected static int  printNumber() {
    	System.out.printf("Please, enter integer: ");
    	int intYourConsoleInput = Assert.isInputInteger();
         return intYourConsoleInput; 
    }
}

public class Task {
    public static void main(String[] args) {
    	
    	System.out.println("Congratulation! Your correct integer input is: "+ PrintFromInput.printNumber()); 
    	
    }
}

Open in new window


Inside class Assert there is a method "isInputInteger" everything work but not as I expect :
Session 1
input : f
output: {nothing }
input : f
output : Your input isn't integer.Please, try again.
input: g
output : Your input isn't integer.Please, try again.
input : 3
Congratulation! Your correct integer input is: 3
Session OVER
My question is why there isn't any message after first "f". Please, help me to find bug. Thx in advance !
0
Comment
Question by:SunnyX
10 Comments
 
LVL 17

Expert Comment

by:krakatoa
ID: 41742650
At first glance, maybe because you are advancing the scanner too early.
0
 
LVL 16

Expert Comment

by:gurpsbassi
ID: 41742654
why don'y you try setting breakpoints and debugging to find out what going on?
1
 
LVL 17

Expert Comment

by:krakatoa
ID: 41742661
Remove this line :

input.nextLine();

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 9

Assisted Solution

by:James Bilous
James Bilous earned 200 total points
ID: 41742691
I believe OP needs the nextLine() read in order to advance the scanner past the '\n' character after the integer is read. I would simply move it to the end of the while loop instead of at the beginning.
0
 
LVL 17

Expert Comment

by:krakatoa
ID: 41742722
Yes, indeed.  I thought the OP would be able to work out the required code move once he sees the result of just deleting it.
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 200 total points
ID: 41742780
That code is wrong in so many ways that it's hard to know where to begin. Let's begin with the first line:

a. your code has the name of a boolean method yet it returns int. Why?
b. in the event of your replying that you misnamed the method then why do you skip a line of input (which could have contained an int)?
1
 
LVL 17

Accepted Solution

by:
krakatoa earned 1600 total points
ID: 41743428
I think if you just want to get your loop working, you can go along these lines:

while (true){
		System.out.println("Input a number");
		if(!(scanner.hasNextInt())){System.out.println("Your input isn't integer.Please, try again. ");scanner.next();continue;}		
		correctNumber = scanner.nextInt();			
}

Open in new window

0
 

Author Comment

by:SunnyX
ID: 41744053
The solution is :
public static int onlyIntegerInput(){
		@SuppressWarnings("resource")
		Scanner scanner = new Scanner(System.in);   

		int correctNumber;
		
		while (true){

			if(!(scanner.hasNextInt())){
				System.out.println("Your input isn't integer. Please, try again. ");
				scanner.next();
				continue;
				}
			else{
			correctNumber = scanner.nextInt();
			break;
			}
		}
			return correctNumber;
	}

Open in new window



Dear experts thx you so much for your help !
0
 

Author Closing Comment

by:SunnyX
ID: 41744055
Everybody many thanks !
0
 
LVL 17

Expert Comment

by:krakatoa
ID: 41744057
Yes, that's it.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month19 days, 5 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question