Results 1 to 3 of 3
Thread: blank space in String won't die!
- 07-29-2011, 04:46 AM #1
Member
- Join Date
- Jul 2011
- Posts
- 23
- Rep Power
- 0
blank space in String won't die!
I need to convert a string of time to an int. In other words "4:30PM" must become 430.
I have used the replaceAll() String method to get replace the colon and letters with " ". However I keep getting a NumberFormatException. I assume because of the space left, the string at this point is "04 30". I have tried the trim() String method but it doesn't work! Next I tried a stringBuffer and the deleteCharAt() method but when I print my Course object (using a toString method) it still has the dang space!!! Any suggestions?
Java Code:public static Course fromAString(String x) { Course temp = new Course(); StringTokenizer cl = new StringTokenizer(x); temp.setName(cl.nextToken()); temp.setSection(cl.nextToken()); String startT = cl.nextToken(); startT = startT.replaceAll(":"," "); startT = startT.replaceAll("AM",""); startT = startT.replaceAll("PM",""); StringBuffer deletespace = new StringBuffer(startT); deletespace.deleteCharAt(2); startT = "" + deletespace; startT.trim(); int starter = Integer.parseInt(startT); temp.setStartTime(starter); String endT = cl.nextToken(); endT = endT.replaceAll(":"," "); endT = startT.replaceAll("AM",""); endT = startT.replaceAll("PM",""); endT.trim(); int ender = Integer.parseInt(endT); temp.setStartTime(ender); temp.setDays(cl.nextToken()); String n = temp.getName(); String sect = temp.getSection(); int et = temp.getEndTime(); int st = temp.getStartTime(); String d = temp.getDays(); String whole = n + " " + sect + " " + st + " " + et + " " + d; return temp; }
- 07-29-2011, 04:48 AM #2
Why are you replacing the colon with a space? Why don't you replace it with an empty String like you do AM and PM?
- 07-29-2011, 04:56 AM #3
Member
- Join Date
- Jul 2011
- Posts
- 23
- Rep Power
- 0
Similar Threads
-
how to repalce space with %20 in a string
By Basit781 in forum CLDC and MIDPReplies: 4Last Post: 06-13-2012, 08:08 PM -
print out string name with the space :) between the first and last name
By the beginner in forum New To JavaReplies: 3Last Post: 02-03-2011, 10:19 PM -
Get item from string array and pack with blank space
By firewalll in forum New To JavaReplies: 2Last Post: 09-02-2009, 07:38 AM -
Blank space
By sandy1028 in forum New To JavaReplies: 1Last Post: 04-21-2009, 10:00 AM -
Print a blank space
By susan in forum New To JavaReplies: 2Last Post: 07-30-2007, 01:58 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks