Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By Junky

Thread: StringTokenizer, making a method help

  1. #1
    katiebear128 is offline Member
    Join Date
    Sep 2011
    Posts
    54
    Rep Power
    0

    Default StringTokenizer, making a method help

    Suppose an external file is made up entirely of integers. In the model we've been using in this unit, the file is actually read in, line by line, as a sequence of Strings. Using a StringTokenizer inside processLine() method can produce individual tokens that look like numbers, but are actually Strings that are composed of digits. To convert each token from String format to integer format, you need to use the parseInt() method from the Integer class. Thus

    int birthYear = Integer.parseInt("1983");

    correctly stores the integer 1983 into the birthYear integer variable.

    For this assignment you should create a complete processLine method in the EchoSum class, so that it transforms each number in each line to integer format, and then sums the entries on the line and prints their sum on a separate line. For example, if the external text file looks like this:
    1 2 3 4
    3 4 7 1 11
    2 3


    your program should display this:
    10
    26
    5


    Java Code:
    public void processLine(line){
    int sum=0;
    StringTokenizer s=new StringTokenizer(line);
    while(s.hasMoreTokens()){
    int i=Integer.parseInt(line);
    sum+=i;
    }
    System.out.println(sum);
    Right now it says runtime error, java.lang.NumberFormatException...

    I'm wondering how I can fix my code to do what its supposed to do, I would really appreciate any help!

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: StringTokenizer, making a method help

    int i=Integer.parseInt(line);
    Given your input, the above would be similar to:
    Java Code:
    int i=Integer.parseInt("1 2 3 4");
    Try it and see if this works. Are you sure you want to parse the line, or parse the token from the tokenizer?

  3. #3
    katiebear128 is offline Member
    Join Date
    Sep 2011
    Posts
    54
    Rep Power
    0

    Default Re: StringTokenizer, making a method help

    I tried int i=Integer.parseInt(s); but it said error. Should I try it again?

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

    Default Re: StringTokenizer, making a method help

    Quote Originally Posted by katiebear128 View Post
    Should I try it again?
    Why? Do you think you will get a different result each time? You cannot parse the enter line. You cannot parse a StringTokenizer. But you can parse the individual elements in the StringTokenizer.
    katiebear128 likes this.

  5. #5
    katiebear128 is offline Member
    Join Date
    Sep 2011
    Posts
    54
    Rep Power
    0

    Default Re: StringTokenizer, making a method help

    thanks Junky! You reminded me to use the nextToken() method. I completely forgot about that

Similar Threads

  1. Java stringtokenizer- making it reverse word
    By katiebear128 in forum New To Java
    Replies: 10
    Last Post: 09-29-2013, 04:20 PM
  2. making method, repurposing
    By katiebear128 in forum New To Java
    Replies: 5
    Last Post: 10-27-2011, 03:21 PM
  3. Making a method exercise question
    By katiebear128 in forum New To Java
    Replies: 6
    Last Post: 10-01-2011, 08:48 PM
  4. Help regarding StringTokenizer
    By mahesh19nov in forum New To Java
    Replies: 3
    Last Post: 10-26-2010, 09:25 AM
  5. Help with StringTokenizer!
    By ookie833 in forum New To Java
    Replies: 13
    Last Post: 12-14-2008, 04:09 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
  •