Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9198381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:16:05+00:00 2026-06-17T22:16:05+00:00

How can I pull the first column from my 2D array? I am initialising

  • 0

How can I pull the first column from my 2D array? I am initialising my array in a method like this

//Initialise the array with the values from the file
public static float[][] data(float[][] data, Scanner scan){
    int count = 0; 

    for (int i=0;i<data.length;i++){
        for (int j=0;j<data[0].length;j++){
            count++;    
            if(count<data.length*data[0].length){
                for(int k=0;k<2; k++){

                    data[i][j] = (float)   IOUtil.skipToDouble(scan); 
                    System.out.print(data[i][j] + "  ");    
                }

            System.out.println();
            }   
        }

    }
    return data;    

}

This is the contents of my test file. (Please bare in mind the file could be any length)

13.97  2.2  
12.05  1.9  
9.99  1.5  
8.0  1.3  
6.0  0.9  
4.0  0.6  
2.0  0.3  
0.0  0.0  
-2.0  -0.3  
-4.0  -0.6  
-6.02  -0.9  
-8.0  -1.3  
-9.99  -1.6  
-12.03  -1.9  
-13.98  -2.2

on the terminal but the problem is when I try to call the left column I am getting the right column. I am not sure if this is because the values on the right are the only ones getting stored or not.

      //should print x but actually prints y 
   public static void printX(float[][] data){

        for (int i=0;i<data.length;i++){
            for (int j=0;j<data[0].length;j++){

                System.out.println(data[i][j]);
            }
        }
    }

which outputs

2.2
1.9
1.5
1.3
0.9
0.6
0.3
0.0
-0.3
-0.6
-0.9
-1.3
-1.6
-1.9
-2.2
0.0

Can anyone clarify how I could best get the data on the left hand column from the 2D array ? Or if there is some other method for achieving this sort of thing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:16:07+00:00Added an answer on June 17, 2026 at 10:16 pm

    You need to use the debugger on you IDE and step through your code to see exactly what is going on. The short answer to me looks like you are clobbering the same field with read data twice for k=0 and k=1.

    EDIT

    Also in your update, I think you are looking for this in your second print loop (not that it matters, could just as easily be 2, but this is better IMO):

    for (int j=0;j<data[i].length;j++){
    

    EDIT Here is the code I think you want explicitly:

    //Only print X
    public static void printX(float[][] data){
    
        for (int i=0;i<data.length;i++){
           System.out.println(data[i][0]);
        }
    }
    
    //Only print Y
    public static void printY(float[][] data){
        for (int i=0;i<data.length;i++){
           System.out.println(data[i][1]);
        }
    }
    
    //Print both
    public static void printBoth(float[][] data){
        for (int i=0;i<data.length;i++){
           System.out.println(data[i][0] + ", " + data[i][1]);
        }
    }
    
    
    //Print any number in array:
    public static void printAll(float[][] data){
        for (int i=0;i<data.length;i++){
            for (int j=0;j<data[i].length;j++){
                System.out.print(data[i][j];
                if (j+1 < data[i].length) {
                    System.out.print(", ");
                } else {
                    System.out.println();
                }
            }
        }
    }
    

    Finally, Your array is initializing incorrectly, which is why you are having so much trouble: You are actually writing both values to the X component, so the first time you loop in k you write a the X value to i,j (eg data[0][0]) and print it out. The second time you loop k you write the Y value to the same i,j (eg data[0][0]) again and print it out. This looks like you are printing the array you want, but actually you are writing both values to the same column. You really want something without the k loop:

    //Initialise the array with the values from the file
    public static float[][] data(float[][] data, Scanner scan){
        int count = 0; 
    
        for (int i=0;i<data.length;i++){
            for (int j=0;j<data[0].length;j++){
                count++;    
                if(count<data.length*data[0].length){
                    data[i][j] = (float)   IOUtil.skipToDouble(scan); 
                    System.out.print(data[i][j] + "  ");
                }    
            }
            System.out.println();
        }
        return data;    
    }
    

    If you want to enforce 2 columns which I think k is doing, something like:

    public static float[][] data(float[][] data, Scanner scan){
        for (int i=0;i<data.length;i++){
            for (int j=0;j<2;j++){
                data[i][j] = (float)   IOUtil.skipToDouble(scan); 
                System.out.print(data[i][j] + "  ");
            }
            System.out.println();
        }
        return data;    
    }
    

    count is also unnecessary as it is just a different way of controlling the total size of the array which it already handled / limited by looping each column (or in the second example where you know it is two wide) by the data.length / data[0].length

    EDIT Added by the OP:

    To clarify for anyone who still does not get it who maybe is confused about the same thing I was: I misunderstood where the values were being stored so this is why I set the problem up wrong and none of the short answers were making any sense.

    I took the whole thing a bit too literally, assuming that the value[here][] was holding the values from the first column and the value[][here] was holding the values from the second column.

    I am not sure why I held onto this, but I think it has something to do with how I had been using 1D arrays (without needing to tell them to have a column).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using jQuery deferred objects so I can pull data from multiple sources ,
It seems like R is really designed to handle datasets that it can pull
For Android GUI: I would like to create a window that I can pull
How can i pull data from different tables(models) into view in yii . actually
how can I pull only words with maximum 50 Characters from MYSQL? $query =
How can I pull relevant packet information from a JpCap packet? I am trying
This is the first time - I'm trying to do something like this -
it's my first time using arrays and I'm stuck on this. How can I
I want to include a Json file which holds data so I can pull
I currently have code in objective C that can pull out an integer's most

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.