This is the content of my Input file:
123
I want to take this input content to a int[] Array
My code:
BufferedReader br = new BufferedReader(new FileReader("yes.txt"));
int[] Array = br.readline().toCharArray(); // Error as it has to be int array
How to solve this:
output:
Array[0]=1
Array[0]=2
Array[0]=3
Simple convert of your string to int array:
private int[] convertStringToIntArray(String str) {
int[] resultArray = new int[str.length()];
for (int i = 0; i < str.length(); i++)
resultArray[i] = Character.getNumericValue(str.charAt(i));
return resultArray;
}
Once you have your string in, it's very simple.
I would recommend using an arraylist for this, as it is easier to add new elements on the fly.
String inputNumbers = "12345";
ArrayList<Integer> numberList = new ArrayList<Integer>();
for(int i = 0; i < inputNumbers.length(); i++) {
numberList.add(Integer.valueOf(numberList.substring(i,i+1)));
}
And there you go! You now have an arraylist called numberList where you can access all of the numbers with numberList.get(0), numberList.get(1), etc.
First read the input as string then convert to int array as below
String inputString = br.readline()
int[] num = new int[inputString.length()];
for (int i = 0; i < inputString.length(); i++){
num[i] = inputString.charAt(i) - '0';
}
Another way is to convert the char array to int array
char[] list = inputString.toCharArray();
int[] num = new int[inputString.length()];
for (int i = 0; i > inputString.length(); i++){
num[i] = list[i];
}
System.out.println(Arrays.toString(num));
Related
I'm trying to reverse each number in an Integer array using do-while, but I get NullPointerException error.I'm trying to reverse each element in this array: for instance if this is my array:{12,34,56} then the result must be:{21,43,65}.Can someone please help me with this?
public class Reverse {
public int[] revCalculator(int[] number) {
int[] reverse = null;
for (int j = 0; j < number.length; j++) {
do {
reverse[j] = reverse[j] * 10 + number[j] % 10;
number[j] /= 10;
} while (number[j] > 0);
}
return reverse;
}
}
public class ShowReverse {
public static void main(String[] args) {
// instantiation
// -----------------------------------------
Reverse rev = new Reverse();
#SuppressWarnings("resource")
Scanner in = new Scanner(System.in);
System.out.println("Enter The Number Of Elements: ");
int len = in.nextInt();
int[] numbers = new int[len];
for (int i = 0; i < len; i++) {
System.out.println("Enter Number: ");
numbers[i] = in.nextInt();
}
int[] result = rev.revCalculator(numbers);
// shows the result
// -----------------------------------------
System.out.println("THE REVERSE WOULD BE: ");
System.out.print(result);
}
}
pic
You didn't initialize your variable reverseArray, initialize it like so:
UPDATE, I see what you are trying to do, you want to reverse the values of your array, not the array itself
public int[] revCalculator(int[] number) {
int[] reverse = new int[number.length]; //Initialize it to the same size as the passed in arary
//int[] reverse = null; //Problem
StringBuilder sb = new StringBuilder();
for (int j = 0; j < number.length; j++)
{
//You could convert the number to string then reverse it and use parseInt() int
reverse[j] = parseInt(sb.append(number[j].toString()).reverse().toString());
//Clear the StringBuilder object
sb.setLength(0);
}
return reverse;
}
I would recommend initializing the reverse array to an array of the same length as the number array.
int[] reverse = new int[number.length];
There are a couple of changes that you have to make:
initialize reverse array int[] reverse = new int[number.length];
System.out.print(result); would give you the address of result array. You have to show the elements of the array.
int[] result = new int[numbers.length];
result = rev.revCalculator(numbers);
// shows the result
// -----------------------------------------
System.out.println("THE REVERSE WOULD BE: ");
//System.out.print(result);
for(int i = 0 ; i<result.length; i++)
System.out.println(result[i]);
Hope this helps.
Input is in this format...
String input = "3 12#45#33 94#54#23 98#59#27";
To be extracted in this array...
int[][] array = new int[3][3];
You can split the string and then iterate over the array. Which results in something like this:
String input = "3 12#45#33 94#54#23 98#59#27";
String[] strings = input.split(" ");
int size = Integer.parseInt(strings[0]);
int[][] result = new int[size][size];
for( int i = 0; i < strings.length - 1; i++ ){
String[] strings2 = strings[i + 1].split("#");
for( int j = 0; j < strings2.length; j++ ){
result[i][j] = Integer.parseInt(strings2[j]); // add the parsed int to result
}
}
How can I convert array of string containing decimal numbers to big integer?
eg:
String s={"1","2","30","1234567846678943"};
My current code:
Scanner in = new Scanner(System.in);
int n = in.nextInt();
String s[]= new String[n];
for(int i=0; i < n; i++){
s[i] = in.next();
}
BigInteger[] b = new BigInteger[n];
for (int i = 0; i < n; i++) {
b[i] = new BigInteger(String s(i));
}
Here:
b[i] = new BigInteger(String s(i));
should be:
b[i] = new BigInteger(s[i]);
In other words: you got half of your syntax correct; but then seem to forget how to read an already defined array slot:
you use [index] square brackets ( "( )" are only used for method invocations)
No need to specify that "String" type within that expression
Just use new BigInteger(s[i]); instead of new BigInteger(String s(i));
FYI, you don't really have to use a separate String array to store initial values. You can directly store them in BigInteger array. Somewhat like this:
Scanner in = new Scanner(System.in);
int n = in.nextInt();
BigInteger[] b = new BigInteger[n];
for(int i=0; i < n; i++){
b[i] = new BigInteger(in.next());
}
I have a string which I want to split by the commas (easy process):
String numbers = "1,2,3";
But, I then want to convert each element of the split string into an int that can be stored in an int array separated by commas as:
int[][] array = new int [0][];
array[0] = new int[] {1,2,3};
How can I achieve this?
The 2D array is unnecessary. All you need to do is splice the commas out of the string using the split method. Then convert each element in the resulting array into an int. Here's what it should look like,
String numbers = "1,2,3";
String[] arr = numbers.split(",");
int[] numArr = new int[arr.length];
for(int i = 0; i < numArr.length; i++){
numArr[i] = Integer.parseInt(arr[i]);
}
String[] strArr = numbers.split(",");
int[][] array = new int[1][];
for(int i = 0; i < strArr.length; i++) {
array[0][i] = Integer.parseInt(strArr[i]);
}
You dont need 2D array, you can use simple array. It will be enough to store this data. Just split it by ',' and then iterate through that array,parse everything int and add that parsed values to new array.
String numbers = "1,2,3,4,5";
String[] splitedArr= numbers.split(",");
int[] arr = new int[splitedArr.length];
for(int i = 0; i < splitedArr.length; i++) {
arr[i] = Integer.parseInt(splitedArr[i]);
}
System.out.println(Arrays.toString(arr));
String s = "1,2,3";
StringTokenizer st = new StringTokenizer(s,",");
int[][] array = new int [0][];
int i = 0;
while(st.hasMoreElements()) {
int n = Integer.parseInt((String) st.nextElement());
array[0][i] = n;
i++;
}
This is twice as fast as String.split()
array[0] = Arrays.stream(numbers.split(","))
.mapToInt(Integer::parseInt)
.toArray();
Ok... So I am in the middle of a project, and I've hit a bit of a wall. If anyone could please explain how to add an integer array to an ArrayList of integer Arrays, I would greatly appreciate it. This is in processing, the javascript version more specifically. I have tested and everything works right up to 'symbols.get(i).add(tempArray). If I print tempArray right before that line it gives me '6 10 16 10 16 20', as it should. And no, it's not just the println(symbols) statement, I also tried just 'println("blah");'and that did not show up in the output, so something is going awry at the .get line.
size(850,250);
String[] myList = new String[100];
ArrayList<Integer[]> symbols = new ArrayList<Integer[]>();
int[] tempArray = new int[];
String numbers = ("6,10,16,10,16,20\n1,25,21,13,3,15\n6,5,20,6,21,20");
myList = (split(numbers, "\n"));
int j = myList.length();
for(int i = 0; i<j; i++)
{
tempArray = int(split(myList[i], ','));
symbols.get(i).add(tempArray);
}
println(symbols);
... I have also tried the following in place of 'symbols.get(i).add(tempArray);'
for(int a = 0; a < tempArray.length(); a++)
{
symbols.get(i[a]) = tempArray[a];
}
println(symbols);
... I have also tried
for(int a = 0; a < tempArray.length(); a++)
{
symbols.get(i) = tempArray[a];
}
println(symbols);
... and
for(int a = 0; a < tempArray.length(); a++)
{
symbols[i][a] = tempArray[a];
}
println(symbols);
I'm out of guesses and tries, any help would be appreciated.
First get the Array of integer by using the get method on the ArrayList, then add [index] to specify what to add at which index of the returned Array.
symbols.get(i)[a] = tempArray[a];
There are some mistakes. The key one is that you can't add int[] to the list expecting Integer[]. Here, see the comments on the code:
void setup()
{
size(850, 250);
String[] myList = new String[100];
ArrayList<Integer[]> symbols = new ArrayList<Integer[]>();
// you can't init an array without a inintial dimension
//int[] tempArray = new int[];
int[] tempArray;
String numbers = ("6,10,16,10,16,20\n1,25,21,13,3,15\n6,5,20,6,21,20");
myList = (split(numbers, "\n"));
//length is not a method... no parenthesis here
//int j = myList.length();
int j = myList.length;
for (int i = 0; i<j; i++)
{
tempArray = int(split(myList[i], ','));
// you cant add an int[] to an Integer[] arrayList
// you gotta either change the arraylist type or convert the ints to Integers
// also just use .add(). not get().add() it's a one dimension list
symbols.add(parseArray(tempArray));
println(symbols.get(i) );
println("--");
}
}
//convenience function..
Integer[] parseArray(int[] a) {
Integer[] b = new Integer[a.length];
for (int i = 0; i<a.length; i++) {
b[i] = Integer.valueOf(a[i]);
}
return b;
}
and the other way...
void setup()
{
size(850, 250);
String[] myList = new String[100];
ArrayList<int[]> symbols = new ArrayList<int[]>();
// you can't init an array without a inintial dimension
//int[] tempArray = new int[];
int[] tempArray;
String numbers = ("6,10,16,10,16,20\n1,25,21,13,3,15\n6,5,20,6,21,20");
myList = (split(numbers, "\n"));
//length is not a method... no parenthesis here
//int j = myList.length();
int j = myList.length;
for (int i = 0; i<j; i++)
{
tempArray = int(split(myList[i], ','));
symbols.add(tempArray);
println(symbols.get(i) );
println("--");
}
}