Re: Java Newbie Code Problem
Your function display() is nested inside the main() function, which is a syntax error not allowed in the language. You should copy and paste the display function outside of the main function.
Re: Java Newbie Code Problem
Thank you for the prompt reply. I'm not sure if this is what you mean ? The below code still gives me the same compile errors.
Code :
class assignment2 {
public static void main (String[]args)
{
int table, lines;
char type, ans;
do{
System.out.println("Please enter the table number you wish to view; ");
table = Keyboard.readInt();
while (table > 12 || table < 1 )
{
System.out.println("Please enter a number between 1 and 12 inclusive: ");
System.out.println("Please enter the table number you wish to view; ");
table = Keyboard.readInt();
}
System.out.println("Please enter the number of lines you wish to view; ");
lines = Keyboard.readInt();
while (lines > 12 || lines < 1)
{
System.out.println("Please enter a number between 1 and 12 inclusive: ");
System.out.println("Please enter the number of lines you wish to view; ");
lines = Keyboard.readInt();
}
System.out.println("Please choose a table type");
System.out.println("(a) Multiplication (b) Division (c) Modular (d) Quit table");
type = Keyboard.readChar();
while (type != 'a' && type != 'b' && type != 'c' && type != 'd')
{
System.out.println("Please choose from the selected options");
System.out.println("Please choose a table type");
System.out.println("(a) Multiplication (b) Division (c) Modular (d) Quit table");
type = Keyboard.readChar();
}
display (table, lines, type);
System.out.println("Would you like to view another table ? 'y' or 'n': ");
ans = Keyboard.readChar();
while(ans != 'n' && ans != 'y')
{
System.out.println("Please choose either 'y' or 'n'");
System.out.println("Would you like to view another table? 'y' or 'n'");
ans = Keyboard.readChar();
}
}while (ans == 'y');
public static void display (int tablenumber,int numberlines,char tabletype)
{
int x = 0;
int y;
switch (tabletype)
{
case 'a': case 'A': for (x = 1; x <= numberlines; x++)
{
y = tablenumber * x;
System.out.println(tablenumber + " * " + x + " = " + y);
}break;
case 'b': case 'B': for (x = 1; x <= numberlines; x++)
{
y = tablenumber / x;
System.out.println(tablenumber + " / " + x + " = " + y);
}break;
case 'c': case 'C': for (x = 1; x <= numberlines; x++)
{
y = tablenumber / x;
System.out.println(tablenumber + " % " + x + " = " + y);
}break;
case 'd': case 'D':
System.out.println("Thank you");
break;
}
}
}
}
Re: Java Newbie Code Problem
It is still inside your main method, remove it from the main method altogether and just keep it as part of the class.
Code :
class YourClass {
public static void main(...){}
public void someFunc(...){}
}
Re: Java Newbie Code Problem
Ah that worked a treat. I had a problem with two brackets. This stuff still confuses the hell outta me!
Thanks alot for the help. Really appreciated!
Here's the revised, working code.
Code :
class assignment2 {
public static void main (String[]args)
{
int table, lines;
char type, ans;
do{
System.out.println("Please enter the table number you wish to view; ");
table = Keyboard.readInt();
while (table > 12 || table < 1 )
{
System.out.println("Please enter a number between 1 and 12 inclusive: ");
System.out.println("Please enter the table number you wish to view; ");
table = Keyboard.readInt();
}
System.out.println("Please enter the number of lines you wish to view; ");
lines = Keyboard.readInt();
while (lines > 12 || lines < 1)
{
System.out.println("Please enter a number between 1 and 12 inclusive: ");
System.out.println("Please enter the number of lines you wish to view; ");
lines = Keyboard.readInt();
}
System.out.println("Please choose a table type");
System.out.println("(a) Multiplication (b) Division (c) Modular (d) Quit table");
type = Keyboard.readChar();
while (type != 'a' && type != 'b' && type != 'c' && type != 'd')
{
System.out.println("Please choose from the selected options");
System.out.println("Please choose a table type");
System.out.println("(a) Multiplication (b) Division (c) Modular (d) Quit table");
type = Keyboard.readChar();
}
display (table, lines, type);
System.out.println("Would you like to view another table ? 'y' or 'n': ");
ans = Keyboard.readChar();
while(ans != 'n' && ans != 'y')
{
System.out.println("Please choose either 'y' or 'n'");
System.out.println("Would you like to view another table? 'y' or 'n'");
ans = Keyboard.readChar();
}
}while (ans == 'y');
} // inserted a curly brace here
public static void display (int tablenumber,int numberlines,char tabletype)
{
int x = 0;
int y;
switch (tabletype)
{
case 'a': case 'A': for (x = 1; x <= numberlines; x++)
{
y = tablenumber * x;
System.out.println(tablenumber + " * " + x + " = " + y);
}break;
case 'b': case 'B': for (x = 1; x <= numberlines; x++)
{
y = tablenumber / x;
System.out.println(tablenumber + " / " + x + " = " + y);
}break;
case 'c': case 'C': for (x = 1; x <= numberlines; x++)
{
y = tablenumber / x;
System.out.println(tablenumber + " % " + x + " = " + y);
}break;
case 'd': case 'D':
System.out.println("Thank you");
break;
}
} //removed a curly brace from here
}
Re: Java Newbie Code Problem
Glad you have got everything working,
please mark this as solved if that is everything :D
Thanks,
Chris
Re: Java Newbie Code Problem
Woops, marked as solved now.
Thanks!