Home:ALL Converter>Switching int and string arrays in Java

Switching int and string arrays in Java

Ask Time:2016-08-29T18:14:02         Author:Werner

Json Formatter

I've been working on this assignment for some time and I'm stuck. The purpose is to make an int array with numbers 1-5, and then make a string array with 6-10, then put the 6-10 in the int array and the 1-5 in the string array, and afterwards do some stuff to it. I've done most of the "stuff" to it ( multiply, add etc ) but I can't figure out how to switch the two arrays with each other. I've tried a few methods that I found on stackoverflow but I couldn't implement them. Currently the methods I tried are commented out

Here's the code :

import java.util.*;
import java.io.*;

public class Rebel
{
   public static void main (String[] args) 
   {  
      int[] numbers = {1,2,3,4,5};
      String[] words = {"6", "7", "8", "9", "10"};

      System.out.println(numbers.getClass().getName()); // test data type before converting
      System.out.println(words.getClass().getName()); // test data type before converting

      for(int i = 0; i < numbers.length; i++) // prints out int array
      {
         System.out.println(numbers[i]);
      }

      for(int j = 0; j < words.length; j++) // prints out string array
      {
         System.out.println(words[j]);
      }
      /* Switching the arrays 


      //java.util.Arrays.toString(numbers[]); // converts int to string
      // numbers = Arrays.asList(words).stream().mapToInt(Integer::parseInt).toArray(); // convert string to int

     // int [] tempNum = Arrays.asList(words.split(",")).stream().map(String::trim).mapToInt(Integer::parseInt).toArray();

     //int [] tempNum = Arrays.asList(words.split(",")).stream().mapToInt(Integer::parseInt).toArray();
     */ 

      System.out.println("There are " + numbers.length + " elements in numbers array");
      System.out.println("There are " + words.length + " elements in words array");
      System.out.println(java.util.Arrays.toString(numbers));
      System.out.println(java.util.Arrays.toString(words));

      for(int num: numbers)
      {
         num = num*4;
         System.out.println(num);
      }
      for (String word: words)
      {
         System.out.println(stringMultiply(word, 3)); // s = word, and n = 3;
      }
      System.out.println(numbers.getClass().getName()); // test data type after converting
      System.out.println(words.getClass().getName()); // test data type after converting


   }


   public static String stringMultiply(String s, int n) /// "multiply" string
   {
    StringBuilder sb = new StringBuilder();
    for(int i = 0; i < n; i++)
    {
        sb.append(s);
    }
    return sb.toString();
   }
}

Author:Werner,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/39203750/switching-int-and-string-arrays-in-java
Jorn Vernee :

You were almost there with:\n\nnumbers = Arrays.asList(words).stream().mapToInt(Integer::parseInt).toArray();\n\n\nBut you need to save the numbers array first, so you can use it later to create the words array.\n\nint[] temp = numbers;\nnumbers = Stream.of(words).mapToInt(Integer::parseInt).toArray();\nwords = IntStream.of(temp).boxed().map(Object::toString).toArray(String[]::new);\n",
2016-08-29T10:23:10
Sarhad Salam :

It can be done in the following way: \n\npublic static void main(String[] args)\n {\n// The data\n int[] numbers = {1, 2, 3, 4, 5};\n String[] words = {\"6\", \"7\", \"8\", \"9\", \"10\"};\n\n // Create a new tempNumber array with word length\n int[] tempNumber = new int[words.length];\n\n // Set i=0 just for a shorter for loop\n int i = 0;\n // Enter each string to the tempNumber array.\n for( String s : words )\n {\n tempNumber[i++] = Integer.valueOf(s);\n }\n\n // Set i to - so it can be reused.\n i = 0;\n\n // Create a new tempNumber array with number length\n String[] tempString = new String[numbers.length];\n // Enter each int to the tempNumber array\n for( int n : numbers )\n {\n tempString[i++] = String.valueOf(n);\n }\n\n // Set numbers array to the one we created.\n numbers = tempNumber;\n // Set string Array to the one we created;\n words = tempString;\n\n // Output the result\n System.out.println(\"The numbers array has: \"+Arrays.toString(numbers));\n System.out.println(\"The string array has: \"+Arrays.toString(words));\n\n // Output~~~~~~~~~~~~~~~~\n // The numbers array has: [6, 7, 8, 9, 10]\n // The string array has: [1, 2, 3, 4, 5]\n }\n",
2016-08-29T10:31:19
yy