This question already has answers here:
Splitting String and put it on int array
(8 answers)
Closed 4 years ago.
If
String a="1,2,3,4,5"
I want to remove the commas in the string, and
change the string to an array, then
change every character of the array to integers, finally
apply any operation to the integers.
Is that possible?
I have this so far:
String input;
input = txtField.getText();
String[] a = new String[input.length()];
a = input.split(",");
for(int i=0;i==input.length();i++){
String c = a[i];
txtField.setText(String.valueOf(a));
}
it is, you could use a method which basically removes all commas(.split)so you get a String without commas, and later take that string and use a for loop to go through each char and parse to int.
Related
This question already has answers here:
How do I declare and initialize an array in Java?
(31 answers)
Closed 1 year ago.
I'm wanting to store an a substring into a new array.
Here is my substring data = line.substring(i, i+1);
How do i do this? I have tried String data [] = line.substring(i, i+1) but not worked.
Thanks
.substring() does not return an array, it returns a String so passing it into an array won't work. You can split a String into a String[] array using the .split(String regex) method. The paramter is the delimiting regular expression, meaning if you did "Hello World".split(" "); (with space as the argument), the resulting array would be ["Hello", "World"]. If you want to split all of the characters into their own index of the array, just pass in an empty String "" as the argument of the split method (like line.split("")).
If your goal is to store the String in an array you can pass it in with a regular array declaration and assignment: String[] data = { line.substring(i, i + 1) }; or just make a new array and pass it into the desired index.
This question already has answers here:
Why does the foreach statement not change the element value?
(6 answers)
Java Modifying Elements in a foreach
(4 answers)
Java: Foreach loop not working as expected on int array? [duplicate]
(3 answers)
Closed 2 years ago.
Why won't this convert the string element as lowercase?
Given string input, converted into a string array. I want each word to be lowercase. However, when I debug, I notice the elements in my array are still varying in cases.
String[] words = paragraph.split(" "); //[how, to, do, in, java]
for(String word : words){
word = word.toLowerCase();
}
Because by doing toLowerCase(), you are instantiating a new String. So your word variable is no more a reference to an item of your array.
Either you add the result of the toLowerCase() to a new array or you loop with index :
for(i = 0;i < words.size();i++) {
words[i] = words[i].toLowerCase();
}
toLowerCase will return a new object, you have to store it.
You can use Java 8 to achieve it in one line:
Arrays.setAll(words, i -> words[i].toLowerCase());
You are not modifying your array instead you are assigning the lowercase string to the word instance
You can use the classic for loop and assign the lowercase value to the current index of the array
for en example, your for loop should look like
for(int i=0;i< words.length;i++){
words[i] = words[i].toLowerCase();
System.out.println(words[i]);
}
String objects are immutable by default. When you reassign something to string it creates a new object, hence the original String object in words array remain the same.
This question already has answers here:
string to string array conversion in java
(15 answers)
Closed 7 years ago.
I was wondering how exactly one would convert a string of non-alphanumeric symbols into a string array. For example, what would be the code that takes the string
"#%$!#& (&$#^"
as an input, and converts it into a string array
{"#", "%", etc}
Create a String array of the length of string..
int size = theString.length();
String[] asStringArray = new String[size];
then step through that array/String adding chars from the String with charAt()
for (int i=0; i<size; i++){
asStringArray[i] = ""+theString.charAt(i);
}
Other option is substring()
asStringArray[i] = theString.substring(i,i+1);
This question already has answers here:
How to convert a String into an array of Strings containing one character each
(6 answers)
Closed 8 years ago.
I'm new to java.How to convert char array into string array.My char array holds the values from Resultset rs.The code for char array is
char[] time= rs.getString(logtime).toCharArray();
How to convert this time into String array.
The question is a bit unclear. Assuming that you want a string array with the same contents as the character array (one character per string), here's a very quick and dirty solution:
char[] time = rs.getString(logtime).toCharArray();
String[] s = (new String(time)).split("(?<=.)");
Or, if you don't really need to convert to a char array first, you could just simply do:
String[] s = rs.getString(logtime).split("(?<=.)");
The regular expression used for the split method is a zero-width positive lookbehind assertion to match any character.
A cleaner (and most likely more efficient) solution would look as follows:
char[] time = rs.getString(logtime).toCharArray();
String[] s = new String[time.length];
for (int i = 0; i < time.length; i++) {
s[i] = String.valueOf(time[i]);
}
Try this
char[] time= rs.getString(logtime).toCharArray();
String[] timeStr = new String[time.length];
for (int i=0,len=time.length; i<len; i++){
timeStr[i] = String.valueOf(time[i]);
}
This question already has an answer here:
Split string on spaces in Java, except if between quotes (i.e. treat \"hello world\" as one token) [duplicate]
(1 answer)
Closed 8 years ago.
I have a String(freeText) "Manas \"Jaikant IBM\"". I want to split into two strings:
String normalMatch="Manas";
String exactMatch="Jaikant IBM";
It means that String normalMatch contains Manas and String exactMatch contains Jaikant IBM.
i am using the split() method of String class in Java
String[] splittedText= freeText.split("\\s");
I am getting 3 string elements but i need 2 string elements only.
Use substring instead of split:
int index = freeText.indexOf(" ");
String normalMatch = freeText.substring(0,index);
String exactMatch = freeText.substring(index); // endIndex == freeText.length())
Split on quotes(") then, you will get Manas and Jaikant IBM and can ignore the 3rd value.