Converting an Object [] array to an int [] array in java?

There seems to be no easy way to do this, but this is what I have done so far, and if someone can fix it to make it work, it would be great. In "newarray [e] = array [i] .intValue ();" I get the error message "No method named" intValue "was found in the method" java.lang.Object ". Help!

/* Description: A game that displays digits 0-9 and asks the user for a number N. It then reverses the first N numbers of the sequence. It continues this until all of the numbers are in order. numbers */ import hsa.Console; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Arrays; public class ReversalGame3test { static Console c; public static void main (String[] args) { c = new Console (); c.println ("3. REVERSAL GAME"); c.println (""); c.println ("Displayed below are the digits 0-9 in random order. You must then enter a"); c.println ("number N after which the computer will reverse the first N numbers in the"); c.println ("sequence. The goal of this game is to sort all of the numbers in the fewest"); c.println ("number of reversals."); c.println (""); //introduction List numbers = new ArrayList (); numbers.add ("0"); numbers.add ("1"); numbers.add ("2"); numbers.add ("3"); numbers.add ("4"); numbers.add ("5"); numbers.add ("6"); numbers.add ("7"); numbers.add ("8"); numbers.add ("9"); Collections.shuffle (numbers); Object[] array = numbers.toArray (new String [10]); // declares + shuffles numbers and converts them to array c.print ("Random Order: "); for (int i = 0 ; i < 10 ; i++) { c.print ((array [i]) + " "); } c.println (""); boolean check = false; boolean check2 = false; String NS; int N = 0; int count = 0; int e = -1; int[] newarray = new int [10]; //INPUT do { c.print ("Enter a number: "); NS = c.readString (); count += 1; check = isInteger (NS); if (check == true) { N = Integer.parseInt (NS); if (N < 1 || N > 10) { check = false; c.println ("ERROR - INPUT NOT VALID"); c.println (""); } else { c.print ("Next Order: "); for (int i = N - 1 ; i > -1 ; i--) { e += 1; newarray [e] = array [i].intValue (); c.print ((newarray [e]) + " "); } for (int i = N ; i < 10 ; i++) { e += 1; newarray [e] = array [i].intValue (); c.print ((newarray [e]) + " "); } check2 = sorted (newarray); } // rearranges numbers if valid } // checks if N is valid number } while (check == false); } // main method public static boolean isInteger (String input) { try { Integer.parseInt (input); return true; } catch (NumberFormatException nfe) { return false; } } //isInteger method public static boolean sorted (int array[]) { boolean isSorted = false; for (int i = 0 ; i < 10 ; i++) { if (array [i] < array [i + 1]) { isSorted = true; } else if (array [i] > array [i + 1]) { isSorted = true; } else isSorted = false; if (isSorted != true) return isSorted; } return isSorted; } // sorted method 

}

+6
source share
5 answers

You can use Integer.valueOf .

 Integer.valueOf((String) array [i]) 

The Integer class has a valueOf method that takes a string as a value and returns an int value, you can use this. It will NumberFormatException if the string passed to it is not a valid integer value.

Also, if you use java5 or higher, you can try using generics to make the code more readable.

+8
source

You can implement the same using Generics , which would be easier.

 List<Integer> numbers = new ArrayList<Integer> (); Integer[] array = numbers.toArray (new Integer [10]); 
+4
source

You cannot call .intValue() on an Object because the intValue() method is missing from the Object class.

Instead, you need to first shift the Object to an Integer class, for example:

 newarray[e] = ((Integer)array[i]).intValue(); 

Edit: just a good tip on StackOverflow - please limit your code to what is appropriate! Although large blocks of code are sometimes needed, this is not the case. This makes the question more enjoyable, and it should get better answers.

Also, do not use . Currently, it is outdated and is in the process of being burned.

+2
source

try Common Languages

 org.apache.commons.lang.ArrayUtils.toPrimitive(Integer[]) 
+1
source

I made this method ... I think it's better!

 public int[] ToMixArray(Object[] Array, int StratIndex, int Valuedefault, int NewLength){ int[] res=new int[NewLength]; for (int i = 0; i < NewLength; i++) { try { res[i]=Integer.parseInt(String.valueOf(Array[StratIndex+i]));} catch(Exception e){res[i]=Valuedefault;} }return res; } 
0
source

All Articles