common problem...can't find answer :(

S

Shanknbake

so here's my issue: my program compiles and runs fine...I have to files
....implementation and driver....very simple.....I have the following:
{...
System.out.println("Would you like to see the current totals?");
answer=keyboard.nextLine();
....}

The compiler does not give me any errors...and it Prints the question
"would you like..." but does not wait for a responce from the user.
Why? and more importantly...how do i fix it?

if you need more code let me know

thanks in advance :)
 
O

Oliver Wong

Shanknbake said:
so here's my issue: my program compiles and runs fine...I have to files
...implementation and driver....very simple.....I have the following:
{...
System.out.println("Would you like to see the current totals?");
answer=keyboard.nextLine();
...}

The compiler does not give me any errors...and it Prints the question
"would you like..." but does not wait for a responce from the user.
Why? and more importantly...how do i fix it?

if you need more code let me know

I need more code. But if you give me too much code, I will get
discouraged and not bother reading it. For a guide on figuring out what a
good amount of code to post, see http://www.physci.org/codes/sscce.jsp

- Oliver
 
R

Roedy Green

System.out.println("Would you like to see the current totals?");
answer=keyboard.nextLine();

Think! How could anyone possibly diagnose your program seeing only one
line of your program which is not even where the problem lies?

Even Superman can't solve problems like that.
 
M

Michael Dunn

perhaps something is left over from a previous read()

try adding the indicated line

{...
System.in.skip(System.in.available()); //<----------------
System.out.println("Would you like to see the current totals?");
answer=keyboard.nextLine();
....}
 
S

Shanknbake

nope Michael Dunn, that didn't do the trick...As for providing more
code....I thought that my issue was very common among beginners
.....here is more code:

public void readInput()
{

System.out.println("Enter length of wood: ");
length=keyboard.nextDouble();
}

