Re: declaration of arrays
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.
Re: declaration of arrays
Quote:
Originally Posted by
Tolls
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?
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
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'.
Re: declaration of arrays
Quote:
Originally Posted by
Tolls
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
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.
Re: declaration of arrays
Take your code in post 1 and do this, as Tolls has been saying: 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 :)
Re: declaration of arrays
Re: declaration of arrays
Quote:
Originally Posted by
MonkeyMan
Take your code in post 1 and do this, as Tolls has been saying:
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
Re: declaration of arrays
Quote:
Originally Posted by
Tolls
That's the idea.
thx.. working on that..
regards
dhilip
Re: declaration of arrays
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
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
Re: declaration of arrays
You probably need to go through the tutorials.
This is not just basic stuff, but fundamental.
Re: declaration of arrays
Quote:
Originally Posted by
Tolls
You probably need to go through the
tutorials.
This is not just basic stuff, but fundamental.
thx , working on that
1 Attachment(s)
Re: declaration of arrays
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.. Attachment 3265
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
Re: declaration of arrays
Quote:
Originally Posted by
noobplus
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..
Attachment 3265
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
Re: declaration of arrays
Quote:
Originally Posted by
noobplus
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..
Attachment 3265
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)?