I didn't put the calculate method in the main class because it needs the input from the user to work. Take a look at the code.
//Need to import Keyboard class here.
//Did it come from your book or something?
import....
class Mathtab{
public static void main(String[]args)
{
input();
calculation();
}
////////////////////////////////////////
// Gathering Data from user
///////////////////////////////////////
static void input()
{
int type, lines;
char table;
//Enter the table eg 5 times table 9 Addition table etc
System.out.print("What maths Table would you like? : ");
type = Keyboard.readInt();
System.out.print("\nHow many lines of the table would you like? :");
lines = Keyboard.readInt();
System.out.print("\nSelect a Table Type (a)Multiplication (b)Division (c)Addition (d)Subtraction: ");
Keyboard.skipLine();
table = Keyboard.readChar();
table = Character.toUpperCase(table);
}
////////////////////////////////////////
// Calculation of Data Gathered
///////////////////////////////////////
static void calculation(int maths, int lines, char type)
{
int total;
System.out.println("Multiplication Table");
int i;
switch(type){
case 'a': for(i = 1; i <= lines; i++){
total = maths * i;
System.out.println(maths + "*" + i + "=" + total);
}
break;
case 'b': for(i = 1; i <= lines; i++){
total = maths / i;
System.out.println(maths + "/" + i + "=" + total);
}
break;
case 'c': for(i = 1; i <= lines; i++){
total = maths + i;
System.out.println(maths + "+" + i + "=" + total);
}
break;
case 'd': for(i = 1; i <= lines; i++){
total = maths - i;
System.out.println(maths + "-" + i + "=" + total);
}
}
}
}
I moved the calculation method back out to main. So imagine that the input() method just ran and you are about to run the calculation() method. The main method doesn't know anything about the what the user entered because nothing was returned from the input method. There is no way to let the calculation know how many lines, what kind of operation to perform and what not.
To fix this, you have 2 options. First would be to return the user input from the input() method back to main. This is possible but complicated due to the fact that a) you have multiple variables to return and b) the variables are of different types. Java only allows one return value. To return multiple values, you would need to return an Array. But arrays can only handle data of on type. To handle the two types that you have (ints and a char) you would need to use a List, Vector.....something along those lines. Now these collections store objects as exactly that....generic Objects. So when you pull them out, you have to type cast them which can cause exceptions and blah blah blah....see what I mean about complicated?
Your second option would be to call the calculation method from inside the input method like I wrote. Look at the code again.
//Need to import Keyboard class here.
//Did it come from your book or something?
import....
class Mathtab{
public static void main(String[]args)
{
input();
}
////////////////////////////////////////
// Gathering Data from user
///////////////////////////////////////
static void input()
{
int type, lines;
char table;
//Enter the table eg 5 times table 9 Addition table etc
System.out.print("What maths Table would you like? : ");
type = Keyboard.readInt();
System.out.print("\nHow many lines of the table would you like? :");
lines = Keyboard.readInt();
System.out.print("\nSelect a Table Type (a)Multiplication (b)Division (c)Addition (d)Subtraction: ");
Keyboard.skipLine();
table = Keyboard.readChar();
table = Character.toUpperCase(table);
calculation(type, lines, table);
}
////////////////////////////////////////
// Calculation of Data Gathered
///////////////////////////////////////
static void calculation(int maths, int lines, char type)
{
int total;
System.out.println("Multiplication Table");
int i;
switch(type){
case 'a': for(i = 1; i <= lines; i++){
total = maths * i;
System.out.println(maths + "*" + i + "=" + total);
}
break;
case 'b': for(i = 1; i <= lines; i++){
total = maths / i;
System.out.println(maths + "/" + i + "=" + total);
}
break;
case 'c': for(i = 1; i <= lines; i++){
total = maths + i;
System.out.println(maths + "+" + i + "=" + total);
}
break;
case 'd': for(i = 1; i <= lines; i++){
total = maths - i;
System.out.println(maths + "-" + i + "=" + total);
}
}
}
}
Look at the end of the input() method just before calculation is called(). You just obtained all the input from the user and it is right there in that method. So why not just call the calculation() method and pass it that data directly? This saves a bunch of work.
Ok, now that I think about it there is a third option that is also easier than the first. You could set up local fields in the Mathtab class that correspond to your input. The input class would then set these and then the calculation method could use them. That would look something like this:
//Need to import Keyboard class here.
//Did it come from your book or something?
import....
class Mathtab{
int type;
int lines;
char table;
//Need a constructor
public Mathtab(){
}
public static void main(String[]args)
{
Mathtabl foo = new Mathtab();
foo.input();
foo.calculation();
}
////////////////////////////////////////
// Gathering Data from user
///////////////////////////////////////
static void input()
{
Don't need these anymore.
//int type, lines;
//char table;
//Enter the table eg 5 times table 9 Addition table etc
System.out.print("What this.type Table would you like? : ");
this.type = Keyboard.readInt();
System.out.print("\nHow many lines of the table would you like? :");
this.lines = Keyboard.readInt(); //this sets the filed declared above for this particular object.
System.out.print("\nSelect a Table Type (a)Multiplication (b)Division (c)Addition (d)Subtraction: ");
Keyboard.skipLine();
this.table = Keyboard.readChar();
this.table = Character.toUpperCase(table);
}
////////////////////////////////////////
// Calculation of Data Gathered
///////////////////////////////////////
static void calculation(int this.type, int lines, char type)
{
int total;
System.out.println("Multiplication Table");
int i;
switch(this.table){
case 'a': for(i = 1; i <= this.lines; i++){
total = this.type * i;
System.out.println(this.type + "*" + i + "=" + total);
}
break;
case 'b': for(i = 1; i <= this.lines; i++){
total = this.type / i;
System.out.println(this.type + "/" + i + "=" + total);
}
break;
case 'c': for(i = 1; i <= this.lines; i++){
total = this.type + i;
System.out.println(this.type + "+" + i + "=" + total);
}
break;
case 'd': for(i = 1; i <= this.lines; i++){
total = this.type - i;
System.out.println(this.type + "-" + i + "=" + total);
}
}
}
}
Again, I didn't test any of these but the concepts are accurate.
As far as not getting output, are you sure you moved the calculation method? I think it might be because you are changing the char to upper case. I didn't notice that before. Try changing to case('x') statements to case('X').
Good luck dude.
-Shoe
Originally Posted by ad0m
Hi ShoeNinja
Thank you so much was very helpful
I hope it is ok if I can geta few more bits of info off you.
IE
The Method 'input();' is in the "public static void manin(String[] args)"
how come I don't put "calculation" there aswell.. ?
I have the Keyboard.class file int he same directory hence why I dont have to import it.
But I am getting No out put after I select the table i want eg Multiplication or Division "a for *" or "b for /" ect ??
Thanks again so much !
Kindest regards
Adam