java string add comma become array [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
String items = "bookcupdoll";
I want to add a comma in items variable like below:
book,cup,doll
How should I make the string become an array?

If you are asking how to convert a string to an array...
There is a handy method called "split(String separator)"
If the string looks like : String items = "book,cup,doll";
you can use it like this
String[] array = items.split(",");
And you will end up with an array of size 3 with the 3 different elements.

Related

Need to add a default value in a string inside a method in java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Suppose we have a string BE231231.
I need an output like this : BE0231231
need to append zero at a this fixed location in java inside a method
public String appendAtIndex(String base, String toAppend, int index){
return base.substring(0,index) + toAppend + base.substring(index);
}
You can implement the logic for restrictions on index accordingly so that base.substring does not give errors.

Find the specific word in the text file and to store that word in array list [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I want to find the specific word in the text file and store that word in the array of list.
I do not how it can be achieved or approached.
A basic solution:
public ArrayList getWords(String searchWord, String listOfWords){
ArrayList<String> foundWords = new ArrayList<>();
if (listOfWords.contains(searchWord)){
foundWords.add(searchWord);
}
return foundWords;
}

How to convert a String to ArrayList? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I store an ArrayList in a file as a String ( arrayList.toString()).
How do i convert it back into an ArrayList?
Overall, I just want to convert something like: [Eve, Anna, Tony, Steve] back to an ArrayList. Any ideas?
All you need is a java.lang.String's split() & java.util.Arrays asList():
String fruits = "Orange,Apple,Grape";
String[] array = fruits.split(",");
List<String> list = Arrays.asList(array);
//Code to check the content in the List (for your understanding only)
for(String s : list) {
System.out.println(s);
}
Prints below output:
Orange
Apple
Grape

Java mismatch between String and String[] [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm using Java to implement the Apriori algorithm but one problem exists.
ArrayList<String> l1=new ArrayList<>();//L1
...
ArrayList<String[]> lk1=l1;//Lk-1
Then it warns that: cannot convert from ArrayList<String> to ArrayList<String[]>. How can I solve this problem?
They're different types. Ignore the collection and just look at the underlying type.
String str = "This is a string.";
String[] array = str;
These are two different types, unconvertable, so as T for the collection, the collections are then unconvertable. Very basic concept here.

Changing the Lengths of Words in a String - Java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am currently writing a game in Java, where words in a string will have to be all changed to equal 5 characters a word.
eg. I am writing in Java
Iamwr iting inJav a
I wonder if anyone knows how I would do this?
First remove spaces between words.
Then split that resulting String to length with 5.
Add a space after every 5 character.

Categories

Resources