How to print array with delimiter [duplicate] - java

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

Related

Java String.split() function gives wrong outputs [duplicate]

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);

How to take in a String and return an array after modifying the String? [duplicate]

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.

ArrayList to Array not displaying the way I want to [duplicate]

This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 5 years ago.
ArrayList<String> nameList = new ArrayList<String>();
nameList.add("James");
nameList.add("Joe");
nameList.add("Josh");
nameList.add("Bob");
nameList.add("Billy");
System.out.println("The names are:\n " + nameList);
nameList.remove(1);
System.out.println("Find the index 1 and remove their name from the list:\n "+ nameList);
nameList.size();
System.out.println("Currently the ArrayList holds " + nameList.size() +
" names after removing the name of index 1" );
String[] tempArray= new String[nameList.size()];
tempArray = nameList.toArray(tempArray);
System.out.println(tempArray); // I want it to display the values of the ArrayList
}
}
The output I'm getting is [Ljava.lang.String;#4554617c when I want it to look like this [James, Josh, Bob, Billy]. New to programming would appreciate the help.
A couple of things.
Firstly, this code could be reduced.
String[] tempArray= new String[nameList.size()];
tempArray = nameList.toArray(tempArray);
could become
String[] tempArray= nameList.toArray(new String[nameList.size()]);
if I am not mistaken.
Secondly, an ArrayList automatically resizes itself with no code needed. How it does that is a whole new question.
And finally. The way objects are printed in Java is that if they are not primaries (int,bool,long,char...), they are printed using their .toString() method. On ArrayLists, this .toString() method returns a nice pretty representation of the list. But arrays don't define this method, and they instead get printed as some JVM dependent String. Instead, use Arrays.toString(tempArray) method. So your print statement would look like this:
System.out.println(Arrays.toString(tempArray));
Don't forget to import java.util.Arrays.

java split function. Store in an array from custom index? [duplicate]

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.

How to print an int array returned in a method in the main function , Java [duplicate]

This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 6 years ago.
First of all,I am newbie in Java and I came across this issue several times. I am trying to return the number of occurrences of a char in a string and I implemented a method called countcharacters which looks like this:
public static int[] countcharacters(String s)
{
String alphabet="abcdefghijklmnopqrstuvwxyz";
int[] sircounter=new int[alphabet.length()];
for(int i=0;i<sircounter.length;i++)
{
sircounter[i]=0;
}
for(int j=0;j<alphabet.length();j++)
{
for(int i=0;i<s.length();i++)
{
if(alphabet.charAt(j)==(s.charAt(i)))
{
sircounter[j]++;
}
}
}
return sircounter;
}
In other words, each time a character from alphabet is found in my string ,the argument of the method, the value zero from each position from sircounter is incremented with the number of occurrence.
I wanted to print the array position by position but it didn't work and if
I do something like this in the main method:
String text="hannah";
System.out.println(Class.countcharacters(text));
I get a hex code: [I#659e0bfd which I don't understand.
Could you help me?
Many thanks and have a nice day!
This is happening because array is treated as an object in java.And when you try to print an object in java you get its hash code(as long as that object hasn't have toString() defined!)
Try this rather-
String text="hannah";
int counter[]=Class.countcharacters(text);
for(int i=0;i<counter.length;i++){
System.out.print(counter[i]+" ");
}

Categories