0
package myProjects;

import java.util.*;  

public class Acm3 {     
    static int numberOfFriends;
    static int rowOfChocolate;
    static int columnOfChocolate;
    static String division;
    static Scanner myScanner = new Scanner(System.in);  

    public static void main(String[] args) {
        System.out.println("How many friends you want to distribute?");
        numberOfFriends = myScanner.nextInt();
        System.out.println("How do you want to distribute?");
        division = myScanner.next();
        String[] myArray = division.split("\\s");
        int[] myArray1 = new int[numberOfFriends];
        int i;
        for (i = 0; i <= numberOfFriends; i++); {       
             myArray1[i] = Integer.parseInt(myArray[i]);
             System.out.print(myArray1[i]);
        }       
    }
}

I am trying to transfer String array to Int Array.I have a string array and want to convert each elements into int and finally into array. Anyone who can help? Thanks in advance

Farhan Qasim
  • 990
  • 5
  • 18
  • 1
    Possible duplicate of [Converting a String array into an int Array in java](http://stackoverflow.com/questions/6881458/converting-a-string-array-into-an-int-array-in-java) – DimaSan Nov 12 '16 at 16:09
  • Please format your code next (time indent 4 spaces or use the {} button). Also note that you have a stray semicolon after your loop, so the block in curly braces runs only once, not once for each array element as you might expect. – Robert Nov 12 '16 at 16:12
  • What error do you get ? On which line ? – senerh Jan 23 '18 at 09:49

1 Answers1

0
int[] intValues = Arrays.asList(myArray)
                       .stream()
                       .mapToInt(Integer::parseInt)
                       .toArray();

System.out.println(Arrays.toString(intValues));
Ali Faris
  • 17,754
  • 10
  • 45
  • 70