• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

hi, I need help with a while loop

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I would like your help with a coding solution from my book that I have not been able make work.


 
Marshal
Posts: 15638
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look through the Scanner API documentation carefully, you'll see that the hasNext*() and next*() methods come in pairs: hasNext() + next(), hasNextLine() + nextLine(), hasNextInt() + nextInt() etc. There's a reason for this.  Checking for hasNext() then calling nextLine() immediately after that doesn't really make sense. It's like going to the grocery store and asking if they have oranges and when they say "Yes, we have oranges" you say "Ok, I'll take a bag of apples then!"
 
John D.West
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:If you look through the Scanner API documentation carefully...



Hi,

Could you point me at which lines are wrong ?

thanks.
 
Rancher
Posts: 179
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which lines do you think have mismatched hasNext/next methods?

What is going wrong with the program (what's the expected behavior vs. the actual behavior)?
 
Marshal
Posts: 69494
277
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can see another serious problem: you are using while (input.hasNext()) ... when reading from System.in. System.in always potentially has a “next” token, so you are in effect writing an infinite loop. I you read a file, it always has a last line, so reading a file will always terminate when input.hasNext()) is false.
 
John D.West
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What is going wrong with the program (what's the expected behavior vs. the actual behavior)?



The program is supposed to count the amount of lines, words and characters, store that data and print it to screen.
As it is the program compiles and executes without errors, but the logic is wrong and i don't understand exactly why.
I would like like to know if the problem is a simple fix or is the program is entirely wrong and needs to be rewritten.
 
Junilu Lacar
Marshal
Posts: 15638
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An example of the input and output would be helpful.
 
John D.West
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:An example of the input and output would be helpful.



Any String type input is fine.
The program ouputs nothing,  thats the problem.
 
Junilu Lacar
Marshal
Posts: 15638
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you running the program?

I ran your program as is from the command line (on a Mac) and got results that agree with the wc command output. I did have to redirect input though:

$ java FileAnalysis.java < FileAnalysis.java
42  99  774
$ wc FileAnalysis.java
      42        99         816

You'll notice that 816 - 774 is 42. The difference is because wc includes newline characters in its count.
 
Campbell Ritchie
Marshal
Posts: 69494
277
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest youi start by defining what number of lines, words and characters means. Do the line end characters count? What if there is a blank line in the middle of the file, or as often happens, at its end? What separates words?
 
John D.West
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:How are you running the program...


I am running the program from the NetBeans run program window.
Nice it seems that the program works. What did you do make it run ?
 
John D.West
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I suggest youi start by defining ....


The program is supposed to add to the total count all lines even blank ones, as well as all characters including the space key.
 
Junilu Lacar
Marshal
Posts: 15638
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ran the program exactly as I showed in my last post. That was on the command line, in a terminal window on my Mac. You're going to have a problem running this interactively though since Scanner will block (pause) while it waits from input from the keyboard and it will look like the program hangs or doesn't exit from the while-loop. When input is redirected from a file like how I did it, it won't block.

To get the program to exit when you run it interactively, you'll have to type Ctrl+D (on Linux or Mac) or Ctrl+Z (on Windows) to signal end of input.
 
John D.West
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:I ran the program exactly ....



I did exactly the same as you did, control-z on the windows terminal window and the program still hangs.
 
Campbell Ritchie
Marshal
Posts: 69494
277
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

John D.West wrote:The program is supposed to add to the total count all lines even blank ones, as well as all characters including the space key.

What about line ends? If you are on Windows®, that will make a difference of 2 × n characters where n is the number of line breaks.

Using ctrl‑Z suggests to me that this isn't the best way to use System.in. Such a loop usually works nicely for a file, which implicitly always has a last line.
 
John D.West
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:
Using ctrl‑Z suggests to me that this isn't the best way to use System.in. Such a loop usually works nicely for a file, which implicitly always has a last line.



Hi, the programs output is fine as is because its just an example from my book. I just need help in understanding why it does not run.
 
Junilu Lacar
Marshal
Posts: 15638
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately, we can't see exactly what you're trying to do. The program does run and it appears to output the correct results. The only thing we don't know is what exactly you're doing. One little thing that you're not telling us could make the difference so try to describe exactly (keystroke for keystroke) what you're doing to run this program.
 
Junilu Lacar
Marshal
Posts: 15638
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For example, when you say Ctrl+Z doesn't work for you, what are you doing? Are you pressing the Ctrl key, releasing it, then pressing Z? Because that won't work. You have to press the Ctrl key, keep pressing it, then press the Z key.
 
Campbell Ritchie
Marshal
Posts: 69494
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But it does run.
 
John D.West
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I found the problem. All I needed to do was to press the enter key after control-z and it worked.

 
No, tomorrow we rule the world! With this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic