0

I'm trying to find the smallest number in a array of 1000 possible slots but my code keeps returning 0 even though 0 is not one of my inputs. My problem is in the last for loop, the rest of the code works. Here is my code:

import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;
public class SmallestNumber
{
 public static boolean isInteger(String num)
 {
 boolean again=false;
 try 
 { 
 int d= Integer.parseInt(num); 
 again=true;
 } 
 catch(NumberFormatException e)
 { 
 again=false; 
 }
 return again; 
}
public static void main(String[] args) 
{
 int [] intNum = new int[1000];
 int i=0;
 String num;
 boolean repeat = false;
 String done="done";
 Scanner inData = new Scanner(System.in);
 System.out.println("You can enter up to 1000 integers." + "\n" + "Enter 'done' to finish");
 while (!repeat)
 {
 System.out.print("Int: ");
 num=inData.next();
 repeat=isInteger(num);
 if (repeat==false)
 { 
 String entry=num.toUpperCase();
 boolean equals=entry.equals("DONE");
 if (equals==true)
 {
 repeat=true;
 }
 else
 {
 System.out.println("Error: you did not enter a valid chracter. Please enter a interger or state 'done'");
 repeat=false;
 }
 }
 else
 {
 int number=Integer.parseInt(num);
 intNum[i]=number;
 i=i+1;
 if(i<1000)
 {
 repeat=false;
 }
 else
 {
 repeat=true;
 }
 }
 } 
 int temp=intNum[0];
 for(int j=1;j<intNum.length;j++)
 {
 if (intNum[j]<temp)
 {
 intNum[j]=temp;
 }
 else
 {
 }
 }
 System.out.print(temp);
 }
}
asked Nov 13, 2014 at 23:05
11
  • 1
    Time to lean how to use your IDE's debugger. Really. Commented Nov 13, 2014 at 23:07
  • 1
    Look at this line - intNum[j]=temp; and explain to me what it's doing. Then change it to what it's supposed to be. Commented Nov 13, 2014 at 23:08
  • Also, you have variables called i, num, number, temp and intNum. How can you possibly keep track of what each one is for? Please use more informative names for all your variables. Commented Nov 13, 2014 at 23:10
  • It sets the value of temp to the array value at j? (Sorry if this seems elementary, I'm a beginner....) Commented Nov 13, 2014 at 23:10
  • 1
    Are you sure it sets the value of temp to something? What usually happens if you write something like a = 5; ? Commented Nov 13, 2014 at 23:11

3 Answers 3

1

You didn't say how many integers you are actually entering, but the problem is that you're iterating intnum.length times. You've declared your input fields as an array of 1000 elements, length will always be 1000 even if the user has entered fewer integers than that. Once your code has flown past the integers you actually entered, it's going to hit the initialized 0s of the array.

answered Nov 13, 2014 at 23:15

2 Comments

That makes sense, is there a way to set the length of the for loop to only the initialized elements of the array?
There is not. The array is just 1000 integers to Java, so the initial zeros are just as valid to it as your data entry. You'll have to keep track of how many valid entries you recorded, which it looks like you're doing with the i variable already.
0

Hi I solved the problem by changing the length of the ending for loop to i and switching

intNum[j]=temp; 

to

temp=intNum[j];

Thanks again

answered Nov 13, 2014 at 23:26

Comments

0

The process:

  1. Prompt user
  2. Validate input: IF input NaN skip, ELSE add to ArrayList of Integers
  3. Sort array list in ascending order
  4. Value at index 0 is the smallest

Everything else in your that doesn't fit this process is either out of place or not needed.

UPDATE:

public static void main(String[] args)
{
 List<Integer> numbers = new ArrayList<Integer>(1000);
 Scanner inData = new Scanner(System.in);
 System.out.println("You can enter up to 1000 integers." + "\n"
 + "Enter 'done' to finish");
 String input = "";
 do
 {
 input = inData.next();
 try
 {
 numbers.add(Integer.parseInt(input));
 }
 catch(NumberFormatException e)
 {
 System.out.println(input + " is not a number. Skipping...");
 }
 } while (!input.equalsIgnoreCase("done"));
 Collections.sort(numbers);
 inData.close();
 System.out.println("Smallest number: " + numbers.get(0));
}

This does exactly what the title suggest without all the extras. It was suggested by David Wallace that might be overkill. I am not sure it is. Don't have time to benchmark. I have to pack for a trip tomorrow. Maybe someone could comment on it.

answered Nov 13, 2014 at 23:26

1 Comment

Sorting the array seems overkill to me. You only need one pass through it to find the lowest number. The OP's original algorithm is more efficient than this - he/she just had a couple of small mistakes in the implementation of it.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.