6

Finding the Smallest Number in an Array Error

 2 years ago
source link: https://www.codesd.com/item/finding-the-smallest-number-in-an-array-error.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Finding the Smallest Number in an Array Error

advertisements

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);

        }

}


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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK