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

Java Help

Joined
Jun 30, 2008
Messages
1,135 (0.32/day)
Likes
67
Location
England
System Name Wasleys PC
Processor Intel i5 2400 3.10GHz
Motherboard Asus P8z68-V
Cooling AC Freezer Pro
Memory Kingston 4GB (2x2GB) DDR3 Hyperx Memory
Video Card(s) HIS ATi 6850 1GB DDR3
Storage Seagate ST3500320AS 500GB Hard Drive SATA II 7200rpm *32MB Cache*
Case Antec 900 with mods
Audio Device(s) On Board
Power Supply OCZ Stealth Xtream 500W
Software Windows 7
#1
Problem solved.
 
Last edited:

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
21,083 (6.22/day)
Likes
10,212
Location
IA, USA
System Name BY-2015
Processor Intel Core i7-6700K (4 x 4.00 GHz) w/ HT and Turbo on
Motherboard MSI Z170A GAMING M7
Cooling Scythe Kotetsu
Memory 2 x Kingston HyperX DDR4-2133 8 GiB
Video Card(s) PowerColor PCS+ 390 8 GiB DVI + HDMI
Storage Crucial MX300 275 GB, Seagate 6 TB 7200 RPM
Display(s) Samsung SyncMaster T240 24" LCD (1920x1200 HDMI) + Samsung SyncMaster 906BW 19" LCD (1440x900 DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay
Audio Device(s) Realtek Onboard, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse SteelSeries Sensei RAW
Keyboard Tesoro Excalibur
Software Windows 10 Pro 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
#2
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.
 
Joined
Jun 30, 2008
Messages
1,135 (0.32/day)
Likes
67
Location
England
System Name Wasleys PC
Processor Intel i5 2400 3.10GHz
Motherboard Asus P8z68-V
Cooling AC Freezer Pro
Memory Kingston 4GB (2x2GB) DDR3 Hyperx Memory
Video Card(s) HIS ATi 6850 1GB DDR3
Storage Seagate ST3500320AS 500GB Hard Drive SATA II 7200rpm *32MB Cache*
Case Antec 900 with mods
Audio Device(s) On Board
Power Supply OCZ Stealth Xtream 500W
Software Windows 7
#3
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:

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
21,083 (6.22/day)
Likes
10,212
Location
IA, USA
System Name BY-2015
Processor Intel Core i7-6700K (4 x 4.00 GHz) w/ HT and Turbo on
Motherboard MSI Z170A GAMING M7
Cooling Scythe Kotetsu
Memory 2 x Kingston HyperX DDR4-2133 8 GiB
Video Card(s) PowerColor PCS+ 390 8 GiB DVI + HDMI
Storage Crucial MX300 275 GB, Seagate 6 TB 7200 RPM
Display(s) Samsung SyncMaster T240 24" LCD (1920x1200 HDMI) + Samsung SyncMaster 906BW 19" LCD (1440x900 DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay
Audio Device(s) Realtek Onboard, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse SteelSeries Sensei RAW
Keyboard Tesoro Excalibur
Software Windows 10 Pro 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
#4
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:
Joined
Jan 3, 2006
Messages
580 (0.13/day)
Likes
19
Location
Manitoba, Canada
Processor Intel i7 3820 @ 4.3 Ghz
Motherboard Asus P9 x79 Pro
Cooling Fans 6x 120mm, 1x 140mm, 2x 200mm
Memory 16GB corsair dominator platinum DDR3 1866
Video Card(s) Asus GTX 670 DCII TOP 2GB
Storage 240GB Intel 330 SSD, 640 GB WD, 2 TB WD, 3 TB Segate GoFlex Desk USB 3.0
Display(s) Samsung 19inch widescreen
Case Coolermaster HAF XM
Audio Device(s) onbored
Power Supply pc power and cooling 750watt
Software Windows 7 SP1
Benchmark Scores 3D Mark 11 = P9951 http://www.3dmark.com/3dm11/4564568
#5
Well as far as getting rid of the white space you'd use trim()
 
Joined
May 24, 2008
Messages
1,262 (0.36/day)
Likes
65
System Name Geometro
Processor i5 760 @ Stock (need 1156 brackets for my cooler)
Motherboard MSI P55A-G55
Cooling Kingwin RVT-12025 HDT
Memory 2x4GB Mushkin Blackline Frostbyte 1600 C9-9-9-24
Video Card(s) GTX 275 MSI Twin Frozr OC
Display(s) Samsung 2333SW 23"
Case Lancool PC-K62
Power Supply Corsair VX550W 550W ATX 12V
Software Windows 7 64 bit / Arch Linux x64
#6
There is ReplaceFirst() function. Try using that ;)
 
Joined
Jun 30, 2008
Messages
1,135 (0.32/day)
Likes
67
Location
England
System Name Wasleys PC
Processor Intel i5 2400 3.10GHz
Motherboard Asus P8z68-V
Cooling AC Freezer Pro
Memory Kingston 4GB (2x2GB) DDR3 Hyperx Memory
Video Card(s) HIS ATi 6850 1GB DDR3
Storage Seagate ST3500320AS 500GB Hard Drive SATA II 7200rpm *32MB Cache*
Case Antec 900 with mods
Audio Device(s) On Board
Power Supply OCZ Stealth Xtream 500W
Software Windows 7
#7

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (3.45/day)
Likes
5,524
Location
Cheeseland (Wisconsin, USA)
#8
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

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (3.45/day)
Likes
5,524
Location
Cheeseland (Wisconsin, USA)
#10
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. :)
 
