How to get values from a delimited string? [duplicate] - java

This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 7 years ago.
I have a delimited string and The delimiter is "$%". The fields are in an specific order. For example: John$%Smith$%30$%Los Angeles
I need to get the values of this string and store them in the respective property
Customer.firstName(_)
Customer.lastName(_)
Customer.age(_)
Customer.city(_)
So far I have tried this but I know I am doing it incorrectly:
if(thisString != null){
if(thisString.endsWith("$%")){
Customer.firstName(thisString.substring(0,indexOf("$%");
}
}

Trying using Java's String.split
For example:
//Split string based on "$%"
String[] values = thisString.split(Pattern.quote("$%"));
Customer.firstName() = values[0]; //Set first name equal to first string from the split
//etc

Related

How to make a while loop end with a specific input [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 2 years ago.
This is the code I have right now and can't figure out why this while loop isnt ending when there is a period entered.
while (emailTxt!="."){
emailTxt = "";
emailTxt = scanner.nextLine();
totalText.add(emailTxt);
}
emailTxt is a String. A String in Java is considered an Object. You want to use the Object.equals().
So
while(!(emailTxt.equals("."))
You should use equals() to compare two string.
while (!".".equals(emailTxt)) {
// ...
}

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.

Splitting string by multiple delimiters [duplicate]

This question already has answers here:
how to get data between quotes in java?
(6 answers)
Closed 6 years ago.
I have a string of names. I am looking to split it based on names between double quotes. I used the following code to split the names.
String []splitterString=str.split("\"");
for (String s : splitterString) {
System.out.println(s);
}
I get the output as:
[
Hossain, Ziaul
,
Sathiaseelan, Arjuna
,
Secchi, Raffaello
,
Fairhurst, Gorry
]
I need to store just the names from these. I am not sure how to do that.
This is the string:
["Hossain, Ziaul","Sathiaseelan, Arjuna","Secchi, Raffaello","Fairhurst, Gorry"]
Thanks for the help!!!
I think following solution may help you out :-
String str = "[\"Hossain, Ziaul\",\"Sathiaseelan, Arjuna\",\"Secchi, Raffaello\",\"Fairhurst, Gorry\"]";
String [] str1 = str.split("\\[\"|\",\"|\"\\]");
for (int iCount = 0; iCount < str1.length; iCount++)
{
System.out.println(str1[iCount]);
}

count occurences of successive characters of a string [duplicate]

This question already has answers here:
Java compressing Strings
(21 answers)
Closed 7 years ago.
I have a String and I need to count occurances of successive characters of the string.
String s1="aabbcccaaa";
It should print output as
a2b2c3a3
String s1="aabbcccaaa";
char c[]=s1.toCharArray();
StringBuffer sb=new StringBuffer();
LinkedHashMap<Character,Integer> map=new LinkedHashMap<Character,Integer>();
for(int i=0;i<c.length;i++)
{
Character c1=c[i];
Integer frequency=map.get(c1);
map.put(c1, (frequency==null)?1:frequency+1);
if(map.size()>1 && c[i]!=c[i-1])
{
sb.append(c[i-1]+""+map.get(c[i-1]));
map.remove(c[i-1]);
}
}
sb.append(map);
System.out.println(sb);
I am not sure how i can achieve counts if same elements occur again.
Hint #1: Test the current character against the previous one.
Hint #2: Ask yourself, why do you need to use a Map?

Frequency distribution of words [duplicate]

This question already has answers here:
Calculating frequency of each word in a sentence in java
(20 answers)
Closed 8 years ago.
I have read in two txt files with words.
Now I was wondering how to calculate the frequency distribution of these two Strings. My idea is to put them into an array and split them by ;. However, how to calculated the frequency distribution?
I appreciate your reply!
Sample code is something like this:
String str1 = "java;python;javascript;programming;Hello;World;Hello";
String str2 = "java;python;javascript;programming;Hello;World;Hello";
List<String> list = new ArrayList<String>();
list.addAll(Arrays.asList(str1.split(";")));
list.addAll(Arrays.asList(str2.split(";")));
for (String word : list)
System.out.println(word + " --> " + Collections.frequency(list,word));

Categories