Results 1 to 16 of 16
Thread: initialising an array
- 10-28-2008, 04:36 PM #1
Member
- Join Date
- Jun 2008
- Posts
- 54
- Rep Power
- 0
initialising an array
Hello,
i have the problem, that i have to read in lines from a text document into an array. I know how to write it, but my main problem is, that one line says 1*0 and so i don't know how i have to initialise the array. It can't be private int array so what do I have to use... it would be lovely if someone could help me
thank you so much
little_polarbear
- 10-28-2008, 04:50 PM #2
I'm a little confused to what you mean. You say you know how to write it so can you post some pseudo code at least so we can get a better idea?
The line that says 1*0 needs to be calculated and read in as just 0? If that's the case you can just noob your way out and put an if statement that checks if the string is "1*0" and change it to just 0, or you could look up a string to int converter method(I am unaware of any off the top of my head) so it would work for multiple occasions like this one.
- 10-28-2008, 04:58 PM #3
Member
- Join Date
- Jun 2008
- Posts
- 54
- Rep Power
- 0
oh no... sry... it is not a calculation... I have to write somethinh with MinTerms... really confusing... this is the code...
import java.io.*;
import java.util.Vector;
public class Implikanten
{
private Vector Q[];
public int n;
private int array[];
public void reading(String fileName)
throws FileNotFoundException, NullPointerException
{
String line = null;
int n = 0;
int track = 0;
BufferedReader in = new BufferedReader(new FileReader(fileName));
try
{
while((line = in.readLine()) != null)
{
if(track == 0)
{
n = Integer.parseInt(line);
array = new array[n];
}
else
{
array[track - 1] = array.valueOf(line.trim());
}
if(track == n+)
{
break;
}
track++;
}
}
catch (IOException e)
{
System.out.println("Error in reading file");
}
for(int i = 0; i <= Q.length; i++)
{
System.out.println(array[i]);
}
}
public static void main(String[] args)
throws FileNotFoundException
{
Implikanten app = new Implikanten();
app.reading("fileName");
}
}
and it is saying that i have to save the MinTerms in the array Q so that if I have for example 100 it must be in Q[2] because it contains 2 zeros, if it has one zero, it must be in Q[1] the * just means, that there is no number... what i wanted to do is read in the terms into an normal array and then put them from there into the Vector Q but i think that there is another way which is more simple, so that i can put it directly into the array Q, but i don't know exactly how to do it... i tried it and this is my code, which followes:
import java.io.*;
import java.util.Vector;
public class Implikanten
{
private Vector Q[];
public int n;
public void reading(String fileName)
throws FileNotFoundException, NullPointerException
{
String line = null;
int n = 0;
int track = 0;
BufferedReader in = new BufferedReader(new FileReader(fileName));
try
{
while((line = in.readLine()) != null)
{
if(track == 0)
{
n = Integer.parseInt(line);
Q = new Vector[n];
}
else
{
Q[track - 1] = Vector.valueOf(line.trim());
}
if(track == n+)
{
break;
}
track++;
}
}
catch (IOException e)
{
System.out.println("Error in reading file");
}
for(int i = 0; i <= Q.length; i++)
{
System.out.println(Q[i]);
}
}
public static void main(String[] args)
throws FileNotFoundException
{
Implikanten app = new Implikanten();
app.reading("fileName");
}
}
little_polarbear
- 10-28-2008, 05:11 PM #4
Member
- Join Date
- Oct 2008
- Posts
- 21
- Rep Power
- 0
toArray() populate a static array with the contents of a Vector? or is that to simple.
cheers
ian J
- 10-28-2008, 05:27 PM #5
Just made your response a little easier to read. Did you copy paste your code exactly from a text document or an IDE? I ask this because of
is there supposed to a digit after that or are you just incrementing it?Java Code:if(track == n+)
also
is supposed to take the value of the line after you parse it. SoJava Code:Q[track - 1] = Vector.valueOf(line.trim());
or some sort of numeric value. I havn't worked with Vector but common sense tells me i can't use a String type and get a valueOf unless it has been parsed to something numeric.Java Code:Q[track - 1] = Vector.valueOf(Integer.parseInt(line.trim()));
- 10-28-2008, 07:20 PM #6
Member
- Join Date
- Jun 2008
- Posts
- 54
- Rep Power
- 0
Hey Ian,
just one question, if i wanna put the vector into an array, i first have to read in the values from the document, haven't I.... but I wanna have all the values which contains two zeros in Q[2] and the values which contains one zero in Q[1] and this method wouldn't do it... id i understood it right... or do i understand it wrong...
little_polarbear
- 10-28-2008, 08:45 PM #7
Member
- Join Date
- Oct 2008
- Posts
- 21
- Rep Power
- 0
- 10-28-2008, 09:08 PM #8
Member
- Join Date
- Jun 2008
- Posts
- 54
- Rep Power
- 0
ok, i kind of managed to read it into an array, the one problem is, that I don't know, how i have to initialisise my array, because i am always getting an error message, that * is not an integer... so i now have an array with
array[0] = 100
array[1] = 001
array[2] = 1*0
the star doesn't mean times, just that there is no number... and now i have to put it into the vector Q[] and here i don't know how to do do it... because i have to count the zeros in this array and then put it into the right position at Q[] can i do it by the way with the methos AddMinterm()
little_polarbear
- 10-28-2008, 09:21 PM #9
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
could you give us the exact error message(s) and what line(s) they indicate?
- 10-28-2008, 09:43 PM #10
Member
- Join Date
- Jun 2008
- Posts
- 54
- Rep Power
- 0
ok, I just put the code down again... and under it the error message
import java.io.*;
import java.util.Vector;
public class Implikanten
{
private Vector Q[];
public int n;
private int array[];
public void AddMinterm(int n, String s)
{
}
public void reading(String fileName)
throws FileNotFoundException, NullPointerException
{
String line = null;
int n = 0;
int track = 0;
BufferedReader in = new BufferedReader(new FileReader(fileName));
try
{
while((line = in.readLine()) != null)
{
if(track == 0)
{
n = Integer.parseInt(line);
Q = new Vector[n];
}
else
{
Q[track - 1] = Vector.valueOf(Integer.parseInt(line.trim()));
}
if(track == n)
{
break;
}
track++;
}
}
catch (IOException e)
{
System.out.println("Error in reading file");
}
for(int i = 0; i <= Q.length; i++)
{
System.out.println(Q[i]);
}
}
public void Ausgeben()
{
}
public static void main(String[] args)
throws FileNotFoundException
{
Implikanten app = new Implikanten();
app.reading("fileName");
}
}
and here is the error message:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method valueOf(int) is undefined for the type Vector
at Implikanten.reading(Implikanten.java:41)
at Implikanten.main(Implikanten.java:70)
hope that it is helping you... thank you so much for all your help
little_polarbear
- 10-28-2008, 09:59 PM #11
Member
- Join Date
- Oct 2008
- Location
- Aberystwyth
- Posts
- 55
- Rep Power
- 0
Maybe im just being a noob here, if i am then im sorry, but try:
Sorry if that doesn't help.Java Code:import java.lang.*;
- 10-28-2008, 10:28 PM #12
Member
- Join Date
- Jun 2008
- Posts
- 54
- Rep Power
- 0
thank you, i tried it, but it doesn't really help, but still thank you so much :-)
little_polarbear
- 10-28-2008, 10:48 PM #13
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
java.lang.* is imported by default and doesn't have to explicitly be stated.
The error is telling you that the class Vector has no method "valueOf" that takes in type int as a parameter.
-
- 10-29-2008, 10:16 AM #15
Member
- Join Date
- Oct 2008
- Posts
- 21
- Rep Power
- 0
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method valueOf(int) is undefined for the type Vector
should be I think ? Q[track - 1] = Integer.valueOf(Integer.parseInt(line.trim()));
I think its expecting a String at valueOf and you are passing an int into the vector.
thanks
Ian JLast edited by ianjedi; 10-29-2008 at 10:26 AM. Reason: new info
- 10-29-2008, 02:26 PM #16
Similar Threads
-
new array ?
By seandingobat in forum New To JavaReplies: 7Last Post: 10-03-2008, 07:31 PM -
initialising Array of object??
By Somitesh Chakraborty in forum New To JavaReplies: 4Last Post: 08-27-2008, 09:00 PM -
initialising a webcam?
By jonsamwell in forum New To JavaReplies: 1Last Post: 08-25-2008, 05:17 PM -
Array Reflection: Multi Array Reflection
By Java Tip in forum java.langReplies: 0Last Post: 04-23-2008, 08:08 PM -
can anyone help... 2d Array
By Mark1989 in forum New To JavaReplies: 2Last Post: 03-12-2008, 08:59 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks