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