Page 2 of 2 FirstFirst 12
Results 21 to 30 of 30
  1. #21
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    If we are all understanding you, then it sounds like you have a single number on each line, you want to read the number, parse it, and keep a running total. It sounds like you are having difficulty parsing the numbers.

    So, let's go one by one.

    1) Read the number.

    Since the numbers are in a text file, the suggestion is use BufferedReader.readLine.

    2) Parse the number.

    Since it's an assignment, you might have restrictions on what you can and cannot use, but I'm a regex maniac, so I would use a regular expression to parse the numbers.

    Java Code:
    String numbers = "1 2 3";
    		
    // matches a number - ex. 123
    Pattern pattern = Pattern.compile("\\d++");
    Matcher matcher = pattern.matcher(numbers);
    		
    while(matcher.find())
    {
    	// parse the numbers and keep a running total here
    	System.out.println(matcher.group());
    }

    2b) Now that you can extract a single number, parse it, and retrieve the int value. Check out the Integer.parseInt method to retrieve the int value for the number.

    3) Keep a running total. Since you are trying to do an average, you need the total count, and the total number of numbers.


    Hope that helps and gives you a push in the right direction.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  2. #22
    raqman is offline Member
    Join Date
    Sep 2009
    Posts
    37
    Rep Power
    0

    Default

    Use stringtokenizer to break up the token using " " as the delimiter.
    Once you have done that, use Integer.parseInt to convert each of the strings to int and then add them. Easy.
    RAQ Report: free Java reporting tool.

  3. #23
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by raqman View Post
    Use stringtokenizer to break up the token using " " as the delimiter.
    Once you have done that, use Integer.parseInt to convert each of the strings to int and then add them. Easy.
    Rather don't do any of that.
    Rather read the replies that have been posted in this thread already.

    Also read the API specs for StringTokenizer to see you should use it in this case.

  4. #24
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Quote Originally Posted by CodesAway View Post
    2) Parse the number.

    Since it's an assignment, you might have restrictions on what you can and cannot use, but I'm a regex maniac, so I would use a regular expression to parse the numbers.
    Regex?
    Why on earth use a regex to parse a number when Integer.valueOf() does a perfectly good job, and gives you an int at the end of it?

    When you need to put a screw in the wall do you reach for a hammer?

  5. #25
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    The OP never confirmed that the input was guaranteed to be one number on each line. If there is only one number on a line (and nothing else), regex is overkill. In all other cases, regex is how I would go.

    Like I said, I'm a regex maniac.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  6. #26
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    It's pretty clear from the original post it's one int per line.

    However, even if it wasn't and they were separated by a " " simply split() on the space. yes, technically it's a regex, but faffing with Matchers is unecessary obfuscation. Wouldn't get past a decent code review.

  7. #27
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    Ok, ok, when I go back to college and take intro to computer science I, I won't use a regex. :)
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  8. #28
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Right tool for the right job, mate.
    Code is about clarity and maintainability, not showing off.

  9. #29
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  10. #30
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

Page 2 of 2 FirstFirst 12

Similar Threads

  1. parsing numbers in a string
    By rsoler in forum Advanced Java
    Replies: 4
    Last Post: 03-31-2009, 06:05 AM
  2. Replies: 5
    Last Post: 01-11-2009, 10:12 AM
  3. printing two smallest numbers from a series of numbers
    By trofyscarz in forum New To Java
    Replies: 2
    Last Post: 10-14-2008, 11:46 PM
  4. Replies: 0
    Last Post: 11-20-2007, 04:59 PM
  5. validating a string for numbers and letters?
    By lockmac in forum New To Java
    Replies: 1
    Last Post: 08-09-2007, 09:17 AM

Posting Permissions

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