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

3
  • 1
    Possible duplicate of Converting a String array into an int Array in java Commented Nov 12, 2016 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. Commented Nov 12, 2016 at 16:12
  • What error do you get ? On which line ? Commented Jan 23, 2018 at 9:49

1 Answer 1

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

System.out.println(Arrays.toString(intValues));
Sign up to request clarification or add additional context in comments.

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.