Reputation: 23
Below is the program, I couldn't find the error, can someone help? Thanks in advance.
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class Proj2b {
public static void main(String[] args){
int i=0;
int[] intArray = new int[100];
try
{ Scanner s = new Scanner(new File("data.txt"));
while(s.hasNextInt()){
intArray[i++] = s.nextInt();
}
int small= intArray[0];
for(int j=0;j<intArray.length;j++) {
if(intArray[j]<small)
small=intArray[j];
}
System.out.println(small);
}
catch(IOException e)
{ System.out.println(e);
}
}
}
data.txt. 219 67 3 12 35 34 86 29 8 30 312 22 91 51 73 10 21 88 6
Upvotes: 0
Views: 90
Reputation: 1568
I am assuming the reason why you're getting that error is because the file data.txt is not in your class path. "data.txt" should be in your project's base directory, not in any of the directories that NetBeans has created for you but it could also be the size of your array
Upvotes: -1
Reputation: 2648
Well you are initializing an array to be a fixed size 100:
int[] intArray = new int[100];
The intArray.length
value will be 100, so you are checking a bunch of uninitialized values in your for loop. Without running this, I assume they will be 0. Can you give us more info about what is printing out in the System.out.println statement?
To fix this, I think you can replace this line:
for(int j=0;j<intArray.length;j++) {
with this:
for(int j=0;j<i;j++) {
since the variable i
will hold how many integers were read in from the file.
Upvotes: 2