1

Hey guys I am fairly new to java and I have this problem when trying to add numbers from a file called compact.txt into an array. Here is my code so far:

public void compactArray(){
    try{
        Scanner scan = new Scanner(new File("compact.txt"));
        while(scan.hasNextInt()){
            num++; 
        }
        int [] a = new int[num];
        Scanner in = new Scanner(new File("compact.txt"));
        while(counter < num){
            a[counter] = in.nextInt();
            counter++;
        }
        System.out.println(Arrays.toString(a));
    }catch(IOException bob){
        bob.getMessage(); 
    }
}

The problem with this code is that it never stops running. First my code reads the compact.txt and then counts the amount of numbers it has to figure out the size of the array. Then I make another scanner variable to add the numbers from compact.txt into the array. I use a counter variable as a way to stop when the desired amount of numbers are added into array a. I am not too sure what the problem is but it keeps on running and doesn't get to the line where it is supposed to print out the array. Can someone please help me. Thank you so much.

3 Answers 3

5

You should call

scan.nextInt();

in your first loop. You never move your cursor, as a result you keep reading the first element.

However, you solution require to go twice through your data set. You might want to use ArrayList, which is an array that can be resized. This way, you wouldn't need to count your file first.

Sign up to request clarification or add additional context in comments.

6 Comments

+1 for answering question as is and giving the hint to not read it twice.
If I call scan.nextInt() inside the first loop, how will I know how many items are in the array. Like dont you have to count the items in the array first and then make the array and finally add the items into it??
You can keep your num++ to count the element. But you also need to move your cursor to the next element using scan.nextInt() so you can exit your loop
@Bobby hasNextInt doesn't consume the int, so you are counting the same one over and over in your version.
These Java questions are like a race. Answer in seconds baby!
|
1

You are doing something wrong there: You should only use one Scanner object for this.

More specifically, what is going wrong in your case is the following: You are checking if the scanner has a next int in while(scan.hasNextInt()){, but you are never actually reading that int. So it will be looping forever.

Correct working code would be:

public void compactArray(){
    List<Integer> ints = new ArrayList<>();
    try{
        Scanner scan = new Scanner(new File("compact.txt"));
        while(scan.hasNextInt()){
            ints.add(in.nextInt());
        }
    }catch(IOException ex){
        ex.getMessage(); 
    }
    System.out.println(Arrays.toString(ints.toArray(new int[ints.size()])));
}

I also changed the following points of your code:

  • Internally now an List<Integer> is being used to store the integers. No need to do the counting anymore because of this!
  • Gave the exception a meaningful name.
  • In the System.out.println, now the List<Integer> is first being converted to an array, and then the String-representation is given.

1 Comment

Thank you for the detailed explanation. I now understand why and my code works :).
0

change

while(scan.hasNextInt()){ here is the problem, This loop never move to next integer. You need to call to scan.nextInt() required to move next integer
            num++; 
}

to

while(scan.hasNextInt()){
         scan.nextInt();
         num++; 
}

Comments

Your Answer

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.