# Java Help



## Wozzer (Jan 28, 2011)

Problem solved.


----------



## FordGT90Concept (Jan 28, 2011)

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._


----------



## Wozzer (Jan 28, 2011)

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?


----------



## FordGT90Concept (Jan 28, 2011)

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.


----------



## nick_1992 (Jan 28, 2011)

Well as far as getting rid of the white space you'd use trim()


----------



## olithereal (Jan 29, 2011)

There is ReplaceFirst() function. Try using that


----------



## Wozzer (Jan 29, 2011)

olithereal said:


> There is ReplaceFirst() function. Try using that



Brilliant - Just tried it and it works


----------



## Kreij (Jan 29, 2011)

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.


----------



## Kreij (Jan 29, 2011)

Use a variable to hold the number of input lines a user has entered and stop prompting them when it hits three.


----------



## Kreij (Jan 29, 2011)

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.


----------



## Wozzer (Jan 29, 2011)

Kreij said:


> Something like ...
> 
> while input count < 3
> Prompt user for input.
> ...



I'll give it a shot and post the results....


----------



## FordGT90Concept (Jan 30, 2011)

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.


----------



## Kreij (Jan 30, 2011)

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).


----------



## Kreij (Jan 30, 2011)

Wasley said:


> I can't stand programming



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 ...

```
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
```


----------



## Wozzer (Jan 30, 2011)

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.


----------



## Kreij (Jan 30, 2011)

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.


----------



## FordGT90Concept (Jan 30, 2011)

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:

```
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.


----------



## Wozzer (Jan 30, 2011)

FordGT90Concept said:


> 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.



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.


----------



## FordGT90Concept (Jan 30, 2011)

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).


----------



## Wozzer (Jan 31, 2011)

Yeah - Scanner was one of the first things we did. Havent heard of BufferReader until you mentioned it.


----------



## olithereal (Feb 1, 2011)

Wasley said:


> Yeah - Scanner was one of the first things we did. Havent heard of BufferReader until you mentioned it.



Funny, BufferedReader was the first thing my teacher thought us when we started Java this semester. I think it works better IMO.


----------



## FordGT90Concept (Feb 1, 2011)

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.


----------



## Bjorn_Of_Iceland (Feb 1, 2011)

Wasley said:


> - It counts the amount of spaces/newlines


use String's split(). then get the length. regex for space is "\\s", line is "\n"


----------

