#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    154
    Rep Power
    14

    Error Messages when compiling program


    Can anyone please provide me with some pointers as to what the following error messages mean. I have tried to fix what I believe each problem is, but I don't see anything wrong with my program.

    Error Messages:

    Found 6 syntax errors in "SheepBank.java":

    167. {
    ^^

    *** Syntax: "}" inserted to complete MethodBody



    184. System.out.println();
    <----->

    *** Syntax: Identifier expected after this token



    187. Flossy.inputdata();
    <------->

    *** Syntax: Identifier expected after this token



    189. System.out.print("Enter the total (in lambs): ");
    <>

    *** Syntax: misplaced construct(s)



    189. System.out.print("Enter the total (in lambs): ");
    <-------------------------->

    *** Syntax: Unexpected symbol ignored



    190. String input = console.readLine();
    <>

    *** Syntax: { expected after this token

    Thanks in advance.
  2. #2
  3. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    We can't help unless you post all your code.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    154
    Rep Power
    14
    Here is partial code which I am using:

    import java.io.*;

    class SheepBank
    {


    // ****** Attributes and most Methods of Class Obmitted *****

    public void inputdata () throws IOException // reads in Coins

    {

    BufferedReader console = new BufferedReader
    (new InputStreamReader(System.in));

    System.out.println("Welcome to Flossy's Sheep Bank Program!");
    System.out.println("<><><><><><><><><><><><><><><><><><><><><><><>");
    System.out.println();
    System.out.println("Enter the current contents of the Sheepy Bank:");
    System.out.println();
    System.out.print("Enter the number of lambs: ");
    String input = console.readLine();
    lamb = Integer.parseInt(input);

    // Previous 3 lines repeated for different coin denominations

    return;

    }

    // output

    public void outputdata () // displays details in neat line
    {

    System.out.println("Lambs: " + lamb);
    System.out.println("Bleats: " + "\t" + bleat);
    System.out.println("Frolics: " + frolic);
    System.out.println("Rams: " + ram);
    System.out.println("Total: " + getTotal());

    return;

    }

    }

    class SheepBankDriver // Program which uses SheepBank Class
    {

    public static void main (String[] args) throws IOException

    { // line 167

    int menu() throws IOException;

    // Variables Declaration and Initialisation

    int tempi;

    boolean finished = false;

    BufferedReader console = new BufferedReader
    (new InputStreamReader(System.in));

    // Object Initialisation

    SheepBank Flossy = new SheepBank ();

    System.out.println(); // line 184

    // Input for Flossy
    Flossy.inputdata(); // line 187

    System.out.print("Enter the total (in lambs): "); // line 189
    String input = console.readLine(); // line 190
    tempi = Integer.parseInt(input);

    Due to revealing such fragmented code, I have added comments to show the large sector of code which I have obmitted and also to show the lines which the error messages shown from the previous post correspond to.

    Hope this is enough to find out the problem(s)!
  6. #4
  7. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    I don't think you understand - your code fragments are not helpful. When you have syntax errors, it can occur on the line it is saying the error is on OR it can happen in another place. The line number given is only when the compiler first notices there is a problem. The real problem can be well before the specified line in some cases. I can't help you if you don't post all the code, intact, exactly as you are trying to use it.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    154
    Rep Power
    14
    I have included the code as a .java file (javaprog.java) attachment.
    Attached Files
  10. #6
  11. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    OK, you must be from the C world. First, you have declared a method in the beginning of your main, as you would a variable. You do not need to declare methods before defining them as you do in C.

    Unlike in C, you can not return a value in the main method. The method signature is
    Code:
    public static void main(String[])
    this states that there is no return (void).

    Thirdly, the menu() method is called from the main method. The main method is a static method. That means it is a Class level method. Class level methods cannot call instance level methods directly. For your code to work you will need to make menu() static also.

    And lastly as a side note, you do not need to nor is it desired to use the "return" keyword at the end of methods when you are not returning anything, but it does not hurt anything to do so.
    Code:
    import java.io.*;
    class TigerBank {
    	private int coin1;
    	private int coin2;
    	private int coin3;
    	private int coin4;
    	private int total;
    
    	public TigerBank() // Default consructor, no defaults
    	{
    		return;
    	}
    	public TigerBank(int o, int tw, int th, int f) // with defaults
    	{
    		coin1 = o;
    		coin2 = tw;
    		coin3 = th;
    		coin4 = f;
    		return;
    	}
    	public int getCoin1() {
    		return coin1;
    	}
    	public int getCoin2() {
    		return coin2;
    	}
    	public int getCoin3() {
    		return coin3;
    	}
    	public int getCoin4() {
    		return coin4;
    	}
    	public int getTotal() {
    		total = coin1 + (coin2 * 5) + (coin3 * 25) + (coin4 * 50);
    		return total;
    	}
    	public void setCoin1(int newCoin1) {
    		coin1 += newCoin1;
    		return;
    	}
    	public void setCoin2(int newCoin2) {
    		coin2 += newCoin2;
    		return;
    	}
    	public void setCoin3(int newCoin3) {
    		coin3 += newCoin3;
    		return;
    	}
    	public void setCoin4(int newCoin4) {
    		coin4 += newCoin4;
    		return;
    	}
    	public void inputdata() throws IOException // reads Coin details
    	{
    		BufferedReader console =
    			new BufferedReader(new InputStreamReader(System.in));
    		System.out.println("Welcome to Sharma's Tiger Bank Program!");
    		System.out.println("<><><><><><><><><><><><><><><><><><><><><><><>");
    		System.out.println();
    		System.out.println("Enter the current contents of Sharma's Bank:");
    		System.out.println();
    		System.out.print("Enter the number of Coin1: ");
    		String input = console.readLine();
    		coin1 = Integer.parseInt(input);
    		System.out.print("Enter the number of Coin2: ");
    		input = console.readLine();
    		coin2 = Integer.parseInt(input);
    		System.out.print("Enter the number of Coin3: ");
    		input = console.readLine();
    		coin3 = Integer.parseInt(input);
    		System.out.print("Enter the number of Coin4: ");
    		input = console.readLine();
    		coin4 = Integer.parseInt(input);
    		return;
    	}
    	public void outputdata() // displays details in neat line
    	{
    		System.out.println("Coin1:              " + coin1);
    		System.out.println("Coin2:     " + "\t" + coin2);
    		System.out.println("Coin3:    	" + coin3);
    		System.out.println("Coin4:               " + coin4);
    		System.out.println("Total:              " + getTotal());
    		return;
    	}
    }
    class TigerBankDriver {
    	public static void main(String[] args) throws IOException {
    		//int menu() throws IOException;
    		int tempi;
    		boolean finished = false;
    		BufferedReader console =
    			new BufferedReader(new InputStreamReader(System.in));
    		TigerBank Sharma = new TigerBank();
    		System.out.println();
    		Sharma.inputdata();
    		System.out.print("Enter the total (in terms of coin1): ");
    		String input = console.readLine();
    		tempi = Integer.parseInt(input);
    		if (tempi != Sharma.getTotal()) {
    			System.out.println();
    			System.out.println(
    				"Error! Total amount entered does not match coins in bank!");
    			System.exit(0);
    		}
    		System.out.println();
    		System.out.println("The Tiger Bank currently contains: ");
    		System.out.println();
    		Sharma.outputdata();
    		do {
    			// Display Tiger Bank Menu
    			int choice = menu();
    			// Switch Options
    			switch (choice) {
    				case 1 :
    					System.out.print("Enter the number of coin1: ");
    					input = console.readLine();
    					tempi = Integer.parseInt(input);
    					Sharma.setCoin1(tempi);
    					break;
    				case 2 :
    					System.out.print("Enter the number of coin2: ");
    					input = console.readLine();
    					tempi = Integer.parseInt(input);
    					Sharma.setCoin2(tempi);
    					break;
    				case 3 :
    					System.out.print("Enter the number of coin3: ");
    					input = console.readLine();
    					tempi = Integer.parseInt(input);
    					Sharma.setCoin3(tempi);
    					break;
    				case 4 :
    					System.out.print("Enter the number of coin4: ");
    					input = console.readLine();
    					tempi = Integer.parseInt(input);
    					Sharma.setCoin4(tempi);
    					break;
    				case 5 :
    					if (Sharma.getCoin1() != 1) {
    						System.out.println(
    							"There are " + Sharma.getCoin1() + " coin1s in the Bank.");
    					}
    					else {
    						System.out.println(
    							"There is " + Sharma.getCoin1() + " coin1 in the Bank.");
    					}
    					break;
    				case 6 :
    					if (Sharma.getCoin2() != 1) {
    						System.out.println(
    							"There are " + Sharma.getCoin2() + " coin2s in the Bank.");
    					}
    					else {
    						System.out.println(
    							"There is " + Sharma.getCoin2() + " coin2 in the Bank.");
    					}
    					break;
    				case 7 :
    					if (Sharma.getCoin3() != 1) {
    						System.out.println(
    							"There are " + Sharma.getCoin3() + " coin3s in the Bank.");
    					}
    					else {
    						System.out.println(
    							"There is " + Sharma.getCoin3() + " coin3 in the Bank.");
    					}
    					break;
    				case 8 :
    					if (Sharma.getCoin4() != 1) {
    						System.out.println(
    							"There are " + Sharma.getCoin4() + " coin4s in the Bank.");
    					}
    					else {
    						System.out.println(
    							"There is " + Sharma.getCoin4() + " coin4 in the Bank.");
    					}
    					break;
    				case 9 :
    					if (Sharma.getTotal() != 1) {
    						System.out.println(
    							"The Bank currently contains the equivalent of "
    								+ Sharma.getTotal()
    								+ " coin1s.");
    					}
    					else {
    						System.out.println(
    							"The Bank currently contains the equivalent of "
    								+ Sharma.getTotal()
    								+ " coin1.");
    					}
    					break;
    				case 0 :
    					finished = true;
    					break;
    				default :
    					System.out.println("Error! Invalid Choice!");
    					break;
    			}
    			if (finished == false) {
    				System.out.println("The Tiger Bank currently contains: ");
    				Sharma.outputdata();
    				System.out.println();
    				System.out.println();
    			}
    		}
    		while (finished == false);
    		System.out.println();
    		System.out.println("The final contents of the Bank are:");
    		Sharma.outputdata();
    		System.out.println();
    		System.out.println("So Make a note!");
    		//return 0;
    	}
    	static int menu() throws IOException // Menu Function
    	{
    		BufferedReader console =
    			new BufferedReader(new InputStreamReader(System.in));
    		int ch;
    		System.out.println();
    		System.out.println();
    		System.out.println(" Tiger bank Menu ");
    		System.out.println("~~~~~~~~~~~~~~~~~~");
    		System.out.println();
    		System.out.println("1. Add Coin1s");
    		System.out.println("2. Add Coin2s");
    		System.out.println("3. Add Coin3s");
    		System.out.println("4. Add Coin4s");
    		System.out.println("5. Display Coin1s");
    		System.out.println("6. Display Coin2s");
    		System.out.println("7. Display Coin3s");
    		System.out.println("8. Display Coin4s");
    		System.out.println("9. Display Total");
    		System.out.println("0. Quit");
    		System.out.println();
    		System.out.print("Enter Choice: ");
    		ch = console.read();
    		return ch;
    	}
    }
    I would suggest getting a free IDE that can inform you of these syntax errors. It will make your life much easier. You can get a free one at www.eclipse.org
    Last edited by Nemi; April 7th, 2003 at 02:45 PM.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    154
    Rep Power
    14
    Thanks for the help, this is indeed one of the first java programs I've constructed.

    Currently I've changed the menu method to static and the input ie. menu choice has been removed out of the menu function and put in the main method body like the following:

    do {

    menu ();

    int option;
    input = console.read(); // line 214
    option = Integer.parseInt(input);


    // Switch Options
    switch (option) {

    case 1: System.out.print("Enter the number of coin1s: ");
    input = console.readLine();
    tempi = Integer.parseInt(input);
    Sharma.setCoin1 (tempi);
    break;

    When compiling however, I get the following error message:

    Found 1 semantic error compiling "SheepBankDriver.java":

    214. input = console.read();
    <-------------------->
    *** Error: The type of the left-hand side in this assignment, "java/lang/String", is not compatible with the type of the right-hand side expression, "int".

    If you look at the code in the previous post, you will see that before the lines:

    menu();
    int choice;
    input = console.read();
    choice = Integer.parseInt(input);

    you will see that the variable input has already been declared as type string, however I would now like input to be of type int. I have tried adding int in front of the input variable above, but that does not seem to work!

    Can anyone explain this error message and explain how to fix it bearing in mind what I am trying to achieve is the creation of a user inputted integer variable.
    Last edited by markb_1984; April 8th, 2003 at 04:52 AM.
  14. #8
  15. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    When you use the 'read' method of a steam, it does indeed return an int and you are trying to put it in a String. What I don't understand however is that you have the answer in your code
    Code:
    input = console.readLine();
    tempi = Integer.parseInt(input);
    Sharma.setCoin1 (tempi);
    This is how you would read a string from the input and then convert it to an int. You have to realize that the input stream from the console is a character steam. You cannot type in a 5 and have it read as a 5 by the read method. The character 5 in ascii is a decimal int value of 53.

    Change the read to a readLine and see what it does.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    154
    Rep Power
    14
    'readLine' worked! Thanks.

IMN logo majestic logo threadwatch logo seochat tools logo