Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Can anyone help?

  1. #1
    Junior Member
    Join Date
    Oct 2017
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Can anyone 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

    My code is :

    int sum = 0;
    StringTokenizer st = new StringTokenizer(line);
    while (st.hasMoreTokens()) {
    sum += Integer.parseInt(st.nextToken());
    }
    System.out.println(sum);

    I don't know why my code can't compile.

  2. #2
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    270
    My Mood
    Amused
    Thanks
    8
    Thanked 18 Times in 18 Posts

    Default Re: Can anyone help?

    Please post your complete code here
    Whatever you are, be a good one

  3. #3
    Junior Member
    Join Date
    Oct 2017
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can anyone help?

    import java.io.*;

    public class EchoSum extends Echo
    {

    public EchoSum (String datafile) throws IOException
    {
    super(datafile);
    }

    // Prints the sum of each line.
    public void processLine(String line){
    int sum = 0;
    StringTokenizer st = new StringTokenizer(line);
    while (st.hasMoreTokens()) {
    sum += Integer.parseInt(st.nextToken());
    }
    System.out.println(sum);
    }
    }

  4. #4
    Member
    Join Date
    Aug 2017
    Location
    Northern Ireland
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can anyone help?

    What is the compiler error you are getting?
    Tim Driven Development

  5. #5
    Junior Member
    Join Date
    Oct 2017
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can anyone help?

    It doesn't say what the error is, but can't get it correctly.

  6. #6
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    270
    My Mood
    Amused
    Thanks
    8
    Thanked 18 Times in 18 Posts

    Default Re: Can anyone help?

    Where to get the datafile ? What is the output you get ?
    Whatever you are, be a good one

  7. #7
    Member
    Join Date
    Aug 2017
    Location
    Northern Ireland
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can anyone help?

    If the code won't compile then the compiler will absolutely present you with a message about why. Perhaps you're using an IDE that's hiding it from you? Can you try compiling from the command line using javac instead?

    You say "can't get it correctly". Get what correctly? What does that mean exactly?
    Tim Driven Development