I am new to Java language. I have been taken some personal tutorials, reading on my own and practicing by writing and solving exercise in the book I am using. Below is an exercise I am trying to solve.

Question.
(Find the Smallest Value) Write an application that finds the smallest of several integers.
Assume that the first value read specifies the number of values to input from the user.

Inline Code Example HereInline Code Example Here
See my solution in the code below;

package findingsmallestnumber;
import java.util.Scanner;


    int d;
    int s = 999999999;
    int smal;
    int k;
    public static void main(String[] args) {
      FindingSmallestNumber sn = new FindingSmallestNumber();
      sn.readFirstNumber();
    }
    public void readFirstNumber(){
    System.out.print("Enter number  ");
    Scanner ts = new Scanner(System.in);
    int p1 = ts.nextInt();
      k = p1;
     readNumber();
    }
    public void readNumber(){
    Scanner tp = new Scanner(System.in);

    while ( d < k){
    System.out.print("Enter number  ");
    int x1 = tp.nextInt();

     if(x1 < s)
     {
       smal = x1; 
       s = x1;
     }

    System.out.printf("s = %d\n",s);
    System.out.printf("d is %d, smal is %d\n",d,smal);
    d++;
    }
    System.out.printf("Smmallest number entered is %d\n",smal);
    }


    }

My question;

I want experienced java developers in this forum to help me review this code and point out areas of improvement. I will like someone to solve this same exercise using a shorter algorithm and approach that will avoid initializing s with 999999999 if possible. Just show me another way of solving the exercise.
Expecting your response...
Andy

firstly, don't overuse variables. if you need three, don't declare five.
use meaningfull names for you variables. it might be clear to you what s and k are supposed to do, or what they mean, but I have no clue.
also: write it out first. you might see it becomes a lot clearer (and easier) if you do.

int smallest = readFirstVariable();
int nextVar;
while(doYouWantToReadMore()){
  nextVar = readNextVariable();
  if ( smallest > nextVar ){
    smallest = nextVar;
  }
}

and that's about all you need.

Thanks stultuske for your quick reply. Your algorithm is truelly concise and I have taken note of what you have pointed out.
But this means I am going to develop three methods..readFirstVariable, doYouWantToReadMore and readNextVariable.

There's nothing wrong with 9...9 as a large int, but Integer.MAX_VALUE is the largest possible int, and nicely self-documenting.

Zubenna: no, that doesn't mean you have to create three methods at all. you already use a Scanner instance, you can use that to perform what those methods would have done.

I merely used those method names not to give you exact copy-ready code, so you can work a bit on it yourself.

Ok. I have really worked on it. It is ok now.Frequently, I will be posting exercises like this here so that I will learn from you and others.

Thanks so much.

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.