Results 1 to 16 of 16
6Likes Thread: declaration of arrays
- 03-14-2012, 12:32 PM #1
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
thxJava 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]); } } }
dhilip
- 03-14-2012, 12:38 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
Re: declaration of arrays
Lines of code like that cannot exist outside of an executable block.Java Code:x=new int[10];
The 'new int[10]' part can be part of the declaration line, though.Please do not ask for code as refusal often offends.
- 03-14-2012, 12:43 PM #3
Re: declaration of arrays
Ive changed the 'static' to non-static, and accessed it through an object.
is that possible?
thxJava 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]); } } }
dhilip
- 03-14-2012, 12:56 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
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'.Please do not ask for code as refusal often offends.
- 03-14-2012, 01:18 PM #5
Re: declaration of arrays
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
dhilipLast edited by noobplus; 03-14-2012 at 01:35 PM.
- 03-14-2012, 02:07 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
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.Please do not ask for code as refusal often offends.
- 03-14-2012, 02:28 PM #7
Member
- Join Date
- Jan 2012
- Posts
- 45
- Rep Power
- 0
Re: declaration of arrays
Take your code in post 1 and do this, as Tolls has been saying:
-Assignments of values have to be inside code block like methodJava Code:public static int[] x = new int[10];
-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.
- 03-14-2012, 02:47 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
Re: declaration of arrays
That's the idea.
Please do not ask for code as refusal often offends.
- 03-16-2012, 02:21 PM #9
Re: declaration of arrays
- 03-16-2012, 02:22 PM #10
Re: declaration of arrays
- 03-16-2012, 03:48 PM #11
Re: declaration of arrays
what does the "this.SIZE" and "return;"do ?Java Code:public void setData(int i,double x,double y) { if(i>this.Size) return; xArray[i]=x; yArray[i]=y; }
(I have a constructor
and the two variables "xArray" and "yArray" are declared in the main class)Java Code:public ReadPlat(int size) { this.Size=size; xArray= new double[this.Size]; yArray= new double[this.Size]; }
thx
dhilip
- 03-16-2012, 03:56 PM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
Re: declaration of arrays
You probably need to go through the tutorials.
This is not just basic stuff, but fundamental.Please do not ask for code as refusal often offends.
- 03-16-2012, 04:00 PM #13
Re: declaration of arrays
- 03-16-2012, 04:18 PM #14
Re: declaration of arrays
I'm getting a run-time errorJava 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(); } }
"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
dhilipLast edited by noobplus; 03-16-2012 at 04:26 PM.
- 03-17-2012, 02:09 AM #15
Re: declaration of arrays
- 03-17-2012, 02:27 AM #16
Re: declaration of arrays
Similar Threads
-
variable declaration
By elm101 in forum New To JavaReplies: 1Last Post: 10-26-2011, 10:40 PM -
array declaration
By maya700 in forum New To JavaReplies: 1Last Post: 03-07-2011, 04:33 PM -
class Declaration
By mahtab in forum New To JavaReplies: 1Last Post: 11-01-2009, 06:49 AM -
Declaration
By asifahmed in forum New To JavaReplies: 1Last Post: 04-05-2008, 05:38 AM -
JSP Declaration Directive
By Java Tip in forum Java TipReplies: 0Last Post: 12-10-2007, 05:42 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks