user188979
user188979

Reputation: 29

Store string into int[] array

I have a string String strings ="100.122.323.344;543.433.872.438;218.544.678.322"; I want to store into int[] like this int[] ={{100,122,323,344},{543,433,872,438},{218,544,678,322}} Below is the sample code

public static void main(String[] args) {
    // TODO Auto-generated method stub

    String strings = "100.122.323.344;543.433.872.438;218.544.678.322";
    strings = strings.replace(".", ",");
    System.out.println(strings);
    String[] coordinates = strings.split(";");
    String[] rect=null;
    int[] intcoordinates;
    for(int i=0;i<coordinates.length;i++)
    {
        //System.out.println(coordinates[i]);
        rect= coordinates[i].split(",");

        for(int j=0;j<rect.length;j++)
        {



        }

    }

}

Till now i am able to separate value from string but don't know how to convert to int please help

Upvotes: 0

Views: 446

Answers (4)

Kylo Zhang
Kylo Zhang

Reputation: 34

First, you want to store the value as int, you should know the {{100,122,323,344},{543,433,872,438},{218,544,678,322}} is a two dimensional array, so it's int[][]. Then the array must be init, the row and col can be figured out .

    int[][] intcoordinates = new int[3][4]; // init the array, the row and the col can be figured.
    for(int i=0; i<coordinates.length; i++)
    {
        //System.out.println(coordinates[i]);
        rect = coordinates[i].split(",");
        for(int j=0; j<rect.length; j++)
        {
            intcoordinates[i][j] = Integer.parseInt(rect[j]);
        }

    }
    System.out.println(Arrays.deepToString(intcoordinates));

The final execution is [[100, 122, 323, 344], [543, 433, 872, 438], [218, 544, 678, 322]]. To get the output {{100,122,323,344},{543,433,872,438},{218,544,678,322}}, you must make some additional methods. So if you just want to get the output, i'd say just replace the . to ,, ; to },{, and add the curly braces.

Upvotes: 1

Dmitry Nikiforov
Dmitry Nikiforov

Reputation: 3038

Conceptually Scanner and collections should work for you:

        String strings = "100.122.323.344;543.433.872.438;218.544.678.322";
    Scanner scanner = new Scanner(new ByteArrayInputStream(strings.getBytes()));
    scanner.useDelimiter(";");
    List<List<Integer>> list = new ArrayList<>();

    while(scanner.hasNext())
    {
        Scanner scanner2 = new Scanner(new ByteArrayInputStream(scanner.next().getBytes()));
        scanner2.useDelimiter("\\D");
        List<Integer> lst = new ArrayList<>();
        while(scanner2.hasNextInt())
            lst.add(scanner2.nextInt());
        list.add(lst);  
    }
    System.out.println("Lists: ");
    for(List<Integer> lst : list)
        System.out.println(lst);

Upvotes: 0

Brian
Brian

Reputation: 13571

You may use

int intrect = Integer.parseInt(rect[j]);

to convert your rect[j] to int.

public static void main(String[] args) {
    // TODO Auto-generated method stub

    String strings = "100.122.323.344;543.433.872.438;218.544.678.322";
    strings = strings.replace(".", ",");
    System.out.println(strings);
    String[] coordinates = strings.split(";");
    String[] rect = null;
    int[][] intcoordinates = new int[3][4];  // initialize the size of the array
    for (int i = 0; i < coordinates.length; i++) {
        // System.out.println(coordinates[i]);
        rect = coordinates[i].split(",");        
        for (int j = 0; j < rect.length; j++) {
            intcoordinates[i][j] = Integer.parseInt(rect[j]);
        }
    }
}

intcoordinates contains [[100, 122, 323, 344], [543, 433, 872, 438], [218, 544, 678, 322]] at the end of the execution. This is the final result of the conversion.

Upvotes: 1

Masudul
Masudul

Reputation: 21961

Initialize a two dimentional array intcoordinates with rows and cols. If don't know the row size, than you should count maximum row size. Than convert String to int and assign to this array.

 int[][] intcoordinates=new int[coordinates.length][countMaxRow(coordinates)];
 for(int i=0;i<coordinates.length;i++){
    rect= coordinates[i].split(",");
    for(int j=0;j<rect.length;j++){
       intcoordinates[i][j]=Integer.parseInt(rect[j]);
    }
 }
 System.out.println(Arrays.deepToString(intcoordinates));

Here is maximum row count method

static int countMaxRow(String[] arr){
    int max=0;
    for (String str : arr) {
        String a[]=str.split(",");
        if(max< a.length){
            max= a.length;
        }
    }
    return max;
}

Upvotes: 0

Related Questions