Solved

core java. isInteger methods. Finding bug.

Posted on 2016-08-04
10
90 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 16

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 16

Expert Comment

by:krakatoa
ID: 41742661
Remove this line :

input.nextLine();

Open in new window

0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 8

Assisted Solution

by:James Bilous
James Bilous earned 50 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 16

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 50 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 16

Accepted Solution

by:
krakatoa earned 400 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 16

Expert Comment

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

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Application launch issue with Apache Tomcat 5 62
jar file executable 12 58
tomcat not starting 6 62
ejb example issues 3 18
Article by: Nadia
Suppose you use Uber application as a rider and you request a ride to go from one place to another. Your driver just arrived at the parking lot of your place. The only thing you know about the ride is the license plate number. How do you find your U…
The greatest common divisor (gcd) of two positive integers is their largest common divisor. Let's consider two numbers 12 and 20. The divisors of 12 are 1, 2, 3, 4, 6, 12 The divisors of 20 are 1, 2, 4, 5, 10 20 The highest number among the c…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

789 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