Filling an array with element from another array in Java [duplicate] - java

This question already has answers here:
Split string with dot as delimiter
(13 answers)
Closed 5 years ago.
I'm trying to fill an array of 2 elements with a element from another array, split by "." but it doesn't fill (for instance, the input is "83.105" and i need the first element to be "83" and the second "105"). When i try to get the 0 element from numberParts array it shows out of bounds exception. I'm really confused since this method is working on C# but not Java.
String[] inputNumbers = console.nextLine().split(" ");
String[] numberParts = inputNumbers[0].split(".");
System.out.println(numberParts[0]);

Dot (.) is a char that must be escaped in the split method.
why? because is a reserved character in regex
if not, splitting will return an empty array
String myString = "83.105";
String[] x = myString.split("\\.");
System.out.println(x[0]);
System.out.println(x[1]);

Related

Substring into an array in Java [duplicate]

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.

Why won't toLowerCase convert the string element as lowercase? [duplicate]

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.

changing String to array and to integers in java [duplicate]

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.

Get Length of split in java [duplicate]

This question already has answers here:
Java String array: is there a size of method?
(12 answers)
How to get first and last element in an array in java?
(7 answers)
Closed 4 years ago.
There is a string and I split it by whitespace and I want to get the last one.
String test = "Hey Hi Hello"; // Defining "test" String
test = test.split(" ")[2]; // Now "test" is "Hello"
System.out.print(test); // prints Hello
I should do this to get the last word of "test" String. But I know the length of the string. What should I do if I don't know the length of the String? What should I write between [ ] to get the last word?
For example when I get a data from an web page and I don't now the value is what.
test.split returns an array of Strings. Just save it somewhere instead of using it immediately and check its length.
String test = "Hey Hi Hello";
String[] words = test.split(" ");
test = words[words.length - 1];
System.out.print(test);
String[] temp = test.split(" "); //this will split whole string into array using white space.
test = temp[temp.length-1]; //gets the element at the last index of temp array

Splitting a word where "." occurs [duplicate]

This question already has answers here:
Split string with dot as delimiter
(13 answers)
Closed 7 years ago.
I need to be able to split a single word string like "x.y" into an array of size 2 where the "." occurs, so the array would look like ["x", "y"]. What I've tried is:
String word = "hello.world";
String[] split = word.split(".")
return split.length == 2;
but this seems to just return an empty array (false). How would I go about doing this? Thanks.
Repalce this
String[] split = word.split(".")
with
String[] split = word.split("\\.")
. (dot) has a special meaning in regex so you need to escape it if you want to use it as a literal to split.

Categories

Resources