I made a function that requires an int[], Conflict(int[] tab), and as you all know we extract information from a JTextField in the form of an String (s) ,so I then convert in into an integer with:
int input=Integer.parseInt(s);
Now, I found this way to do it :
int input= 1234;
String input= String.valueOf(input);
Vector<int> tab = new Vector<int>();
for (int cpt = 0; cpt <input.length(); cpt++)
{
tab.add(Integer.valueOf(input.substring(cpt, cpt+1)).intValue());
}
but I'm looking for a simple way without the vector ,so again the Question is How can I convert the input (int) into a table of int[] so that I can use it in the Conflict function?
Here is an answer to my question for all of you who's looking to turn an int into an int[] using a string with a very smart simple trick
tcst = ""+inputc;
int tcn = tcst.length();
int[] tctab = new int[tcn];
for(int h=0; h<tcn; h++){
tctab[h] = (int) Integer.parseInt("" + tcst.charAt(h));
}
If you have a String of number like "12345" best way is to convert it into char array using toCharArray() method of string. Once you get a char array of numbers these char can be used as integer in you program. just when you get an element from the char array and convert it into int by using getNumericValue() method of character
If your input is a String (from a JTextField), then I don't see why you're converting it to an integer, just to convert it to a String again.
To go from String to int[], here's a way to do it.
char[] chars = inputString.toCharArray();
int[] result = new int[chars.length];
for(int i=0; i<chars.length; i++){
result[i] = Character.getNumericValue(chars[i]);
}
Related
I'm using Eclipse and I'm trying to take a string:
1 2 3 4
out of an ArrayList:
ArrayList strings = new ArrayList<>();
And store each number into a two dimensional array:
int size = (int) strings.get(0); // Represents the number of rows
int stringList[][] = new int[size][];
// Store results in a two dimensional array
for(int i = 0; i < size; i++){
int index = i + 1;
String fish = (String) strings.get(index);
Scanner npt = new Scanner(fish);
for(int j = 0; npt.hasNext(); j++) {
size[i][j] = Integer.parseInt(npt.next());
}
}
This is the section that is causing the error:
// ERROR: The type of the expression must be an array type but it resolved to int
size[i][j] = Integer.parseInt(npt.next());
strings is a raw type. Let's start by fixing that1,
List<String> strings = new ArrayList<>();
Then you can use Integer.parseInt(String) parse2 the String to an int like
int size = Intger.parseInt(strings.get(0));
Then there's no need for a cast in
String fish = (String) strings.get(index);
You can use
String fish = strings.get(index);
etc.
1 And program to the List interface.
2 Not cast.
You probably meant
stringList[i][j] = Integer.parseInt(npt.next());
though that wouldn't work either, unless you properly initialize the stringList array.
You would have to initialize stringList[i] (for each i) with something like stringList[i] = new String[someLength]; but I'm not sure which length you should use.
I was curious how to muliply an array by a factor? Not each cell (t[0], t[1], etc) individually, but as a whole number. Ex: t[0] = 9 t[1] =2 t[2] = 5, t[] = 925. 925 times 3 =2775
Basically, I am receiving a value and converting from ASCII to Decimal(I have already done this). However, I want to multiply by it a factor of 3. Do I need to store the entire array as a string, and then use the multiply function?
The relevant code for this section
byte[] readBuf =(byte[]) msg.obj);
char x;
String readMessage = newString(readBuf,0,msg.arg1);
int[] t = new int[readMessage.length()];
for(int i = 0; i<readMessage.length(); i++)
{
x = readMessage.charAt(i);
int z = (int) x;//Array has been converted from ASCII into decimal values
t[i] = z;//Array has been populated with decimal values
//Confused about the next part, Convert back into string and then multiply string?
}
Why make extra variables for char and int, use space for tables and pass things from one variable to another when you can do it all in one line? From what i've understood this is all you need.
byte[] readBuf =(byte[]) msg.obj);
String readMessage = newString(readBuf,0,msg.arg1); //You create the string here
String final=""; //new string to be parsed
for(int i = 0; i<readMessage.length(); i++){
final+=""+(int)readMessage.charAt(i); // get the charAt(i) cast it to int and give it to the string
}
return Integer.parseInt(final)*factor; //return the int multiplied by 3
Edit: Use Double.parseDouble(readMessage) to convert it to decimal
ASCII characters are only Integer numbers
I'm not sure if I understand the format of your input string. Does this example solve your problems?
public static void main(String args[]) throws Exception {
String input = "925";
int parsed = Integer.parseInt(input);
parsed *= 3;
System.out.println(parsed); // prints 2775
}
Your ask is not clear, I try to understand: you have a String in an array where the zero index is the most significative position and the last is the less significative position. So, if you have 123, the situation is t[0]=1, t[1]=2, t[2]=3.
If it's correct, then you say you want to rebuild the number (in my example 123 and return that multiplyed by a factor, i.e. 3). So return 369.
Here's my solution assuming that the resulting number will be lower than MAXINT.
I also mantain your code as is, and return the result multiplyed by a factor in value "factor" (int).
byte[] readBuf =(byte[]) msg.obj);
char x;
String readMessage = newString(readBuf,0,msg.arg1);
int[] t = new int[readMessage.length()];
String final="";
for(int i = 0; i<readMessage.length(); i++)
{
x = readMessage.charAt(i);
int z = (int) x;//Array has been converted from ASCII into decimal values
t[i] = z;//Array has been populated with decimal values
final+=""+t[i]; // add to String the char in position i.
//Confused about the next part, Convert back into string and then multiply string?
}
return Integer.parseInt(final)*factor;
i need to convert char array into string , the problem in doing this is......i need to convert the character in char array of particular length say k to string. ie, char array is "b" .b takes value dynamically.....for instance take as "p,a,p,e,r,s" now k value also dynamic ,for this word "k=5" ,and then only 4 characters in char array "b" should be converted into string...ie the string should print as "paper"........
the code what i have now is
for(int c=0;c<=k;c++)
{
System.out.print(b[c]);
}
str=new String(b);
System.out.println(str);
where b[c] prints correct value(in char array) as "paper". While converting to string str (in program) it prints as "papers" itself....can anyone give me solution for this?
You can use a different constructor of String that lets you specify the array along with the start point and number of characters to use.
In your case, you would try:
str = new String( b, 0, k );
char newArr[] = new char[k];
for (int i = 0; i < k; i++) {
newArr[i] = b[i];
System.out.print(b[i]); // print until the kth index
}
return new String(newArr);
I need to convert a string to a corresponding int array in java. i wrote the following code but its not working as i expected .
String temp= "abc1";
int[] intArray = new int[temp.length()];
for (int i = 0; i < temp.length(); i++) {
intArray[i] = Integer.parseInt(temp[i]);
}
I have written an rc4 encryption program which take the key and plain text as int arrays. So I need to convert the user specified key to int array before passing it to the encryption function. Is this the correct way of using key in encryption programs?
Use this to get the ASCII code
intArray[i] = (int)temp.charAt(i);
You can convert string to charArray. Travesing charArray you can convert as:
char[] c = inputString.toCharArray()
for(int i=0;i<c.length;i++)
int n = Integer.parseInt(c[i]);
if you are Using Java8 or higher then it might be helpful for you
String temp="abc1";
int[] intArray =temp.chars().map(x->x-'0').toArray();
System.out.println(Arrays.toString(intArray ));
int array would be [49, 50, 51, 1]
I solved this by using byte instead of int. Modified the rc4 to take byte array.
converted the string to byte using
String Nkey = jTextField2.getText();
jTextField3.setText(Nkey);
int i;
byte[] key = Nkey.getBytes();
I got a little problem here with Java and I am fairly new to it.
My program reads a String via InputStreamReader and saves it in the String input.
How do I save the elements of the String in a 2d char array with n x m elements?
Edit:
I think I´ve got a solution:
I used 2 for-loops (is that the right english translation for it? ) and .toCharArray to convert the String.
public static char[][] transform (String text, int arrBreite, int arrLaenge) {
char[][] returnArray = new char[arrBreite][arrLaenge];
char[] buffer = text.toCharArray();
for (int i = 0; i < arrBreite; i++) {
for (int j = 0; j <arrLaenge; j++) {
if (((i * arrBreite) + j) > buffer.length - 1) returnArray[i][j] = " ".charAt(0);
else returnArray[i][j] = buffer[(i*arrBreite)+j];
}
}
return returnArray;
}
Thanks for your help guys.
You can use the toCharArray() method to get a char array from your String.
If you need to split with a given delimiter to determine the array lines, you use first the Split method on the String, then use toCharArray to create your 2 dimensional array.
You should use String.toCharArray().