This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 3 years ago.
I have a string of "A B C" and I have this line of code to split it:
String[] reFormatted = TestString.split(" ");
System.out.println(reFormatted.toString());
and the output I get is this:
[Ljava.lang.String;#30f1c0
if I put this string array in a foreach loop like this:
for(String s : reFormatted){
System.out.println(reFormatted);
}
I get the output of:
[Ljava.lang.String;#30f1c0
[Ljava.lang.String;#30f1c0
[Ljava.lang.String;#30f1c0
what is the problem here? what am I doing wrong?
I think you are printing the address of the variable reFormatted and for the for loop, you should print s not reFormatted
for(String s : reFormatted){
// System.out.println(reFormatted);
System.out.println(s);
Related
This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 4 years ago.
Say I have an array, and I need to print out the info in that array with a certain character in between each string in the array. How would I go about this? I'll provide an example.
Print out array with info seperated by a " | " without hardcoding it
ex: yes|no|maybe etc...
public class Responses {
public static void main(String[] args)
String[] response = {"yes", "no", "maybe", "perhaps"};
The only way I could think of was hard coding it, but I am not allowed to hard code it so that in the event you take out or add something to the array, it will automatically print out that info as well
EDIT: This is not the same as simply printing an array. It is printing the array with the addition of a character between each string
Use the build-in function join:
String[] response = {"yes", "no", "maybe", "perhaps"};
System.out.println(String.join("|", response)); // yes|no|maybe|perhaps
This question already has answers here:
How to split a String by space
(17 answers)
Closed 5 years ago.
I want to take in a String and split it every time I find a space. And then store each of these pieces into an array.
Let's say I have this string:
String names = "amy bob lily harry luna james";
I also have this method declaration:
public static String[] seperateNames(String names) {
String[] newNames;
// Some code here
return newNames[];
}
What would I fill this method with so I can get something like this:
newNames = {"amy", "bob", "lily", "harry", "luna", "james"};
What I think I should do is create a for-loop and inside it have an if-statement that can check if there's space.
But I really don't know how to go about doing this.
I also think I will need to use trim() after everything is stored in an array to remove spaces before and after each name stored in the array.
Any help or advice appreciated. Thanks!
public static String[] seperateNames(String names) {
return names.split(" ");
}
To start you off on your assignment, String.split splits strings on a regular expression, this expression may be an empty string:
String[] ary = "abc".split("");
Yields the array:
(java.lang.String[]) [, a, b, c]
Getting rid of the empty 1st entry is left as an exercise for the reader :-)
Note: In Java 8, the empty first element is no longer included.
This question already has answers here:
How I can index the array starting from 1 instead of zero?
(6 answers)
Closed 5 years ago.
I want to split a string and store in an array from custom index and NOT from "0" index by default.
Eg:
String splitThis = "cat,dog";
String [] array = splitThis.split(",");
System.out.println array[0] + array[1]
Above code prints "catdog" but I want "cat" to be store in index "1" and "dog" in index "2"
PS: I am very new to Programming and this is my very first question. Please correct me in syntax/logic/whatever :)
You may just add an empty entry at index 0. Something like this
String splitThis = "cat,dog";
String spit2 = ","+splitThis;
String [] array = split2.split(",");
System.out.println (array[1]);
System.out.println (array[2]);
You should probably create an entirely new class to handle that.
This question already has answers here:
What Does The Colon Mean In Java?
(5 answers)
Closed 7 years ago.
I have a string handling related question
split() - The return type is String[]
In this for loop we are storing the split value in a String literal
for (String retval: Str.split("-"))
Why doesn't it give a type mismatch error like in the code below?
String Str1 = "abfg-hjddh-jdj";
String Str = Str1.split("-");
String Str = Str1.split("-");
gives error because split returns an array, so the correct syntax is:
String[] Str = Str1.split("-");
In a for-each loop
for (String retval : Str.split("-"))
For each loop : indicates you will be iterating an array, collection or list of Strings, so no error is trhown
Examples:
for (int retval : string.split("-")) // error,
ArrayList<Books> books;
for (Book book : books) // correct
Set<Integer> integers;
for (Integer mInt : integers) // correct
for (String mInt : integers) // incorrect!!!
LAST BUT NOT LEAST: variables in Java should start with LOWERCASE, please check Code Conventions.
This question already has answers here:
How to get a substring of text?
(5 answers)
Closed 8 years ago.
I made a program in java like this:
class substr{
public static void main (String[]args){
int n=1;
String s1="";
String s2=" ";
while (n<=5){
System.out.println(s2.substring(n,5)+n+s1+n);
s1=s1+" ";
s2=s2+" ";
n++;
}
}
}
I can't make use of substring in ruby, so I wanted to know if there is some way. I read about regex, but you don't use it in a program like this.
You can use array notation to substring an string. In your case, something like s2[n,5].