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);
}
}
3 Answers 3
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.
2 Comments
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
Comments
The process:
- Prompt user
- Validate input: IF input NaN skip, ELSE add to
ArrayList
ofIntegers
- Sort array list in ascending order
- 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.
intNum[j]=temp;
and explain to me what it's doing. Then change it to what it's supposed to be.i
,num
,number
,temp
andintNum
. How can you possibly keep track of what each one is for? Please use more informative names for all your variables.temp
to something? What usually happens if you write something likea = 5;
?