hey everyone. I am supposed to make a program that finds the smallest number out of a set of numbers. the amount of numbers input is up to the user. i have to print the smallest number, and what input it was (ex, 1st number 2nd number 3rd number...)

i have the code pretty much complete, however i cant figure out how to keep track of whether it was the first second or third number. Do i need to use a do while loop? can i do this with a normal while loop? i thought i could make a counter to keep track of it but i cant quit seem to get it...

this is what i have

import java.util.Scanner;
public class WhileLoops2 {
	
	public static void main(String[] args) {

		int smallest=0;
		int num;
		int counter=1;
		int numCounter = 0;
		int inputVals;
		
	Scanner input=new Scanner (System.in);
	
	System.out.println("Enter number of input values: ");
	inputVals = input.nextInt();
	System.out.printf("\nEnter a number:");
	num=input.nextInt();
	
		smallest=num;
		counter++;
		
		while (counter<=inputVals){
			
			System.out.println("Enter another number: ");
			num=input.nextInt();
			
				if (num<smallest)
					
					smallest=num;
				

				counter++;}
				
				
	
		System.out.println(smallest +" was the smallest number entered.");
		System.out.println(smallest +" was input integer number "+numCounter+".");
		

		}
	}

I'm pretty new to Java, but I'd recommend using an array to store the numbers.

hmm, thanks for the tip but i can not use an array

System.out.printf("\nEnter a number:");
	num = input.nextInt();
	
		smallest=num;
		[B]numCounter++;[/B]
while (counter<=inputVals){
			
			System.out.println("Enter another number: ");
			num = input.nextInt();
			
			if(num < smallest)
			[B]{[/B]
				smallest = num;
				[B]numCounter++;[/B]
			[B]}[/B]
				

			counter++;
		}

I think this should work. Increment numCounter when you get the first input, then increment it again every time an input number is smaller. counter is used to move through the loop.

Hello...

Why are you making the program so somplex.... :?:

Try using simple logic.... Life is complex.... Atleast lets have our prog as simple.... Try the below code....

// Ask the user how many number is he going to compare...

System.out.println("Please enter how many numbers you wanna compare....");
tot = input.nextInt();

int num[ ] = new int[tot];
for(int i=0; i<tot ; i++)
{

System.out.println(" Enter the number ");

num[i] = input.nextInt();
}

Now you have all the number... the "tot" value can keep an eye on counters. So now you can proceed with determinig the smallest.

--

Rigidboss

Another possbiel way is to use an arrylist. This is an array who's size does not have to be declared when it is created.

You should have 2 int vars: one that stores the smallest int, the other that stores the position of that int in the arraylist.

Every time you receive input, compare the lowest int var with the input. If the new input is lower, store that in the smallest int var, and store its position in the araylist.

I forgot to mention: every time input is received, insert it into the arraylist first thing.

Shout if you need help with the code :) although I just made a suggestion, and there are more than enough tutorials on arraylists, etc. online.

While use of arrays or an arraylist would make sense they are not necessary. The original program is almost there. It even has an integer declared to hold the value in numcounter.

What is needed is to record the current value of counter in numcounter when a new smaller value is found. That is, assign numcounter=counter in the if statement. (Create a block with {}).

While use of arrays or an arraylist would make sense they are not necessary. The original program is almost there. It even has an integer declared to hold the value in numcounter.

What is needed is to record the current value of counter in numcounter when a new smaller value is found. That is, assign numcounter=counter in the if statement. (Create a block with {}).

Yup that makes more sense than using arrays/arraylists at this point.

With this type loop, how can you print the number's back to the user, as to list the ones they entered? Ex.

while (count >= 1) {
					System.out.println(" Enter Number:\t ");
					number = input.nextDouble();
					sum += number;
					number++;
					}
System.out.print(" The numbers you entered are" + number + number1 + etc. + "\n");
System.out.print(" Total of numbers: ",sum);

If I'm thread hijacking, I apologize, but this is very close to the same program I'm working on. Thanx

To print the numbers back out you need to store them. This is where you need to use some sort of array or list. You could create a fixed length array using the input for the quantity of numbers entered of use an arraylist to make it more flexible. Each number is then added in to the array as they are entered.

I thought so too, but he said that he couldn't use an array list.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.