user4225587
user4225587

Reputation: 1

How to populate 2 array's from one txt file

I'm not entirely grasping how to populate two arrays from 1 of many txt files that contain two lines of numbers. Here is my code thus far,

import java.util.*;
import java.io.*;
public class Merge
{
    public static void main(String[] args) throws IOException
    {
    Scanner scan = new Scanner(System.in);

    int[] arr1 = new int[7];
    int[] arr2 = new int[9];
    int[] arr3 = new int[arr1.length + arr2.length];
    int a = 0, b = 0, c = 0;

    try{
        System.out.print("Enter the filename: ");
        String input = scan.nextLine();

        File file = new File(input);
        Scanner inputFile = new Scanner(file);

        while(inputFile.hasNext() && a < arr1.length && b < arr2.length){
             arr1[a] = inputFile.nextInt();
             arr2[b] = inputFile.nextInt();
             System.out.println(arr1[a] + " " + arr2[b] + " ");
        }
        inputFile.close();
    }
    catch(FileNotFoundException fnfe){
        System.out.println("FILE NOT FOUND");
        System.exit(0);
    }
    }
}  

The program compiles and runs but I really don't know what to make of the output and how to begin sorting and merging them. Do I need another while loop or for loop to traverse the elements individually? The text file is values1.txt has the following values.

1 25 38 43 57 58 71
2 3 4 26 41 42 59 60 61

Upvotes: 0

Views: 205

Answers (2)

drew moore
drew moore

Reputation: 32680

You have 3 big problems:

First, it sounds like you want to read the first line into arr1 and the second line into arr2, and that's not what you're doing with:

//a = b = 0
while(inputFile.hasNext() && a < arr1.length && b < arr2.length){
     arr1[a] = inputFile.nextInt();
     arr2[b] = inputFile.nextInt();
     a++; 
     b++;
}

This is taking the first value (on the first line) and placing it in arr1[a], the second value on (on the first line) and placing it in arr2[b], etc. So you'd end up with:

arrA = [1, 38, 57, 71, 3, 26, 42]
arrB = [25, 43, 58, 2, 4, 41, 59, 0, 0]

You need to break the input file into lines, then iterate through them - we'll get back to that.

Second: The a++; b++ in my code above, is not in yours - both a and b are always 0, so with each iteration you're just placing two new values in the same slots, replacing the previous ones. So, as you have it written, your code produces:

arr1 = [60, 0, 0, 0, 0, 0, 0]
arr2 = [61, 0, 0, 0, 0, 0, 0, 0, 0]

Third, that while condition is a mess: See @SergeBallesta's answer for one definite problem, and there are other potential ones. As a general rule of thumb, anytime you find yourself writing && more than twice in a while/if condition, stop and think hard about whether it could be broken up: Writing simple/clean code != writing as few lines as possible

In this case, breaking it up goes hand in hand with solving our first problem.

The point of the exercise is for you to figure out how to do this, so I'm going to leave that to you. Feel free to post a comment if you get stuck/don't understand something I've said.

Also: Arrays.toString() gives you a handy way to print the contents of an array. If you throw these two lines at the bottom of your main:

System.out.println("arr1:" + Arrays.toString(arr1));
System.out.println("arr2:" + Arrays.toString(arr2));

You'll see the final outputs each time you run it.

Upvotes: 1

Serge Ballesta
Serge Ballesta

Reputation: 148975

It will break because you have a total of 9 + 7 = 16 integers. With your loop, you will read 2 * 8 and arr1 is only of size 7. So you will only read 14 integers and won't affect them correctly.

You need to read a full line at a time into a string, splits that string on spaces ("\s+" see javadoc of String.split() ...)and convert each item to an int to store it in your arrays.

Upvotes: 0

Related Questions