1. Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

Java Help

Discussion in 'Programming & Webmastering' started by Wozzer, Jan 28, 2011.

  1. Wozzer

    Joined:
    Jun 30, 2008
    Messages:
    1,135 (0.34/day)
    Thanks Received:
    68
    Location:
    England
    Problem solved.
     
    Last edited: Feb 1, 2011
  2. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    20,256 (6.32/day)
    Thanks Received:
    9,423
    Location:
    IA, USA
    spaces (0x20) and new lines (0x0D, 0x0A) are both considered "characters." You could omit them from the count by doing a replace but your instructor might expect them.

    This is a console app, no? I would do something like:
    string[] inputs = new string[3];
    for (int i = 0; i < inputs.length; i++)
    inputs = in.readLine()

    The readLine() should omit the 0x0A, 0x0D characters and that way, you'd only get your 3 lines worth.


    If you're trying to do: "one two three" instead of:
    one
    two
    three

    Use a string.split function to turn that "one two three" into:
    [0] one
    [1] two
    [2] three

    To get your 3 limit, do a for loop and only hit the first 3. Make sure there is at least 3 in the array first so it doesn't throw an exception.
     
    Crunching for Team TPU
  3. Wozzer

    Joined:
    Jun 30, 2008
    Messages:
    1,135 (0.34/day)
    Thanks Received:
    68
    Location:
    England
    It's just a simple program. I've read both your suggestions and the only one I'm struggling to understand is the spaces / newlines.

    We haven't been taught about the "0x0D, 0x0A" side of coding so I'm un-sure whether to include this part as it could look a bit advance for what we've been taught. Is this the best way doing it?
     
    Last edited: Feb 1, 2011
  4. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    20,256 (6.32/day)
    Thanks Received:
    9,423
    Location:
    IA, USA
    I would recommend using a BufferedReader instead of a Scanner. That will take care of the carriage returns/line feeds. Here's an example how that is done:
    http://www.java2s.com/Code/Java/File-Input-Output/CreateBufferedReaderfromSystemin.htm

    0x0D and 0x0A are the hex values of ASCII characters:

    0x0D = \r = Carriage Return
    0x0A = \n = Line Feed

    When you press "Enter" in a document, it almost always does \r\n, also know as the carriage return-line feed. The BufferedReader, when calling readLine() will return up to a \r\n and consume both.

    Definitely use a for-loop and an array instead of three individual variables. That will vastly simplify everything.
     
    Last edited: Jan 30, 2011
    Wozzer says thanks.
    Crunching for Team TPU
  5. nick_1992

    Joined:
    Jan 3, 2006
    Messages:
    580 (0.14/day)
    Thanks Received:
    19
    Location:
    Manitoba, Canada
    Well as far as getting rid of the white space you'd use trim()
     
    10 Year Member at TPU
  6. olithereal

    olithereal New Member

    Joined:
    May 24, 2008
    Messages:
    1,262 (0.38/day)
    Thanks Received:
    65
    There is ReplaceFirst() function. Try using that ;)
     
    Wozzer says thanks.
  7. Wozzer

    Joined:
    Jun 30, 2008
    Messages:
    1,135 (0.34/day)
    Thanks Received:
    68
    Location:
    England
    Brilliant - Just tried it and it works :)
     
  8. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,817 (3.62/day)
    Thanks Received:
    5,629
    Location:
    Cheeseland (Wisconsin, USA)
    If you have the time (and the motivation) try to make this work ...

    Output text to tell the user to enter three words on a single input line (ie. one two three)
    Each time a character is entered increment a counter.
    If a space is entered increment a whitespace counter, but not the character counter.
    When a 3rd space or CR is entered, just output the character counter value.

    No need to store what the user types that way, since you are not doing anything with it anyway.
     
    10 Year Member at TPU
  9. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,817 (3.62/day)
    Thanks Received:
    5,629
    Location:
    Cheeseland (Wisconsin, USA)
    Use a variable to hold the number of input lines a user has entered and stop prompting them when it hits three.
     
    10 Year Member at TPU
  10. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,817 (3.62/day)
    Thanks Received:
    5,629
    Location:
    Cheeseland (Wisconsin, USA)
    Something like ...

    while input count < 3
    Prompt user for input.
    On receipt of input, increment input count;

    The loop will stop when input count hits 3

    I'm not giving you code so you figure it out for yourself. :)
     
    10 Year Member at TPU
  11. Wozzer

    Joined:
    Jun 30, 2008
    Messages:
    1,135 (0.34/day)
    Thanks Received:
    68
    Location:
    England
    I'll give it a shot and post the results....
     
  12. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    20,256 (6.32/day)
    Thanks Received:
    9,423
    Location:
    IA, USA
    If it one application calling another, you should use the startup arguments instead of requesting it from the user.

    Your code should have main(String[] args) in it. args is the startup arguments. If you started the application with "one two three" in the arguments, args would contain:
    [0] one
    [1] two
    [2] three

    Just loop through them and do what you want with it.
     
    Crunching for Team TPU
  13. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,817 (3.62/day)
    Thanks Received:
    5,629
    Location:
    Cheeseland (Wisconsin, USA)
    That makes it even easier.
    Use one input parameter (a string of three word).
    If the input is valid, remove the spaces and return the length of the string.
    else return an error code value (any value less than 3 would do as that the shortest valid string length possible ... I would probably use 0 or -1 though).
     
    10 Year Member at TPU
  14. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,817 (3.62/day)
    Thanks Received:
    5,629
    Location:
    Cheeseland (Wisconsin, USA)
    That's because you are trying to think in programming language terms instead of just thinking about it in general terms. This is why many schools use psuedo-code with their beginning students.

    Make it as simple as possible.
    Why ask for 3 inputs when 1 is sufficient?
    Do only what is needed and no more, other wise you just have superfluous code.

    Some psuedo-code ...
    Code:
    Get three words from user in one line
    Check to make sure there are 3 words
    Are there 3 words?
      Yes : Continue
      No : Return an error
    Make the three words one big word by removing the spaces and NL
    Return the length of the new big word
    End
    
     
    10 Year Member at TPU
  15. Wozzer

    Joined:
    Jun 30, 2008
    Messages:
    1,135 (0.34/day)
    Thanks Received:
    68
    Location:
    England
    I was just reading about psuedo-code and it does seem to help. If i was to go by your psuedo-code, the line I would be stuck on is ''Check to make sure there are 3 words''.

    Thats what I can't change into Java. I know I have to do it - Just not sure how to program it.
     
  16. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,817 (3.62/day)
    Thanks Received:
    5,629
    Location:
    Cheeseland (Wisconsin, USA)
    Remove any whitespace from the beginning and end of the input line.
    Check for more than 2 spaces in the line.
    If there are more and they are not consecutive, you have more than 3 words.
     
    10 Year Member at TPU
  17. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    20,256 (6.32/day)
    Thanks Received:
    9,423
    Location:
    IA, USA
    Most of your problems stem from the Scanner. It is a regular expressions based tokenizer.

    Again, I stress using a BufferedReader instead. Something like this:
    Code:
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    
    public class Main {
      public static void main(String[] args) throws Exception {
        // If the words were not passed via the startup args, request them.
        if (args.length() == 0) {
          // Get it from console on one line, space delimited.
          BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
          args = in.readLine().split(" ");
          in.close();
        }
    
        int charcount = 0; // Keep a running count.
        for (int i = 0; i < args.Length; i++) {
          charcount += args[i].length();  // Add length of index argument to charcount.
        }
        System.out.printIn("You entered " + charcount + " characters.");
      }
    }
    With that code, it doesn't matter if it was given via the commandline or typed in the console. It will get it either way.
     
    Crunching for Team TPU
  18. Wozzer

    Joined:
    Jun 30, 2008
    Messages:
    1,135 (0.34/day)
    Thanks Received:
    68
    Location:
    England
    Although what you say is true - As I havent been taught how to use the BufferReader, I'd be better off using the scanner method.

    I'll attempt the program again and post my results.
     
  19. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    20,256 (6.32/day)
    Thanks Received:
    9,423
    Location:
    IA, USA
    They introduced the Scanner to you? BufferReader is far simpiler than Scanner. All BufferedReader has is .read and .readLine. .readLine is very useful for doing the things you are trying to do (takes the \r\n out of the equation).
     
    Last edited: Jan 30, 2011
    Crunching for Team TPU
  20. Wozzer

    Joined:
    Jun 30, 2008
    Messages:
    1,135 (0.34/day)
    Thanks Received:
    68
    Location:
    England
    Yeah - Scanner was one of the first things we did. Havent heard of BufferReader until you mentioned it.
     
  21. olithereal

    olithereal New Member

    Joined:
    May 24, 2008
    Messages:
    1,262 (0.38/day)
    Thanks Received:
    65
    Funny, BufferedReader was the first thing my teacher thought us when we started Java this semester. I think it works better IMO.
     
  22. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    20,256 (6.32/day)
    Thanks Received:
    9,423
    Location:
    IA, USA
    Same. It is the equivilent of the StreamReader in .NET which I use all the time. Regular Expressions and Tokenizers are extremely complex to be teaching right off the bat.
     
    Crunching for Team TPU
  23. Bjorn_Of_Iceland

    Bjorn_Of_Iceland

    Joined:
    Jan 2, 2008
    Messages:
    3,234 (0.93/day)
    Thanks Received:
    397
    use String's split(). then get the length. regex for space is "\\s", line is "\n"
     

Currently Active Users Viewing This Thread: 1 (0 members and 1 guest)