Separating numbers of a string array to a integer array [duplicate] - java

This question already has answers here:
split int value into separate digits
(8 answers)
Closed 6 years ago.
So I have a String array that has a number like "1234567898765" and I want to convert it to an int and assign each number to each index of an array of integers.
For example, array[i]= "1234567898765" and I want to do arrayOfIntegers[0] = 1 , arrayOfIntegers[1] = 2, arrayOfIntegers[2] = 3, ect... How can I do that?

public class StringTest {
public static void main(String args[])
{
String input = "123456789765";
int [] arrayOfInts = new int [input.length() ];
for (int i = 0; i < input.length(); i++ ) {
arrayOfInts[i] = Character.getNumericValue(input.charAt(i));
}
}
}

Related

Is this a optimal way of initializing an array with 1 through 5? [duplicate]

This question already has answers here:
Fill arrays with ranges of numbers
(7 answers)
Closed 2 years ago.
The usage of i - 1 concerns me the most. Should i use another variable for the indexing or is this fine?
public class MainClass {
public static void main(String[] args) {
int[] array = new int[5];
for(int i = 1; i<=array.length; i++) {
array[i-1] = i;
}
for(int x: array)
System.out.println(x);
}
}
Technically, it doesn't matter. However, you may do
for (int i = 0; i < array.length;)
array[i] = ++i;
to generate the same output:
1
2
3
4
5

How to extract integers from a separated string in java [duplicate]

This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 4 years ago.
Examples of the String
131-5923-213
1421-41-4-12-4
1-1
How would I extract the integers into an Array or find the sum of these integers? My code so far goes
int hyphenCount = socialNum.length()-socialNum.replaceAll("-", "").length();
ArrayList<Integer> sum = new ArrayList<Integer>();
for(int i = 0; i < hyphenCount; i++)
{
//my brain is too small
}
What I want to do is make a function like the following
public void extractSum(String s)
{
int outputSum;
//stuff
return outputSum;
}
Using Streams you can do:
int sum = Arrays.stream(str.replace("-", "").split("")).mapToInt(Integer::valueOf).sum();
Which will replace the hyphen and then split each character, parse the integer, and return the sum
Output: (For the String "131-5923-213")
30
If you want the sum of 131 + 5923 + 213 you could do:
int sum = Arrays.stream(str.split("-")).mapToInt(Integer::valueOf).sum();
Which will split on a hyphen, parse the integers, and return the sum
Apart from #GBlodgett's answer using stream, you could simply run a for loop to calculate the sum.
String string = "131-5923-213";
String[] num = string.split("-"); //<----num[0] = "131" ,num[1]="5923",num[2] = "213"
int hyphenCount = num.length; //<----it would give you 3 as length
int mySum = 0; //initialize the sum as 0
for(int i = 0; i < hyphenCount; i++)
{
mySum+= Integer.parseInt(num[i]); //<---convert the string to an int
}
Output: 6267

How to random java string array , that each array will be true random? [duplicate]

This question already has answers here:
Random shuffling of an array
(31 answers)
Closed 7 years ago.
i have java string array which looks like this :
String [] cards = {"c1","c2","c3", , , , , , ,, , "c45"};
so i have there 45 elements , now i like to rendom them each time
so it will be :
int[] cards2 = Arrays.copyOf(cards , cards .length);
random(cards2);
how should be the random function look like ?
You can use
Collections.shuffle(Arrays.asList(cards));
You can use this if you want to copy to a new array.
public String[] randome(String[] arr) {
Random rgen = new Random();
String[] randArray = new String[arr.length];
System.arraycopy(arr, 0, randArray, 0, arr.length);
for (int i = 0; i < randArray.length; i++) {
int randIn = rgen.nextInt(randArray.length);
String temp = randArray[i];
randArray[i] = randArray[randIn];
randArray[randIn] = temp;
}
return randArray;
}

How to convert integer in to base -2 using JAVA [duplicate]

This question already has answers here:
Integer to binary array
(11 answers)
Closed 7 years ago.
If given int array A is [1,0,0,1,1] =9 A[i]*(-2)^i (^ power).
How to write a java program get the int array sequence for give integer.
public class A {
public static void main(String[] args) {
int [] arr = {1,0,0,1,1};
for (int i = 0; i < arr.length; i++) {
arr[i]=9*arr[i]*(int)Math.pow(-2,i);
}
} System.out.println(Arrays.toString(arr));
}

How to create an array with both int and char [duplicate]

This question already has answers here:
How do I declare and initialize an array in Java?
(31 answers)
Closed 8 years ago.
I'm trying to create a single array that contains the numbers 1-9 and the characters A-F. The array should look like: 1 2 3 4 5 6 7 8 9 A B C D E F. I don't know how to set the array up and would appreciate any advice.
Try this following piece of code,This will work for you,However you must keep in mind that An array can contain only a single type of value as said by #MadProgrammer in comments
import java.util.*;
public class MyClass
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
char[] arr = new char[16];
/*taking input from the keyboard*/
for(int i = 0; i < 16; i++)
{
arr[i] = sc.next().charAt(0);
}
/*displaying the contents of the array*/
for(int i = 0; i < 16; i++)
{
System.out.println(arr[i] + ",");
}
}
}
In this code the numbers 0-9,which you are taking as input are still inserted as characters.

Categories

Resources