8 Replies - 435 Views - Last Post: 01 February 2013 - 10:35 PM Rate Topic: -----

#1 TeaAnyOne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • 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  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7746
  • View blog
  • Posts: 13,102
  • 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;
}

Was This Post Helpful? 3
  • +
  • -

#3 TeaAnyOne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • 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 :)
Was This Post Helpful? 0
  • +
  • -

#4 burakaltr  Icon User is offline

  • D.I.C Regular

Reputation: 91
  • View blog
  • Posts: 274
  • 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){
	System.out.println("Please enter a NEGATIVE Integer");
	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);

}

 
 }
	

Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7746
  • View blog
  • Posts: 13,102
  • 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){
	System.out.println("Please enter an Integer");
	Scanner in=new Scanner(System.in);
	int i=0;
	int count = 0;
	float total = 0;
	while(true){
		try{
			i=in.nextInt();
		}
		catch(InputMismatchException e){
			System.out.println("Please enter an integer");
		 	continue;
		}
		if(i<0){
			break;
		}	
		total += i;
		count += 1;
	}
	System.out.println("Average = " +total/count);
}



Was This Post Helpful? 1
  • +
  • -

#6 burakaltr  Icon User is offline

  • D.I.C Regular

Reputation: 91
  • View blog
  • Posts: 274
  • 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
Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7746
  • View blog
  • Posts: 13,102
  • Joined: 19-March 11

Re: Asks for Integers till a negative is entered

Posted 01 February 2013 - 07:37 AM

View Postburakaltr, 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.
Was This Post Helpful? 0
  • +
  • -

#8 burakaltr  Icon User is offline

  • D.I.C Regular

Reputation: 91
  • View blog
  • Posts: 274
  • 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.
Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7746
  • View blog
  • Posts: 13,102
  • 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.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1