0

I was wondering how would I convert a string to an int array.

I created a reader to read from a text file, which consists of two lines.

  1. The first line is a random quote like "dogs are red"
  2. The second line are numbers.

I am supposed to save each character from the quote into a char array, and then the second line, consisting of the numbers, is the order number of each char, and when printed out in that order, it will spell out a phrase.

I used the .hasNextLine method to first save the two lines into two string variables. Then, I converted the first line into the char array, and that part is ok but I do not know how to convert the second part into an int array.

I called the variable holding the second line "numbers" and it contains the string "6 25 11 32 6 11 44......"

How would I convert this into an int array?

Thanks so much

5
  • Wow, you described your intent in plain English :-). It helps when you paste the source code itself. Commented Mar 31, 2016 at 23:19
  • 1
    It'd be helpful if you provided code. That way we'd know where you're stuck. Commented Mar 31, 2016 at 23:19
  • Taking a leap of faith -- you have a string made of blank separated integers. Each integer is to be read as a "string". Try to separate these number substrings and send them to Integer.valueOf. Commented Mar 31, 2016 at 23:20
  • Your String of ints is delimited by spaces? Commented Mar 31, 2016 at 23:20
  • Hi, Sorry about that, let me post the code, and yes the numbers are seperated by spaces :). I have to run different files into my program, so everything varies a little bit. Commented Mar 31, 2016 at 23:35

2 Answers 2

5

You could do something real simple like this:

String[] strArray = input.split(" ");
int[] intArray = new int[strArray.length]; 

for(int i = 0; i < strArray.length; i++) {
    intArray[i] = Integer.parseInt(strArray[i]);
}

Also, you'd want to check for NumberFormatException using a try..catch block.

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

Comments

3

If your numbers are delimited by spaces like it is described in your post, you can use String.split() to your advantage.

You can create a method like the following which will convert a String of integers to an int[]:

public static int[] stringArrayToIntArray(String intString) {
    String[] intStringSplit = intString.split(" "); //Split by spaces
    int[] result = new int[intStringSplit.length]; //Used to store our ints

    for (int i = 0; i < intStringSplit.length; i++) {
        //parse and store each value into int[] to be returned
        result[i] = Integer.parseInt(intStringSplit[i]); 
    }
    return result;
}

Which can be called like so:

public static void main(String[] args) {

    String intString = "6 25 11 32 6 11 44"; //Original String

    int[] intArray = stringArrayToIntArray(intString); //Call our method

}

If you were to iterate through intArray and print each value with the following:

    for (int i : intArray) {
        System.out.println(i);
    }

You'd get a result of:

run:
6
25
11
32
6
11
44
BUILD SUCCESSFUL (total time: 0 seconds)

Cheers!

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.