Finding and outputting largest number in a loop

Finding and outputting largest number in a loop, ending loop with a ne

Page 1 of 1

6 Replies - 9974 Views - Last Post: 24 October 2009 - 02:16 PM Rate Topic: -----

#1 jcs224  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 23-October 09

Finding and outputting largest number in a loop

Posted 23 October 2009 - 10:51 AM

Hey guys. I have been learning Java for the past 2 months in school. I have a little program here that is supposed to input any values above 0 (positive numbers), then return the largest number of those inputs as soon as I input -1.0. In this attempt I tried to create a loop that would cycle through until it found any negative number, then output the largest number. For example, it should look something like this in the terminal:

Please enter a bunch of values. To end, enter a negative number.
3.0
2.8
4.7
2.0
-1.0

The largest input value was 4.7

Right now I am at a loss; there is a compiler error at line 22 that says "variable largest might not have been initialised" (I'm using BlueJ compiler, by the way). There is probably something else wrong, too, so please let me know if so.

Also, I am NOT supposed to use arrays. Only for and while loops for now. We are learning about arrays next, they are NOT part of this assignment.

Thanks in advance.

 
/**
 * @author Joe Sweeney
 * @version 10/20/2009
 */
import java.util.*;

public class LoopTest
{
	public static double whileTest()
	{
		Scanner in = new Scanner(System.in);

		double inNumber;  
		double largest;
		
		int i = 0;
		while(i >= 0)
		{
			System.out.println("Please enter a bunch of values. To end, enter a negative number.");
			inNumber = in.nextDouble();
			
			if(inNumber > largest)
			{
				largest = inNumber;
			}
		}	 
		return largest;
		System.out.println("The largest input value was " + largest);
	}   
}



This post has been edited by jcs224: 23 October 2009 - 11:39 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Finding and outputting largest number in a loop

#2 Mikeyp926  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 29
  • View blog
  • Posts: 205
  • Joined: 20-March 09

Re: Finding and outputting largest number in a loop

Posted 23 October 2009 - 11:41 AM

You need to initialize largest first. If you aren't accepting negative values then maybe you could initialize it to 0.

double inNumber;
double largest = 0;

Also, right now your loop will never end. You might want to use the inNumber as the condition on your loop, because that is what changes every time. Initialize that to 0 also, and then check that it is greater than or equal to zero every time through.

I think that should work.
-Michael
Was This Post Helpful? 1
  • +
  • -

#3 jcs224  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 23-October 09

Re: Finding and outputting largest number in a loop

Posted 23 October 2009 - 04:55 PM

Hey! Thanks for your help. I think I am on the right track. However, I'm still stumped as to how to pull the largest number out of my inputs and display it in the final message, "The largest number is ".

Right now it simply puts the last number I entered into the final message. Here is what it looks like right now;

Please enter a bunch of values. To end, enter a negative number.
5
6
7
8
-2
The largest number is -2.0

But I need it to look like this

Please enter a bunch of values. To end, enter a negative number.
5
6
7
8
-2
The largest number is 8.0

I made some significant alterations to my code, hope I didn't go backwards with my progress.

Again, thanks for your help!

import java.util.*;

public class LoopTest
{
	public static double whileTest()
	{
		Scanner in = new Scanner(System.in);

		double inNumber = 0;  
		double largest = 0;
		int i = 0;
		
		System.out.println("Please enter a bunch of values. To end, enter a negative number.");
		
		while(inNumber >= 0)
		{
			inNumber = in.nextDouble();
		}	 
		
		if(inNumber < 0)
		{
			System.out.println("The largest number is " + inNumber);
		}
		return 0;
	}   
		
}



Was This Post Helpful? 0
  • +
  • -

#4 Mikeyp926  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 29
  • View blog
  • Posts: 205
  • Joined: 20-March 09

Re: Finding and outputting largest number in a loop

Posted 23 October 2009 - 06:33 PM

Hi,
You are definitely making progress, and I think you almost have it. I only see about two things you really need to change. First, after you have stored the input number to inNumber, you should see if that is larger than the largest. If it is, then store it in the largest variable. You can do that with a fairly simply if-statement like this:
while(inNumber >= 0)
		{
			inNumber = in.nextDouble();
			if(inNumber>largest)
			{
				  largest = inNumber;
			}
		} 



Then, once you are out of that loop and want to print out the largest result, print the largest, not the inNumber.
System.out.println("The largest number is " + largest);




Does that help?

-Michael
Was This Post Helpful? 1
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Finding and outputting largest number in a loop

Posted 23 October 2009 - 10:10 PM

 while(i >= 0)
		{
			System.out.println("Please enter a bunch of values. To end, enter a negative number.");
			inNumber = in.nextDouble();
			
			if(inNumber > largest)
			{
				largest = inNumber;
			}
		}	 



you never change the value of i to exit the loop

better to exit with a break

double largest = 0.0;
while(true) {
		   System.out.println("Please enter a bunch of values. To end, enter a negative number.");
			inNumber = in.nextDouble();
			if(inNumber < 0.0)
			   break;
			if(inNumber > largest)
			{
				largest = inNumber;
			}
}
System.out.println("Largest number: " + largest);


Was This Post Helpful? 0
  • +
  • -

#6 d0ulikethat  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 22-March 09

Re: Finding and outputting largest number in a loop

Posted 23 October 2009 - 11:53 PM

or exit with a boolean.

/**
   A class that takes in values and keeps track of the highest value.
*/
public class LoopTest
{  public static void main(String[] args)
	{  Scanner in = new Scanner(System.in);
	   double largestSoFar = 0;
	   boolean done = false;
		
	   System.out.println("A negative value ends the program.");
		
	   while(!done)
	   {  System.out.println("Enter a value:");
		  if (in.hasNextDouble())
		  {  double x = in.nextDouble();
			 if (x < 0)
			 {  System.out.println("The largest input value was " + largestSoFar);
				done = true;
			 }
			  
			 if (x > largestSoFar)
			 {  largestSoFar = x;
			 }
		  }
		}
	}
}


This post has been edited by d0ulikethat: 23 October 2009 - 11:59 PM

Was This Post Helpful? 0
  • +
  • -

#7 jcs224  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 23-October 09

Re: Finding and outputting largest number in a loop

Posted 24 October 2009 - 02:16 PM

Fantastic help, you guys. Thanks a bunch!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1