> i can create the boolean array but don't know how to convert the string
> (001011) which im reading in from the file, in to the array.
Assuming the array is the same size as the string, you could use a
for-loop to iterate over the string, looking at the characters one by one,
and setting the appropriate value in the array.
- Oliver
> I'm reading in details from a file using java scanner ;
>
> Here is my code so far;
[snip code]
Try this: (need to put the correct path to "p.txt".
public class Test
{
public static void main(String[] args)
{
String file = "p.txt";
String forename;
String surname;
String city;
String telephone;
char gender;
int age;
int intrests;
int i = 0;
try {
Scanner in = new Scanner(new File(file));
while (in.hasNext())
{
forename = in.next();
surname = in.next();
city = in.next();
telephone= in.next();
gender = in.next().charAt(0);
age = in.nextInt();
intrests = Integer.parseInt(in.next());
String holdit = in.next();
boolean[] boolArray = new boolean[6];
for (i=0; i < 6; i++) {
boolArray [i] = holdit.charAt(i) != '0';
}
Person person = new Person(forename, surname, city,
telephone, gender, age, intrests, boolArray);
}
} catch (Exception e) {
System.out.println(e);
}
}
}
class Person {
String forename;
String surname;
String city;
String telephone;
char gender;
int age;
int intrests;
boolean [] boolArray;
Person ( String forename, String surname, String city, String
telephone, char gender, int age, int intrests, boolean[] boolAraray )
{
this.forename = forename;
this.surname = surname;
this.city = city;
this.telephone = telephone;
this.gender = gender;
this.age = age;
this.intrests = intrests;
this.boolArray = boolArray;
}
}

Signature
Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________
'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
IchBin - 15 Feb 2006 21:04 GMT
>> I'm reading in details from a file using java scanner ;
>>
[quoted text clipped - 3 lines]
>
> Try this: (need to put the correct path to "p.txt".
[snip code]
> intrests = Integer.parseInt(in.next());
> String holdit = in.next();
[quoted text clipped - 3 lines]
> boolArray [i] = holdit.charAt(i) != '0';
> }
[snip code]
if you are not sure of the size of the input field just do this:
intrests = in.nextInt();
String holdit = in.next();
int tokenLength = holdit.length();
boolean[] boolArray = new boolean[tokenLength ];
for (i=0; i < tokenLength ; i++) {
boolArray [i] = holdit.charAt(i) != '0';
}

Signature
Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________
'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
thefatson@gmail.com - 17 Feb 2006 05:21 GMT
Very strange Im doing the same project right now I can do all the suff
you have mentioned so far but something weird is happenning.
I can read the string to a boolean array but the output was messed up
in the end.
I put in println s to find the problem and discovered that even though
the right boolean values were being read from the string when I tried
to print them from the object holding the array each one contained
"false false false true true true".
Here is the code :
public static void main (String [] args)
{
Client c;
Client[] clients = new Client[20];
File people;
Scanner in;
int i = 0;
Boolean[] bools = new Boolean[6];
people = new File("client.txt");
try
{
in = new Scanner(people);
while (in.hasNext())
{
String name = in.next();
String sname = in.next();
String local = in.next();
String telo = in.next();
String sex = in.next();
String age = in.next();
String like = in.next();
Character s = sex.charAt(0);
for(int j = 0; j<6; j++)
{
if (like.charAt(j) == '1')
{
bools[j] = new Boolean(true);
System.out.println("111");
System.out.println(Boolean.toString(bools[j]));
}
if(like.charAt(j) == '0')
{
bools[j] = new Boolean(false);
System.out.println("222");
System.out.println(Boolean.toString(bools[j]));
}
}
c = new Client(name, sname, local, telo, s, Integer.parseInt(age),
bools);
clients[i] = c;
i++;
}
in.close();
for(int ii = 0;ii<clients.length;ii++)
{
System.out.println(clients[ii].getSurname() + " " +
Boolean.toString(clients[ii].getInterests()[0])+ " " +
Boolean.toString(clients[ii].getInterests()[1])+ " " +
Boolean.toString(clients[ii].getInterests()[2])+ " " +
Boolean.toString(clients[ii].getInterests()[3])+ " " +
Boolean.toString(clients[ii].getInterests()[4])+ " " +
Boolean.toString(clients[ii].getInterests()[5]));
}
The printlns used when converting to the boolean array give the right
output but the long line of printlns at the end just give "false false
false true true true" for each client object.
The constructor seems fine, this is driving me insane its like 2+2
suddenly equals 5.
I realise the Boolean is slightly different from boolean but I have
been messing around with things like that to find a solution.
thefatson@gmail.com - 17 Feb 2006 10:20 GMT
I sorted it out, I just declared the boolean array within the while
loop and it worked. It must have been using the same array for each
object before I did that. I also changed all my Booleans to booleans
but that is inconsequential.