public void writeOutput()
{
System.out.println("Inital Lengh of wood: "+length+" m.");
System.out.println("Number of 5 m. lengths:
"+lengths.format(fiveCalc())+" m.");
System.out.println("Number of 2 m.
lengths: "+lengths.format(twoCalc())+" m.");
System.out.println("Extra leftover wood: "+lengths.format(extra())+"
m.");
}

public void writeTotals()
{


System.out.println("Would you like to see the current totals?");
answer=keyboard.nextLine();

if(answer.equalsIgnoreCase("y")){

System.out.println("Total 5 m.
lengths: "+lengths.format(getTotalFive())+" m.");
System.out.println("Total 2 m.
lengths: "+lengths.format(getTotalTwo())+" m.");
System.out.println("Total leftover
lengths: "+lengths.format(getTotalExtra())+" m.");}

else{}
}
*************************************************************************************************
*************************************************************************************************
the three methods are called in the order in which they appear here
from the driver file.
Once again, my problem lies in the fact that once I ask the question
"would you like to see the current totals?", the compiler skips the
responce and moves on to re-loop to the first method again (or ends the
program).
if more code is needed let me know

thanks again and sorry for this is not a strong subject for me.
 
S

Shanknbake

I rememeber that when I had this problem in C++, I would use a
cin.ignore(); but I dont know if that is the problem I am having here
 
O

Oliver Wong

Shanknbake said:
nope Michael Dunn, that didn't do the trick...As for providing more
code....I thought that my issue was very common among beginners
....here is more code:

Console I/O is a very common problem among beginners. Unfortunately,
different people do Console I/O in all sorts of different ways. Until we
know what way YOU'RE doing it, we can't give you advice on your particular
problem. If you just want a program to manage logging (as in trees)
inventory, someone could write one for you, but you'd probably want to learn
how to do it yourself.

[snip]
answer=keyboard.nextLine();

The issue is that we don't know what the type of "keyboard" is. And if
it's not a type from Sun's library, we don't know its implementation either.
Did you actually read the page on SSCCE? Here's the link again:

http://www.physci.org/codes/sscce.jsp

The code you posted is certainly not "self-contained", because there is
no information about the type of "keyboard".
thanks again and sorry for this is not a strong subject for me.

You'll get it eventually.

- Oliver
 
O

Oliver Wong

Roedy Green said:
Think! How could anyone possibly diagnose your program seeing only one
line of your program which is not even where the problem lies?

Even Superman can't solve problems like that.

In the OP's defense, perhaps (s)he does not know whether or not
"keyboard" is a keyword in the Java language which has fixed semantics that
us "pros" would know about.

- Oliver
 
B

Bjorn Abelli

...

public void writeTotals()
{
System.out.println("Would you like to see the current totals?");
answer=keyboard.nextLine();

if(answer.equalsIgnoreCase("y")){

System.out.println("Total 5 m.
lengths: "+lengths.format(getTotalFive())+" m.");
System.out.println("Total 2 m.
lengths: "+lengths.format(getTotalTwo())+" m.");
System.out.println("Total leftover
lengths: "+lengths.format(getTotalExtra())+" m.");}

else{}
}
********************************************************
********************************************************
the three methods are called in the order in which they appear here
from the driver file.

Where's the code for those calls?

What class is "keyboard" instantiated from?

Is it "Scanner"? Then the question is, how was it instantiated?

Scanner keyboard = new Scanner(System.in);

?
Once again, my problem lies in the fact that once
I ask the question "would you like to see the current
totals?", the compiler skips the responce and moves
on to re-loop to the first method again (or ends the
program).

I don't believe the "compiler" skipped anything...

If that would be the case (that it wasn't even compiled), you wouldn't be
able to even start the program...

A start would be for you to put another line into the method writeTotals:

public void writeTotals()
{
System.out.println("Would you like to see the current totals?");
answer=keyboard.nextLine();
System.out.println(answer); // To see what it got...
...

}

// Bjorn A
 
S

Shanknbake

okay...hehe lets try this again here is my program in FULL (as it is a
very small and simple program i will give all the code in full)
*****************************************************************************************************
implementation file
*****************************************************************************************************
import java.util.*;
import java.text.*;

class lengthOfWood
{
public double length,leftOver;
public int five,two,totalFive=0,totalTwo=0;
public double extra,totalExtra=0;
public String answer;

DecimalFormat lengths = new DecimalFormat("0.0");
Scanner keyboard = new Scanner (System.in);
public void readInput()
{

System.out.println("Enter length of wood: ");
length=keyboard.nextDouble();
}

public void writeOutput()
{
System.out.println("Inital Lengh of wood: "+length+" m.");
System.out.println("Number of 5 m. lengths:
"+lengths.format(fiveCalc())+" m.");
System.out.println("Number of 2 m.
lengths: "+lengths.format(twoCalc())+" m.");
System.out.println("Extra leftover wood: "+lengths.format(extra())+"
m.");
}

public void writeTotals()
{


System.out.println("Would you like to see the current totals?");
answer=keyboard.nextLine();

if(answer.equalsIgnoreCase("y")){

System.out.println("Total 5 m.
lengths: "+lengths.format(getTotalFive())+" m.");
System.out.println("Total 2 m.
lengths: "+lengths.format(getTotalTwo())+" m.");
System.out.println("Total leftover
lengths: "+lengths.format(getTotalExtra())+" m.");}

else{}
}

public int fiveCalc()
{
five = (int)length / 5;
leftOver = length - (five*5);
totalFive+=five;
return five;

}

public int twoCalc()
{
two = (int)leftOver / 2;
leftOver = leftOver - (two*2);
totalTwo+=two;
return two;
}

public double extra()
{
extra = leftOver;
totalExtra+=extra;
return extra;
}

public int getTotalFive()
{
return totalFive;
}

public int getTotalTwo()
{
return totalTwo;
}

public double getTotalExtra()
{
return totalExtra;
}
}
*****************************************************************************************************
driver file
*****************************************************************************************************
import java.util.*;

class driver
{
public static void main(String [] args)
{
Scanner keyboard = new Scanner (System.in);
int trys,copyTrys=1;
System.out.print("How many lengths of wood do you have?: ");
trys = keyboard.nextInt();
do{
lengthOfWood i = new lengthOfWood();
i.readInput();
i.writeOutput();
i.writeTotals();
copyTrys++;}
while(copyTrys!=(trys+1));
}
}
**************************************************************************************************
hopefully posting my entire program was not an overkill but based on
SSCCE...it needed to be compileable...and there was no way of easily
making my "problem" compilable w/out possibly changing things that
would cause another problem or temporarily 'fix' the problem i have
now...so ya.....

round 3 DING DING
 
O

Oliver Wong

Shanknbake said:
okay...hehe lets try this again here is my program in FULL (as it is a
very small and simple program i will give all the code in full) [code snipped]
hopefully posting my entire program was not an overkill but based on
SSCCE...it needed to be compileable...and there was no way of easily
making my "problem" compilable w/out possibly changing things that
would cause another problem or temporarily 'fix' the problem i have
now...so ya.....

If you "temporarily fix" the problem you had, why don't you look at what
change you did, and try to think about why it fixed the problem. Maybe
that'll lead you to a solution.

Anyway, here's a temporary fix for you: Change the line that reads

<oldCode>
answer = keyboard.nextLine();
</oldCode>

to

<newCode>
answer = keyboard.nextLine();
answer = keyboard.nextLine();
</newCode>

To understand why this works, think about what the order of calls on the
keyboard object are. First its nextInt(), then nextDouble(), then
nextLine(), then nextDouble(), then nextLine(), and so on.

When the program asks me how many pieces of wood, I'll type in, for example
"1" followed by the enter key.

nextInt() eats up the 1, but the newline is still in the buffer.

Then when it asks for the length of the wood, I type in, for example "10"
followed by the enter key.

nextDouble() skips the whitespace of the newline that's in the buffer, then
sees the 10, and eats that up. But there's the second newline still in the
buffer.

then when you call nextLine(), it eats up the newline in the buffer and
returns immediately.

You need another call to nextLine() to wait so that the console will pause
and wait for the user, for example, to type in "y" followed by enter.

Of course, just putting two nextLine() calls one after the other is a
horrible way to fix this. Now that you have an understanding of what the
problem is, maybe you can figure out a "proper" way to fix it.

- Oliver
 
R

Rhino

Shanknbake said:
okay...hehe lets try this again here is my program in FULL (as it is a
very small and simple program i will give all the code in full)
****************************************************************************
*************************
implementation file
****************************************************************************
*************************
import java.util.*;
import java.text.*;

class lengthOfWood
{
public double length,leftOver;
public int five,two,totalFive=0,totalTwo=0;
public double extra,totalExtra=0;
public String answer;

DecimalFormat lengths = new DecimalFormat("0.0");
Scanner keyboard = new Scanner (System.in);
public void readInput()
{

System.out.println("Enter length of wood: ");
length=keyboard.nextDouble();
}

public void writeOutput()
{
System.out.println("Inital Lengh of wood: "+length+" m.");
System.out.println("Number of 5 m. lengths:
"+lengths.format(fiveCalc())+" m.");
System.out.println("Number of 2 m.
lengths: "+lengths.format(twoCalc())+" m.");
System.out.println("Extra leftover wood: "+lengths.format(extra())+"
m.");
}

public void writeTotals()
{


System.out.println("Would you like to see the current totals?");
answer=keyboard.nextLine();

if(answer.equalsIgnoreCase("y")){

System.out.println("Total 5 m.
lengths: "+lengths.format(getTotalFive())+" m.");
System.out.println("Total 2 m.
lengths: "+lengths.format(getTotalTwo())+" m.");
System.out.println("Total leftover
lengths: "+lengths.format(getTotalExtra())+" m.");}

else{}
}

public int fiveCalc()
{
five = (int)length / 5;
leftOver = length - (five*5);
totalFive+=five;
return five;

}

public int twoCalc()
{
two = (int)leftOver / 2;
leftOver = leftOver - (two*2);
totalTwo+=two;
return two;
}

public double extra()
{
extra = leftOver;
totalExtra+=extra;
return extra;
}

public int getTotalFive()
{
return totalFive;
}

public int getTotalTwo()
{
return totalTwo;
}

public double getTotalExtra()
{
return totalExtra;
}
}
****************************************************************************
*************************
driver file
****************************************************************************
*************************
import java.util.*;

class driver
{
public static void main(String [] args)
{
Scanner keyboard = new Scanner (System.in);
int trys,copyTrys=1;
System.out.print("How many lengths of wood do you have?: ");
trys = keyboard.nextInt();
do{
lengthOfWood i = new lengthOfWood();
i.readInput();
i.writeOutput();
i.writeTotals();
copyTrys++;}
while(copyTrys!=(trys+1));
}
}
****************************************************************************
**********************
hopefully posting my entire program was not an overkill but based on
SSCCE...it needed to be compileable...and there was no way of easily
making my "problem" compilable w/out possibly changing things that
would cause another problem or temporarily 'fix' the problem i have
now...so ya.....

round 3 DING DING
The problem with "round 2" was that 'keyboard' wasn't defined. You dealt
with that in "round 3" by showing us how 'keyboard' was defined but it is
based on a class called Scanner which you haven't shown us. Therefore, we
don't know what the Scanner class is - it's not part of the standard Java
API - so we still can't help much. I assume that someone, a teacher
probably, gave you the Scanner class to use to simplify the coding of your
read logic.

Rather than asking you to provide the source code for Scanner, which may use
non-standard classes too for all I know, I'm going to suggest a fairly
straightforward way to do screen I/O that I use in my programs. If you use
this technique, you should be "good to go". In addition, you will be using
standard classes that anyone with Java experience will recognize. That's a
lot easier for *ME* than trying to debug the mysterious Scanner class
without its source code.

So here's how I do console I/O. I'm not saying this is the best or only way
to do what you want but it should get the job done.

BufferedReader rdr = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter your first name:");
String firstName = rdr.readLine();

Here's a brief explanation. 'rdr' is a BufferedReader; it is wrapped around
an InputStreamReader which is reading 'System.in' which is the keyboard
input. (For a proper discussion of Streams and Readers, take a look at the
Java Tutorial chapter; here is a link to the appropriate chapter:
http://java.sun.com/docs/books/tutorial/essential/io/index.html.) You only
need to create 'rdr' once, not before each line is read from the keyboard.

The System.out.println("") simply displays a prompt that tells you what
input you are supposed to give.

The third line reads a line from the console and stores it, as a String, in
the variable 'firstName'. Anything read via a BufferedReader is always a
String, even if it is only digits. (If you are inputting a number that needs
to be treated as a number, there are standard classes to convert it to the
appropriate number format.)

All you have to remember is that the user needs to type his response to the
prompt and press ENTER before the program can see the response.

I hope this helps you and doesn't get you into trouble with your teacher for
not using the Scanner class.

Rhino
 
O

Oliver Wong

The problem with "round 2" was that 'keyboard' wasn't defined. You dealt
with that in "round 3" by showing us how 'keyboard' was defined but it is
based on a class called Scanner which you haven't shown us. Therefore, we
don't know what the Scanner class is - it's not part of the standard Java
API - so we still can't help much. I assume that someone, a teacher
probably, gave you the Scanner class to use to simplify the coding of your
read logic.

Actually, java.util.Scanner was added in Java 1.5.

[snip]
So here's how I do console I/O. I'm not saying this is the best or only
way
to do what you want but it should get the job done.

BufferedReader rdr = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter your first name:");
String firstName = rdr.readLine();

I agree that Rhino's implementation here is probably more robust than
using the Scanner class with intermittent nextLine() calls. Alternatively,
use nextLine() consistently in the Scanner class.

- Oliver
 
B

Bjorn Abelli

...


I agree that Rhino's implementation here is probably more robust than
using the Scanner class with intermittent nextLine() calls. Alternatively,
use nextLine() consistently in the Scanner class.

As Oliver wrote in another post in this thread, you need to know what
Scanner actually does, and what to expect from it. It's *not* a simplified
"Reader-class" but can be used as such if used properly.

He also suggested a workaround in that post.

Another workaround could be to not only be consequent by "eating" the
newlines, but to start with a simpler use of the Scanner instance, by *only*
using nextLine, e.g. changing

keyboard.nextInt()

into

Integer.parseInt ( keyboard.nextLine() )

and

keyboard.nextDouble()

into

Double.parseDouble ( keyboard.nextLine() )

// Bjorn A
 
S

Shanknbake

wow such great responces!!! thanks guys!!! As far as understanding the
Scanner class...i don't think that I will attempt to look at it right
now, as it might confuse me to a point of suicide...and that wouldn't
solve n e thing :p But perhaps in a semester or 2 I'll be at that
level :) thanks again guys

-Kevin
 
J

Julian McMaster

Roedy Green said:
Think! How could anyone possibly diagnose your program seeing only one
line of your program which is not even where the problem lies?

Even Superman can't solve problems like that.

How dare you insult Superman. At least he's not like Batman...that guy is a
..NET junkie.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top