Results 1 to 3 of 3
  1. #1
    MetalR0 is offline Member
    Join Date
    Jul 2011
    Posts
    23
    Rep Power
    0

    Default 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;				
    		
    	}

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default

    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?

  3. #3
    MetalR0 is offline Member
    Join Date
    Jul 2011
    Posts
    23
    Rep Power
    0

    Default

    Because I can't see the forest for the trees. Thanks for pointing out the oversight. No problem now!

Similar Threads

  1. how to repalce space with %20 in a string
    By Basit781 in forum CLDC and MIDP
    Replies: 4
    Last Post: 06-13-2012, 08:08 PM
  2. Replies: 3
    Last Post: 02-03-2011, 10:19 PM
  3. Get item from string array and pack with blank space
    By firewalll in forum New To Java
    Replies: 2
    Last Post: 09-02-2009, 07:38 AM
  4. Blank space
    By sandy1028 in forum New To Java
    Replies: 1
    Last Post: 04-21-2009, 10:00 AM
  5. Print a blank space
    By susan in forum New To Java
    Replies: 2
    Last Post: 07-30-2007, 01:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •