Solved

core java. isInteger methods. Finding bug.

Posted on 2016-08-04
10
87 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 15

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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

831 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