Results 1 to 16 of 16
Like Tree6Likes
  • 1 Post By Tolls
  • 1 Post By Tolls
  • 1 Post By Tolls
  • 1 Post By MonkeyMan
  • 1 Post By Tolls
  • 1 Post By Tolls

Thread: declaration of arrays

  1. #1
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default declaration of arrays

    hi, I want to initialise the array 'x' in a class which displays the values of 'x'
    but where should we declare the array 'x'
    I declared inside the public class , but shows errors

    Java Code:
    public class array_inc {
    
    	
    	public static int[] x;
    	x=new int[10];
    	
    	
    	public static void main(String args[])
    	{for(int i=0;i<10;i++){
    		x[i] = i;
    		System.out.println(x[i]);
    	}
    		
    	}
    }
    thx
    dhilip

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: declaration of arrays

    Java Code:
    x=new int[10];
    Lines of code like that cannot exist outside of an executable block.
    The 'new int[10]' part can be part of the declaration line, though.
    noobplus likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: declaration of arrays

    Quote Originally Posted by Tolls View Post
    Java Code:
    x=new int[10];
    Lines of code like that cannot exist outside of an executable block.
    The 'new int[10]' part can be part of the declaration line, though.
    Ive changed the 'static' to non-static, and accessed it through an object.
    is that possible?

    Java Code:
    public class fileread {
    
    	
    	int[] x;//shows an error yet. i get a thought, that i can't declare a variable outside a method
    	x=new int[10];
    	
    	
    	public static void main(String args[])
    	{
    		fileread a = new fileread();
    		
    	for(int i=0;i<10;i++){
    		a.x[i] = i;
    		System.out.println(a.x[i]);
    	}
    		
    	}
    }
    thx
    dhilip

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: declaration of arrays

    The problem is nothing to do with static or non-static, it is to do with your assignment on line 5.
    That code is not allowed outside a block (eg a method).
    You can, as I said in my previous post, do the assignment on the same line as the declaration, on line 4.

    ETA: You have no, of course, introduced a problem by removing the static from 'x'.
    noobplus likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: declaration of arrays

    Quote Originally Posted by Tolls View Post
    The problem is nothing to do with static or non-static, it is to do with your assignment on line 5.
    That code is not allowed outside a block (eg a method).
    You can, as I said in my previous post, do the assignment on the same line as the declaration, on line 4.

    ETA: You have no, of course, introduced a problem by removing the static from 'x'.
    thx, I've declared it in main, and shows no errors..
    what means "ETA", and
    what about a constructor,
    [code]
    public class fileread {

    private int[] x;
    void fileread()
    {
    int[] x = new int[10];//ive initializes x, will it be initialized into its object?
    }


    public static void main(String args[])
    {
    fileread a = new fileread();

    for(int i=0;i<10;i++){
    a.x[i] = i;
    System.out.println(a.x[i]);
    }

    }
    }
    /[code]

    thx
    dhilip
    Last edited by noobplus; 03-14-2012 at 01:35 PM.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: declaration of arrays

    ETA = Edited To Add.

    You're flailing now.
    Look, go back to your original code in your first post.

    Now, repeating myself -
    "The 'new int[10]' part can be part of the declaration line, though."
    That is, the 'new int[10]' on line 5 should be on line 4.
    That's it.
    That's all you had to do to fix the problem.
    noobplus likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    MonkeyMan is offline Member
    Join Date
    Jan 2012
    Posts
    45
    Rep Power
    0

    Default Re: declaration of arrays

    Take your code in post 1 and do this, as Tolls has been saying:
    Java Code:
    public static int[] x = new int[10];
    -Assignments of values have to be inside code block like method
    -Declarations can be outside of method blocks
    -Combine assignment with declaration

    =

    problem solved?

    Correct me if I'm wrong Tolls, thank you :)
    Last edited by MonkeyMan; 03-14-2012 at 02:30 PM.
    noobplus likes this.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: declaration of arrays

    That's the idea.
    noobplus likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: declaration of arrays

    Quote Originally Posted by MonkeyMan View Post
    Take your code in post 1 and do this, as Tolls has been saying:
    Java Code:
    public static int[] x = new int[10];
    -Assignments of values have to be inside code block like method
    -Declarations can be outside of method blocks
    -Combine assignment with declaration



    problem solved?

    Correct me if I'm wrong Tolls, thank you :)
    thank you, problem solved , and working on that..

    regards
    dhilip

  10. #10
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: declaration of arrays

    Quote Originally Posted by Tolls View Post
    That's the idea.
    thx.. working on that..

    regards
    dhilip

  11. #11
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: declaration of arrays

    Java Code:
    public void setData(int i,double x,double y)
    	{
    		if(i>this.Size)
    			return;
    		xArray[i]=x;
    		yArray[i]=y;
    	}
    what does the "this.SIZE" and "return;"do ?
    (I have a constructor
    Java Code:
    public ReadPlat(int size)
    	{
    		this.Size=size;
    		xArray= new double[this.Size];
    		yArray= new double[this.Size];
    	}
    and the two variables "xArray" and "yArray" are declared in the main class)

    thx
    dhilip

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: declaration of arrays

    You probably need to go through the tutorials.
    This is not just basic stuff, but fundamental.
    noobplus likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  13. #13
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: declaration of arrays

    Quote Originally Posted by Tolls View Post
    You probably need to go through the tutorials.
    This is not just basic stuff, but fundamental.
    thx , working on that

  14. #14
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: declaration of arrays

    Java Code:
    import java.io.*;
    
    public class ReadPlat 
    {
    	double[] xArray ;
    	double[] yArray ;
    	int Size;
    	
    	public ReadPlat(int size)
    	{
    		this.Size=size;
    		xArray= new double[this.Size];
    		yArray= new double[this.Size];
    	}
    	
    	public void setData(int i,double x,double y)
    	{
    		if(i>this.Size)
    			return;
    		xArray[i]=x;
    		yArray[i]=y;
    	}
    	
    	public static void main(String[] args) throws Exception
    	{   
    		ReadPlat ReadPlatInstance;
    		String filename;
    		FileReader file_in;
    		BufferedReader in;	
    		String input_line;
    		String[]input_split;
    		int TotalLineCount=0;
    		
    		filename = args[0];
    		file_in = new FileReader(filename);
    	    in 		= new BufferedReader(file_in);
    	    
    	    input_line ="Starting";
    	    
    	    
    	    //Reading toatal lines 
    	    while(input_line!=null)
    	    {	
    	    	input_line =in.readLine();
    	    	TotalLineCount++;
    	    }
    	    System.out.println(TotalLineCount);
    	    ReadPlatInstance = new  ReadPlat(TotalLineCount);
    	    
    	    //Closing the File and reopening
    	    TotalLineCount=0;
    	    input_line ="ReStarting";
    	    in.close();
    	    file_in.close();
    	    file_in = new FileReader(filename);
    	    in 		= new BufferedReader(file_in);	    
    	    while(input_line!=null)
    	    {	
    	    	 System.out.println("---------------");
    	    	 System.out.println("Line No:"+TotalLineCount);
    	    	
    	    	
    	    	input_line =in.readLine();
    	    	input_line=input_line.replaceAll("\"","");
    	    	input_split = input_line.split(" ");    	   	
    	    	
    	    	 System.out.println("Input Line:"+input_line);
    	    	 System.out.print(input_split.length);
    	    	 System.out.print("x"+input_split[0]);
    	    	 System.out.print("y"+input_split[2]);
    	    	ReadPlatInstance.setData(TotalLineCount, Double.valueOf(input_split[0].trim()).doubleValue(),Double.valueOf(input_split[2].trim()).doubleValue()   )	;    	
    	    	TotalLineCount++;
    	    	
    	    }	
    	    
    	    in.close();
    	    file_in.close();
    	    
    	    
    	}
    	
    }
    I'm getting a run-time error
    "Exception in thread "main" java.lang.NullPointerException
    at ReadPlat.main(ReadPlat.java:65)"
    Could I know why it comes?

    --edited: im attaching the argument file.. abc.txt
    the text file containing the following values..
    "0.000000" " -0.012"
    "0.000010" " +0.053"
    "0.000020" " +0.050"
    "0.000030" " +0.053"
    "0.000040" " +0.050"
    "0.000050" " +0.053"
    "0.000060" " +0.053"
    "0.000070" " +0.050"
    "0.000080" " +0.053"
    "0.000090" " +0.053"
    "0.000100" " +0.053"
    "0.000110" " +0.053"
    "0.000120" " +0.053"
    "0.000130" " +0.053"
    "0.000140" " +0.053"

    regards
    dhilip
    Last edited by noobplus; 03-16-2012 at 04:26 PM.

  15. #15
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: declaration of arrays

    Quote Originally Posted by noobplus View Post
    Java Code:
    import java.io.*;
    
    public class ReadPlat 
    {
    	double[] xArray ;
    	double[] yArray ;
    	int Size;
    	
    	public ReadPlat(int size)
    	{
    		this.Size=size;
    		xArray= new double[this.Size];
    		yArray= new double[this.Size];
    	}
    	
    	public void setData(int i,double x,double y)
    	{
    		if(i>this.Size)
    			return;
    		xArray[i]=x;
    		yArray[i]=y;
    	}
    	
    	public static void main(String[] args) throws Exception
    	{   
    		ReadPlat ReadPlatInstance;
    		String filename;
    		FileReader file_in;
    		BufferedReader in;	
    		String input_line;
    		String[]input_split;
    		int TotalLineCount=0;
    		
    		filename = args[0];
    		file_in = new FileReader(filename);
    	    in 		= new BufferedReader(file_in);
    	    
    	    input_line ="Starting";
    	    
    	    
    	    //Reading toatal lines 
    	    while(input_line!=null)
    	    {	
    	    	input_line =in.readLine();
    	    	TotalLineCount++;
    	    }
    	    System.out.println(TotalLineCount);
    	    ReadPlatInstance = new  ReadPlat(TotalLineCount);
    	    
    	    //Closing the File and reopening
    	    TotalLineCount=0;
    	    input_line ="ReStarting";
    	    in.close();
    	    file_in.close();
    	    file_in = new FileReader(filename);
    	    in 		= new BufferedReader(file_in);	    
    	    while(input_line!=null)
    	    {	
    	    	 System.out.println("---------------");
    	    	 System.out.println("Line No:"+TotalLineCount);
    	    	
    	    	
    	    	input_line =in.readLine();
    	    	input_line=input_line.replaceAll("\"","");
    	    	input_split = input_line.split(" ");    	   	
    	    	
    	    	 System.out.println("Input Line:"+input_line);
    	    	 System.out.print(input_split.length);
    	    	 System.out.print("x"+input_split[0]);
    	    	 System.out.print("y"+input_split[2]);
    	    	ReadPlatInstance.setData(TotalLineCount, Double.valueOf(input_split[0].trim()).doubleValue(),Double.valueOf(input_split[2].trim()).doubleValue()   )	;    	
    	    	TotalLineCount++;
    	    	
    	    }	
    	    
    	    in.close();
    	    file_in.close();
    	    
    	    
    	}
    	
    }
    I'm getting a run-time error
    "Exception in thread "main" java.lang.NullPointerException
    at ReadPlat.main(ReadPlat.java:65)"
    Could I know why it comes?

    --edited: im attaching the argument file.. abc.txt
    the text file containing the following values..
    "0.000000" " -0.012"
    "0.000010" " +0.053"
    "0.000020" " +0.050"
    "0.000030" " +0.053"
    "0.000040" " +0.050"
    "0.000050" " +0.053"
    "0.000060" " +0.053"
    "0.000070" " +0.050"
    "0.000080" " +0.053"
    "0.000090" " +0.053"
    "0.000100" " +0.053"
    "0.000110" " +0.053"
    "0.000120" " +0.053"
    "0.000130" " +0.053"
    "0.000140" " +0.053"

    regards
    dhilip
    problem solved.. I restarted my eclipse, now it's working fine without run time errors.
    thx
    dhilip

  16. #16
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: declaration of arrays

    Quote Originally Posted by noobplus View Post
    Java Code:
    import java.io.*;
    
    public class ReadPlat 
    {
    	double[] xArray ;
    	double[] yArray ;
    	int Size;
    	
    	public ReadPlat(int size)
    	{
    		this.Size=size;
    		xArray= new double[this.Size];
    		yArray= new double[this.Size];
    	}
    	
    	public void setData(int i,double x,double y)
    	{
    		if(i>this.Size)
    			return;
    		xArray[i]=x;
    		yArray[i]=y;
    	}
    	
    	public static void main(String[] args) throws Exception
    	{   
    		ReadPlat ReadPlatInstance;
    		String filename;
    		FileReader file_in;
    		BufferedReader in;	
    		String input_line;
    		String[]input_split;
    		int TotalLineCount=0;
    		
    		filename = args[0];
    		file_in = new FileReader(filename);
    	    in 		= new BufferedReader(file_in);
    	    
    	    input_line ="Starting";
    	    
    	    
    	    //Reading toatal lines 
    	    while(input_line!=null)
    	    {	
    	    	input_line =in.readLine();
    	    	TotalLineCount++;
    	    }
    	    System.out.println(TotalLineCount);
    	    ReadPlatInstance = new  ReadPlat(TotalLineCount);
    	    
    	    //Closing the File and reopening
    	    TotalLineCount=0;
    	    input_line ="ReStarting";
    	    in.close();
    	    file_in.close();
    	    file_in = new FileReader(filename);
    	    in 		= new BufferedReader(file_in);	    
    	    while(input_line!=null)
    	    {	
    	    	 System.out.println("---------------");
    	    	 System.out.println("Line No:"+TotalLineCount);
    	    	
    	    	
    	    	input_line =in.readLine();
    	    	input_line=input_line.replaceAll("\"","");
    	    	input_split = input_line.split(" ");    	   	
    	    	
    	    	 System.out.println("Input Line:"+input_line);
    	    	 System.out.print(input_split.length);
    	    	 System.out.print("x"+input_split[0]);
    	    	 System.out.print("y"+input_split[2]);
    	    	ReadPlatInstance.setData(TotalLineCount, Double.valueOf(input_split[0].trim()).doubleValue(),Double.valueOf(input_split[2].trim()).doubleValue()   )	;    	
    	    	TotalLineCount++;
    	    	
    	    }	
    	    
    	    in.close();
    	    file_in.close();
    	    
    	    
    	}
    	
    }
    I'm getting a run-time error
    "Exception in thread "main" java.lang.NullPointerException
    at ReadPlat.main(ReadPlat.java:65)"
    Could I know why it comes?

    --edited: im attaching the argument file.. abc.txt
    the text file containing the following values..
    "0.000000" " -0.012"
    "0.000010" " +0.053"
    "0.000020" " +0.050"
    "0.000030" " +0.053"
    "0.000040" " +0.050"
    "0.000050" " +0.053"
    "0.000060" " +0.053"
    "0.000070" " +0.050"
    "0.000080" " +0.053"
    "0.000090" " +0.053"
    "0.000100" " +0.053"
    "0.000110" " +0.053"
    "0.000120" " +0.053"
    "0.000130" " +0.053"
    "0.000140" " +0.053"

    regards
    dhilip
    hi,
    I want to write to create a calculation on the arrays "xAary" and yArray" values.. so, where should I write it?
    inside main(at line 73) ? or as a different method(before main)?

Similar Threads

  1. variable declaration
    By elm101 in forum New To Java
    Replies: 1
    Last Post: 10-26-2011, 10:40 PM
  2. array declaration
    By maya700 in forum New To Java
    Replies: 1
    Last Post: 03-07-2011, 04:33 PM
  3. class Declaration
    By mahtab in forum New To Java
    Replies: 1
    Last Post: 11-01-2009, 06:49 AM
  4. Declaration
    By asifahmed in forum New To Java
    Replies: 1
    Last Post: 04-05-2008, 05:38 AM
  5. JSP Declaration Directive
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-10-2007, 05:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •