Asks for Integers till a negative is entered

Page 1 of 1

8 Replies - 584 Views - Last Post: 01 February 2013 - 10:35 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=310579&amp;s=8e44afcadb52634b2c65d0d3f787c20c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 TeaAnyOne

Reputation: 0
• Posts: 21
• Joined: 02-December 12

Asks for Integers till a negative is entered

Posted 31 January 2013 - 02:00 PM

Im trying to get this while loop to work. The program keeps asking for integers till a negative is entered then it prints out the average

I think the problem is in the while loop but im not sure. Any help?

```
public static void main(String[] args) {

int count = 0;
int sum = 0;
boolean finished = false;

while(!finished);{
Scanner myInput = new Scanner(System.in);
System.out.print("Enter an integer: ");
int n = myInput.nextInt();

if (n < 0)

finished = true;
else
sum = (sum + n);
count = count+1;

System.out.println(sum/count);

}

```

Is This A Good Question/Topic? 0

Replies To: Asks for Integers till a negative is entered

#2 jon.kiparsky

• Pancakes!

Reputation: 8561
• Posts: 14,799
• Joined: 19-March 11

Re: Asks for Integers till a negative is entered

Posted 31 January 2013 - 02:09 PM

When posting a problem, it's helpful if you tell us what the problem is - that way we can get right to helping you, and not spend time trying to figure out what it is you need.
If there's error output, copy that and post it, or if the program runs but doesn't do what you want it to, then describe what it does and what you want it to do instead.

That being said, this is a problem:

```while(!finished);{
```

Specifically, the semicolon means that you're doing this:

```while(!finished)
{
;  // empty instruction: do nothing
}
```

which means that if this compiles, it'll spin forever. However, that extra open curly brace probably means that you're getting a compiler error, because you're getting to the end of file before you get to the matching curly brace.

A better construction for that loop would look more like this:

```Scanner myInput = new Scanner(System.in);  // no need to make a new Scanner each time
while(!finished)  {
System.out.print("Enter an integer: ");
int n = myInput.nextInt();
if (n < 0)
finished = true;
else
sum = (sum + n);
count = count+1;
}
```

#3 TeaAnyOne

Reputation: 0
• Posts: 21
• Joined: 02-December 12

Re: Asks for Integers till a negative is entered

Posted 31 January 2013 - 03:56 PM

Thanks for the help!

Appreciate it

#4 burakaltr

• D.I.C Regular

Reputation: 92
• Posts: 279
• Joined: 07-November 10

Re: Asks for Integers till a negative is entered

Posted 01 February 2013 - 04:34 AM

You Pretty Much need code like below :

```import java.util.Scanner;

import javax.swing.JOptionPane;

class Until_Neg {

static public   void main(String ... args){
Scanner in=new Scanner(System.in);
int i=9;
boolean bool=true,cool=true;

int k;
k=1;
while(bool){
try{

i=in.nextInt();
if(i>0){
System.out.println("NEG PLS");
bool=true;

}
if(i<=0){bool=false;
break;

}
}
catch(Exception e){
bool=true;

}

}

if(i<0)
System.out.println("ENTERED :   "+i);

}

}

```

#5 jon.kiparsky

• Pancakes!

Reputation: 8561
• Posts: 14,799
• Joined: 19-March 11

Re: Asks for Integers till a negative is entered

Posted 01 February 2013 - 05:48 AM

Yeah not so much like that.

More like this should work:

```import java.util.Scanner;

class Average {
static public void main(String [] args){
Scanner in=new Scanner(System.in);
int i=0;
int count = 0;
float total = 0;
while(true){
try{
i=in.nextInt();
}
catch(InputMismatchException e){
continue;
}
if(i<0){
break;
}
total += i;
count += 1;
}
System.out.println("Average = " +total/count);
}

```

#6 burakaltr

• D.I.C Regular

Reputation: 92
• Posts: 279
• Joined: 07-November 10

Re: Asks for Integers till a negative is entered

Posted 01 February 2013 - 06:06 AM

Mr. jon_Kiparsky, when I run your code and enter a Non-Integer but a String, like aaabbbccc, it goes into infinite loop

#7 jon.kiparsky

• Pancakes!

Reputation: 8561
• Posts: 14,799
• Joined: 19-March 11

Re: Asks for Integers till a negative is entered

Posted 01 February 2013 - 07:37 AM

burakaltr, on 01 February 2013 - 08:06 AM, said:

Mr. jon_Kiparsky, when I run your code and enter a Non-Integer but a String, like aaabbbccc, it goes into infinite loop

Teach me to deal with code before I've dealt with coffee.

Consume a newline in the catch block to fix that problem.

#8 burakaltr

• D.I.C Regular

Reputation: 92
• Posts: 279
• Joined: 07-November 10

Re: Asks for Integers till a negative is entered

Posted 01 February 2013 - 10:29 PM

Sorry, I don't get it.

#9 jon.kiparsky

• Pancakes!

Reputation: 8561
• Posts: 14,799
• Joined: 19-March 11

Re: Asks for Integers till a negative is entered

Posted 01 February 2013 - 10:35 PM

The endless loop happens because there is a token in the Scanner - a newline - which is not an int, and this token is not removed from the Scanner by the repeated nextInt() call. Calling in.nextLine() in the catch block will clear the scanner and force the nextInt() to wait for an input.

This is just a little gotcha in the nextX methods of the Scanner class. One reason I always suggest that the user get nextLine and parse it themselves, at least until they are able to understand what it is that nextInt actually does.

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }