Reputation: 93
I'm a new student at programming and my college is teaching Java.
I was doing this exercise from my homework, and can't figure out why it only returns 0.
I've been looking at the internet for the past couple of days before I decided to post here. I read somewhere that Java never changes the value 0 if I start the variable with it (i7 = 0), but even when I change to i7 = 1, it returns 0 and the array is no more of 8 indexes, but 7. Also, wasn't it supposed to start filling the array at the moment I input numbers with the keyboard? Maybe I misunderstood something? How can I make it show the smallest number?
Thanks!
//Read and array of 8 indexes and find the smallest number
I have:
int array1[] = new int[8];
int i7;
int smallest = array1[0];
System.out.println("Type 8 numbers.");
for (i7 = 0; i7 < array1.length; i7++)
{
array1[i7] = keyboard.nextInt();
if (array1[i7] < array1[0])
{
smallest = array1[i7];
}
}
System.out.println("The smallest number is " + smallest);
Upvotes: 2
Views: 343
Reputation: 5661
Below I have annotated your code with the location of problems or their cause:
int array1[] = new int[8]; // create an array of 8 ints. all are initially 0.
int i7;
int smallest = array1[0]; // set smallest to 0.
System.out.println("Type 8 numbers.");
for (i7 = 0; i7 < array1.length; i7++)
{
array1[i7] = keyboard.nextInt(); // potential problem, what is 'keyboard'?
if (array1[i7] < array1[0]) // compare last read against first read
{
smallest = array1[i7];
}
}
System.out.println("The smallest number is " + smallest);
Now, here is what I would do to fix it:
int array1[] = new int[8]; // create an array of 8 ints. all are initially 0.
int i7;
int smallest = Integer.MAX_VALUE; // set the smallest to (2^31)-1; the largest int.
System.out.println("Type 8 numbers.");
for (i7 = 0; i7 < array1.length; i7++)
{
array1[i7] = keyboard.nextInt();
if (array1[i7] < smallest) // compare last read against current smallest
{
smallest = array1[i7];
}
}
System.out.println("The smallest number is " + smallest);
Another potential source or problems is your call to keyboard.nextInt()
. What class of object is keyboard
? It could be returning 0 at each call. If you run the fixed code without supplying 0
as one of the inputs and still get 0
as the smallest, then you know that your keyboard.nextInt()
call has problems.
Upvotes: 1
Reputation: 14039
You initialize smallest to array1[0] which is still not initialized from user input - i.e. it starts with 0.
Now, to fix the above issue, you compare with array[0] instead of smallest in the if
- but this isn't the right fix.
int array1[] = new int[8];
int i7;
System.out.println("Type 8 numbers.");
array1[0] = keyboard.nextInt();
int smallest = array1[0]
for (i7 = 1; i7 < array1.length; i7++)
{
array1[i7] = keyboard.nextInt();
if (array1[i7] < smallest)
{
smallest = array1[i7];
}
}
System.out.println("The smallest number is " + smallest);
Alternate solution.
int array1[] = new int[8];
int i7;
int smallest;
System.out.println("Type 8 numbers.");
for (i7 = 0; i7 < array1.length; i7++)
{
array1[i7] = keyboard.nextInt();
if(i7 == 0)
{
smallest = array1[0];
}
else
{
if (array1[i7] < smallest)
{
smallest = array1[i7];
}
}
}
System.out.println("The smallest number is " + smallest);
I prefer the 1st one, though.
Upvotes: 2
Reputation: 105
You didn't actually asked the user for the numbers did you?
Try adding this before your for call:
Scanner sc = new Scanner(System.in);
for(int z=0;z < 8;z++){
array1[z] = new Integer(sc.nextLine()).intValue();
}
Upvotes: 0
Reputation: 18148
array1[i7] < array1[0]
always performs the exact same comparison; you want array1[i7] < smallest
In addition, array1[0]
equals zero after it's initialized, so smallest
will always equal 0 unless you're entering negative numbers. Instead, initialize smallest = Integer.MAX_VALUE
. This will initialize smallest
to the largest possible integer, so any numbers you enter will (probably) be smaller.
Upvotes: 0