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

  • Home
  • SEARCH
  • 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 9197265
In Process

The Archive Base Latest Questions

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

I have problem with conversion from String into two dimension int array. Let’s say

  • 0

I have problem with conversion from String into two dimension int array.
Let’s say I have:

String x = "1,2,3;4,5,6;7,8,9"

(In my program it will be String from text area.) and I want to create array n x n

int[3][3] y = {{1,2,3},{4,5,6},{7,8,9}} 

(Necessary for next stages.) I try to split the string and create 1 dimensional array, but I don’t have any good idea what to do next.


As you suggest I try split at first using ; then , but my solution isn’t great. It works only when there will be 3 x 3 table. How to create a loop making String arrays?

public int[][] RunMSTFromTextFile(JTextArea ta)
    {
        String p = ta.getText();
        String[] tp = p.split(";");

        String tpA[] = tp[0].split(",");
        String tpB[] = tp[1].split(",");
        String tpC[] = tp[2].split(",");

        String tpD[][] = {tpA, tpB, tpC};

        int matrix[][] = new int[tpD.length][tpD.length];

        for(int i=0;i<tpD.length;i++)
        {
            for(int j=0;j<tpD.length;j++)
            {
                matrix[i][j] = Integer.parseInt(tpD[i][j]);
            }
        }
        return matrix;
    }
  • 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:01:53+00:00Added an answer on June 17, 2026 at 10:01 pm

    After using split, take a look at Integer.parseInt() to get the numbers out.

    String lines[] = input.split(";");
    int width = lines.length;
    String cells[] = lines[0].split(",");
    int height = cells.length;
    int output[][] = new int[width][height];
    
    for (int i=0; i<width; i++) {
        String cells[] = lines[i].split(",");
        for(int j=0; j<height; j++) {
            output[i][j] = Integer.parseInt(cells[j]);
        }
    }
    

    Then you need to decide what to do with NumberFormatExceptions

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

Sidebar

Related Questions

Let's say that I have a boost::variant<std::string, int> myVariant; In this object I keep
I have this generic string to number conversion : enum STRING_BASE : signed int
I have a String to Date conversion problem using SQL Bulkcopy in asp.net 3.5
I have problem with UIWebView delay when the load image from url. In my
I have problem while loading data into html select when users press or click
The problem I have a byte[] that is converted to a hex string, and
I've been getting the error Conversion failed when converting from a character string to
I have a complex database conversion console app that reads from an old database,
I know I'm having a problem with a conversion from Unicode but I'm not
I have a question regarding type conversion in C#. Object data_obj = test; string

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.