0

I need to figure out how to find the max number of an array by using a while loop. I have searched all over but do not understand how to do it. If I could get any help at all that would be great. Thanks!

Anyway the findMax method is the one that "is supposed to determine which number is the maximum of the array data.

// ReverseIntegers.java
// Nick Elliott 1/29/10

import java.io.*;
import java.util.Scanner;
public class ReverseIntegers
{
	//interactively reads in a list of integers, one per line(max of 10)
	// and prints out the list in reverse order
	public static void main(String [] args)throws IOException
	{
		Integer [] data = new Integer[10];
		
		System.out.println("Type in a list of integers, one per line." + "\nPress enter at a blank line to stop");
		
		int size = readData(data);
		int max = findMax(data,size);
		
		System.out.println("The largest integer in your data is " + max);
		
		
		System.out.println("\nYour data reversed: ");
		printDataReversed(data,size);
	}
	
	// interactively reads a list of integers into data array
	// returns number of integers stored
	public static int readData(Integer[]data) throws IOException
	{
		Scanner scanner = new Scanner(System.in);
		
		
		String line = null;
		int count=0;
		while( true )
		{
			line = scanner.nextLine();
			if(line.equals(""))
				break;
			data[count] = new Integer(line);
			count++;
		}
		return count; //number of data elements stored
	}
	
	//print data[0..size-1] in reverse order
	public static void printDataReversed(Integer[] data, int size)
	{
		int count=size-1;
		while(count>=0)
		{
		System.out.println(data[count]);
		count--;
		}
	}
	//Finds the max number in the aray and prints it
	public static int findMax(Integer[] data, int size)
	{  
	//No idea???
	System.out.println("The maximum integer is : ");
	}
}

Thank you for your time :)

2
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by BestJewSinceJC
Featured Replies
  • I mean you [I]could[/I] use a while loop, but it would be very unusual to do so in this situation. For example, consider the following array: [CODE]int[] myints = new int[10]; int i = 0; while (i < myints.length){ //do something i++; } //compare that improper use of while loop … Read More

0

You wouldn't use a while loop to loop over an array [in this case], you would use a for loop, since the size of the array is known in advance. To find the max number make an Integer variable max, write a for loop that loops over the entire array

for (int i = 0; i < array.length; i++)

then if your current integer you're looking at (which is array) is > 'max' then assign it to max.

0

Thanks i guess i'll have to use that...

You wouldn't use a while loop to loop over an array [in this case], you would use a for loop, since the size of the array is known in advance. To find the max number make an Integer variable max, write a for loop that loops over the entire array

for (int i = 0; i < array.length; i++)

then if your current integer you're looking at (which is array) is > 'max' then assign it to max.

Edited by nicklbmx: n/a

1

I mean you could use a while loop, but it would be very unusual to do so in this situation. For example, consider the following array:

int[] myints = new int[10];
int i = 0;
while (i < myints.length){
//do something
i++;
}

//compare that improper use of while loop to this:
for (int i =0 ; i < myints.length; i++){
//do something
}

Hope that helps.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.