Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Java Newbie Code Problem

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Java Newbie Code Problem

    Hi,

    I'm just entering my second semester of Java programming so still fairly uncertain about various elements. I have been trying for the past few hours to get this program to compile but it keeps failing. It'd be a great help if one of you fine people could pinpoint the error(s).

    I'm using Textpad and its saying theres problems with my switch statement but I think its theres an error when I'm passing variables from the main method to the method containging the switch.

    The program is supposed to be a simple text based menu system that allows the user to display various maths tables. It is supposed to gather the required info from the user such as whter they want to view mult or division and how many table lines they would like to display etc. then pass the information to a new method that will display the information to the screen.

    Thanks in advance,
    Lee.

    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);
     
    			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;
    				}
     
    			}
     
    			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');
     
    	}
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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.

  3. The Following User Says Thank You to copeg For This Useful Post:

    lee (January 15th, 2010)

  4. #3
    Junior Member
    Join Date
    Jan 2010
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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.


    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;
    		}
     
    	}
     
    	}
    }

  5. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default 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.

    class YourClass {
          public static void main(...){}
          public void someFunc(...){}
    }

  6. The Following User Says Thank You to Freaky Chris For This Useful Post:

    lee (January 15th, 2010)

  7. #5
    Junior Member
    Join Date
    Jan 2010
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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.

    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
     
    }

  8. #6
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Java Newbie Code Problem

    Glad you have got everything working,
    please mark this as solved if that is everything

    Thanks,
    Chris

  9. The Following User Says Thank You to Freaky Chris For This Useful Post:

    lee (January 16th, 2010)

  10. #7
    Junior Member
    Join Date
    Jan 2010
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Java Newbie Code Problem

    Woops, marked as solved now.

    Thanks!

Similar Threads

  1. newbie here
    By salpoe in forum Member Introductions
    Replies: 0
    Last Post: January 15th, 2010, 01:05 AM
  2. newbie needs help with for loop
    By b00036962 in forum Loops & Control Statements
    Replies: 2
    Last Post: January 7th, 2010, 04:57 AM
  3. code needed for the following problem
    By romilc in forum Java Theory & Questions
    Replies: 1
    Last Post: October 11th, 2009, 10:05 AM
  4. Newbie...
    By jchan in forum Member Introductions
    Replies: 2
    Last Post: September 28th, 2009, 02:39 AM
  5. I'm a newbie
    By r12ki in forum Member Introductions
    Replies: 2
    Last Post: June 1st, 2009, 06:38 AM