Joined
Jun 30, 2008
Messages
1,135 (0.32/day)
Likes
67
Location
England
System Name Wasleys PC
Processor Intel i5 2400 3.10GHz
Motherboard Asus P8z68-V
Cooling AC Freezer Pro
Memory Kingston 4GB (2x2GB) DDR3 Hyperx Memory
Video Card(s) HIS ATi 6850 1GB DDR3
Storage Seagate ST3500320AS 500GB Hard Drive SATA II 7200rpm *32MB Cache*
Case Antec 900 with mods
Audio Device(s) On Board
Power Supply OCZ Stealth Xtream 500W
Software Windows 7
#11
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. :)
I'll give it a shot and post the results....
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
21,083 (6.22/day)
Likes
10,212
Location
IA, USA
System Name BY-2015
Processor Intel Core i7-6700K (4 x 4.00 GHz) w/ HT and Turbo on
Motherboard MSI Z170A GAMING M7
Cooling Scythe Kotetsu
Memory 2 x Kingston HyperX DDR4-2133 8 GiB
Video Card(s) PowerColor PCS+ 390 8 GiB DVI + HDMI
Storage Crucial MX300 275 GB, Seagate 6 TB 7200 RPM
Display(s) Samsung SyncMaster T240 24" LCD (1920x1200 HDMI) + Samsung SyncMaster 906BW 19" LCD (1440x900 DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay
Audio Device(s) Realtek Onboard, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse SteelSeries Sensei RAW
Keyboard Tesoro Excalibur
Software Windows 10 Pro 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
#12
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

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (3.45/day)
Likes
5,524
Location
Cheeseland (Wisconsin, USA)
#13
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

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (3.45/day)
Likes
5,524
Location
Cheeseland (Wisconsin, USA)
#14
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 ...
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
 
Joined
Jun 30, 2008
Messages
1,135 (0.32/day)
Likes
67
Location
England
System Name Wasleys PC
Processor Intel i5 2400 3.10GHz
Motherboard Asus P8z68-V
Cooling AC Freezer Pro
Memory Kingston 4GB (2x2GB) DDR3 Hyperx Memory
Video Card(s) HIS ATi 6850 1GB DDR3
Storage Seagate ST3500320AS 500GB Hard Drive SATA II 7200rpm *32MB Cache*
Case Antec 900 with mods
Audio Device(s) On Board
Power Supply OCZ Stealth Xtream 500W
Software Windows 7
#15
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

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (3.45/day)
Likes
5,524
Location
Cheeseland (Wisconsin, USA)
#16
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

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
21,083 (6.22/day)
Likes
10,212
Location
IA, USA
System Name BY-2015
Processor Intel Core i7-6700K (4 x 4.00 GHz) w/ HT and Turbo on
Motherboard MSI Z170A GAMING M7
Cooling Scythe Kotetsu
Memory 2 x Kingston HyperX DDR4-2133 8 GiB
Video Card(s) PowerColor PCS+ 390 8 GiB DVI + HDMI
Storage Crucial MX300 275 GB, Seagate 6 TB 7200 RPM
Display(s) Samsung SyncMaster T240 24" LCD (1920x1200 HDMI) + Samsung SyncMaster 906BW 19" LCD (1440x900 DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay
Audio Device(s) Realtek Onboard, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse SteelSeries Sensei RAW
Keyboard Tesoro Excalibur
Software Windows 10 Pro 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
#17
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.
 
Joined
Jun 30, 2008
Messages
1,135 (0.32/day)
Likes
67
Location
England
System Name Wasleys PC
Processor Intel i5 2400 3.10GHz
Motherboard Asus P8z68-V
Cooling AC Freezer Pro
Memory Kingston 4GB (2x2GB) DDR3 Hyperx Memory
Video Card(s) HIS ATi 6850 1GB DDR3
Storage Seagate ST3500320AS 500GB Hard Drive SATA II 7200rpm *32MB Cache*
Case Antec 900 with mods
Audio Device(s) On Board
Power Supply OCZ Stealth Xtream 500W
Software Windows 7
#18
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

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
21,083 (6.22/day)
Likes
10,212
Location
IA, USA
System Name BY-2015
Processor Intel Core i7-6700K (4 x 4.00 GHz) w/ HT and Turbo on
Motherboard MSI Z170A GAMING M7
Cooling Scythe Kotetsu
Memory 2 x Kingston HyperX DDR4-2133 8 GiB
Video Card(s) PowerColor PCS+ 390 8 GiB DVI + HDMI
Storage Crucial MX300 275 GB, Seagate 6 TB 7200 RPM
Display(s) Samsung SyncMaster T240 24" LCD (1920x1200 HDMI) + Samsung SyncMaster 906BW 19" LCD (1440x900 DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay
Audio Device(s) Realtek Onboard, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse SteelSeries Sensei RAW
Keyboard Tesoro Excalibur
Software Windows 10 Pro 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
#19
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:
Joined
Jun 30, 2008
Messages
1,135 (0.32/day)
Likes
67
Location
England
System Name Wasleys PC
Processor Intel i5 2400 3.10GHz
Motherboard Asus P8z68-V
Cooling AC Freezer Pro
Memory Kingston 4GB (2x2GB) DDR3 Hyperx Memory
Video Card(s) HIS ATi 6850 1GB DDR3
Storage Seagate ST3500320AS 500GB Hard Drive SATA II 7200rpm *32MB Cache*
Case Antec 900 with mods
Audio Device(s) On Board
Power Supply OCZ Stealth Xtream 500W
Software Windows 7
#20
Yeah - Scanner was one of the first things we did. Havent heard of BufferReader until you mentioned it.
 

olithereal

New Member
Joined
May 24, 2008
Messages
1,262 (0.36/day)
Likes
65
System Name Geometro
Processor i5 760 @ Stock (need 1156 brackets for my cooler)
Motherboard MSI P55A-G55
Cooling Kingwin RVT-12025 HDT
Memory 2x4GB Mushkin Blackline Frostbyte 1600 C9-9-9-24
Video Card(s) GTX 275 MSI Twin Frozr OC
Display(s) Samsung 2333SW 23"
Case Lancool PC-K62
Power Supply Corsair VX550W 550W ATX 12V
Software Windows 7 64 bit / Arch Linux x64
#21
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

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
21,083 (6.22/day)
Likes
10,212
Location
IA, USA
System Name BY-2015
Processor Intel Core i7-6700K (4 x 4.00 GHz) w/ HT and Turbo on
Motherboard MSI Z170A GAMING M7
Cooling Scythe Kotetsu
Memory 2 x Kingston HyperX DDR4-2133 8 GiB
Video Card(s) PowerColor PCS+ 390 8 GiB DVI + HDMI
Storage Crucial MX300 275 GB, Seagate 6 TB 7200 RPM
Display(s) Samsung SyncMaster T240 24" LCD (1920x1200 HDMI) + Samsung SyncMaster 906BW 19" LCD (1440x900 DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay
Audio Device(s) Realtek Onboard, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse SteelSeries Sensei RAW
Keyboard Tesoro Excalibur
Software Windows 10 Pro 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
#22
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.
 
Joined
Jan 2, 2008
Messages
3,248 (0.88/day)
Likes
396
System Name Thakk
Processor i7 6700k @ 4.5Ghz
Motherboard Gigabyte G1 Z170N ITX
Cooling Deepcool Maelstrom 120k / H55
Memory 16GB Corsair LPX DDR4 2400
Video Card(s) GTX1080ti Founders Ed. AIO Cooled
Storage Corsair Force GT 120GB SSD / Intel 250GB SSD / Samsung Pro 512 SSD / 3TB Seagate SV32
Display(s) Acer Predator X34 100hz IPS Gsync / HTC Vive
Case Cougar QBX mini itx
Audio Device(s) Realtek ALC1150 > Creative Gigaworks T40 > AKG Q701
Power Supply Thermaltake 600w 140mm
Mouse Logitech G900
Keyboard Ducky Shine TKL MX Blue + Vortex PBT Doubleshots
Software Windows 10 64bit
Benchmark Scores http://www.3dmark.com/fs/12108888
#23
- It counts the amount of spaces/newlines
use String's split(). then get the length. regex for space is "\\s", line is "\n"