Home Answers Viewqa Java-Beginners How to Solve the Syntax Error in java??

 
 


vigneshwaran
How to Solve the Syntax Error in java??
1 Answer(s)      2 years and 6 months ago
Posted in : Java Beginners

Syntax error in java? i have tried this program its showing error like this in Eclipse "Syntax error on token "i", VariableDeclarator expected after this token" . in last line of the program "objectname.i"

What is the reason for this error??How to access the variable "i"

import java.util.Scanner; public class constructors { int i ; private int j; private int k;

public static void main(String args[]) { System.out.println("Enter the name:");

} } class constructor{ constructors objectname = new constructors(); Scanner myScanner = new Scanner(System.in); objectname.i ; }

View Answers

December 2, 2010 at 10:50 AM


Hello Friend,

Actually your syntax is not correct.

Anyways, try the following code:

import java.util.*;

    class constructor{
        int i=0;
        constructor(){
        Scanner input=new Scanner(System.in);
        System.out.print("Enter number: ");
        i=input.nextInt();
        }
    }
    public class constructors{
        public static void main(String args[]){
        constructor con=new constructor();
        System.out.println(con.i);
        }
    }

Thanks









Related Pages:

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.