This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes find number Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "find number" Watch "find number" New topic

find number

peterx peter

Joined: Nov 02, 2004
Posts: 19
How can I find the smallest number(nbr).
please help me.

class smallestNmr{
public static void main(String[]args){
int []v={7,2,9,3,5,4,9,2,2};

int nbr=0;
int first;
int secnd;
int k=0;
second= v[k+1];
first= v[ k ];

System.out.println(" "+nbr);
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Hi Peter:

The ending code tag should be [/CODE] not [CODE]. It is like html.

Your code looks nice but you did too much.

Just set nbr to v[0] and then iterate over the array, setting nbr to the smaller of nbr and the current element. When you are done, nbr will be the smallest value.

Hold onto your original code for when your class covers the bubble sort (my favorite).


Mike Gershman
SCJP 1.4, SCWCD in process
Greg T Robertson
Ranch Hand

Joined: Nov 18, 2003
Posts: 91
To figure this one out think about the actual process you would use in your head to determine which one is the lowest. You start by taking the first number and storing it in your memory. You then compare each of the other numbers in the array to the one stored in memory. If it's lower you update your memory with the lower one and continue on until you find another lower one. If you don't find another lower one by the time you are done - you have found the lowest number. Hope that helps.
peterx peter

Joined: Nov 02, 2004
Posts: 19
I agree. Here's the link:
subject: find number
jQuery in Action, 3rd edition