0

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

3
  • 3
    Use Integer.parseInt() Commented Mar 24, 2015 at 8:06
  • 4
    so the problem is only "how to convert string to int"? which you can simply google and get a thousand of answer? Commented Mar 24, 2015 at 8:08
  • Why do you replace every period with a comma? Looks very unnecessary. Commented Mar 24, 2015 at 8:50

4 Answers 4

1

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.

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

Comments

1

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.

3 Comments

But how to get output as i am expecting int[] ={{100,122,323,344},{543,433,872,438},{218,544,678,322}}
intcoordinates should be a 2 dimensional array.
intcoordinates equals [[100, 122, 323, 344], [543, 433, 872, 438], [218, 544, 678, 322]] at the end of the execution. This is the array you want.
0

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;
}

Comments

0

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);

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.