Reputation: 1
This is a beginner course so there is probably an easier method, but can someone look at my code and tell me why it wont split the last string and print it? I split it successfully the first two times.
//////////////////////////////////// String line = "user=Abby&topic=0&message=I+cannot+wait+for+the+snow.";
String[] parts = line.split("&"); //split&
String part1 = parts[0]; // user=Abby
String part2 = parts[1];// topic=0
String part3 = parts[2];// message=I+cannotwaitforthesnow
String[] user = part1.split("="); //Split USER=Abby
String user1 = user[0]; // user
String user2 = user[1]; // Abby
String[] topic = part2.split("="); //split topi=0
String topic1 = topic[0]; // Topic
String topic2 = topic[1]; // 0
String[] text = part3.split("="); //split message=iasd
String text1 = text[0]; // message
String text2 = text[1]; // I+cannot+wait+for+the+snow
String[] message = text2.split("+"); //split I+cannot+wait+for+the+snow.
String message1 = message[0];//I
String message2 = message[1];//cannot
String message3 = message[2];//wait
String message4 = message[3];//for
String message5 = message[4];//the
String message6 = message[5];//snow.
output.println("This is the input that the client sent: ");
System.out.println(user2);
System.out.println(topic1 + topic2);
System.out.println(message1 + message2 + message3 + message4 + message5 + message6);
//////////////////////
so it successfully works, but when i added the split for message at the end it did not split and print, just blank. an anyone tell me why?
thanks for any help
Upvotes: 0
Views: 91
Reputation: 154
You should try
String[] message = text2.split("\\+");
In java '+' is a reserved character, you should escape it with '\' in order to use it in this context
Upvotes: 0
Reputation: 4048
The Javadocs say
public String[] split(String regex)
Splits this string around matches of the given regular expression.
And + is a reserved character in regex so you need to escape it.
Upvotes: 4