The Artima Developer Community
Sponsored Link

Java Answers Forum
Char to boolean error

4 replies on 1 page. Most recent reply: May 26, 2003 7:39 AM by Courtenay

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Courtenay

Posts: 23
Nickname: courtz
Registered: Apr, 2003

Char to boolean error Posted: May 26, 2003 3:46 AM
Reply to this message Reply
Advertisement
I have a simple problem in that i want to make a char into a boolean object. Don't ask me why its a long problem. i have the following method:

public boolean validateSent(boolean test[]){

int count = 0;
for (int i = 0; i <= data.length; i++){

if (data[i] != '*' && data[i] != '~'){
data[i] = test[count];
System.out.println(data[i]);
count++;
}

}

}


where data[i] is an array of chars and test[count] is an array of boolean. Can anyone help with this seemingly trivial problem

thanks
courtz


Adam Duffy

Posts: 168
Nickname: adamduffy
Registered: Feb, 2003

Re: Char to boolean error Posted: May 26, 2003 4:55 AM
Reply to this message Reply
Just to make sure I have understood your question, you want to do something like

char c = 'x';
boolean b = false;
 
c = (boolean) b;


Is that right? As far as I know, it's not possible to cast from a char to a boolean or from anything to a boolean. Your best bet is to decide under what circumstances the boolean is true or false and use an if-statement as appropriate.

Adam

Courtenay

Posts: 23
Nickname: courtz
Registered: Apr, 2003

Re: Char to boolean error Posted: May 26, 2003 6:56 AM
Reply to this message Reply
How would i use an if statement? do you mean like this:

public boolean validateSent(boolean test[]){

int count = 0;
for (int i = 0; i <= data.length; i++){

if (data[i] != '*' && data[i] != '~'){
if (test[count])
data[i] = true;

}

}

}


even this doesnt work though? thanx courtz

Adam Duffy

Posts: 168
Nickname: adamduffy
Registered: Feb, 2003

Re: Char to boolean error Posted: May 26, 2003 7:13 AM
Reply to this message Reply
See the comments in the code...

public boolean validateSent(boolean test[])
{
  int count = 0;
  // the variable 'data' is an array of char
  // for example, char data[]
  for (int i = 0; i <= data.length; i++)
  {
    // if element 'i' of 'data' is neither * nor ~
    // then proceed
    if (data[i] != '*' && data[i] != '~')
    {		
      // if element 0 of boolean array 'test' is true
      // note: variable 'count' is never changed...
      if (test[count])
        // cannot perform this operation since variable
        // 'data' is an array of characters...
        data[i] = true;
    }
  }
}


Perhaps if you state what you wish to do with your code, we might be of more assistance.

Adam

Courtenay

Posts: 23
Nickname: courtz
Registered: Apr, 2003

Re: Char to boolean error Posted: May 26, 2003 7:39 AM
Reply to this message Reply
Thanks for your swift reply. Basically i need to read in a proposition from a file - ie abc*~a~b~c
where * is an and (&) sign
~ is a not (!) sign
and the letters are all or(ed) (||)
so basically i need to find out whether the above sentence is true or false.
in this case it would be:
a or b or c and not a or not b or not c
The textfile that we must use looks like this:
3
2
abc*~a~b~c*

where 3 is the number of variables and 2 the number of clauses.

here is my code so far:

import java.io.*;

public class SATProblem{

private int variable;
private int clause;
private char[] data;

private void Stringreader(){



String line = "";

String file = "testfile.txt";


try
{
FileReader fr = new FileReader(file);
BufferedReader inFile = new BufferedReader(fr);




variable = Integer.parseInt(inFile.readLine());
clause = Integer.parseInt(inFile.readLine());

data = (inFile.readLine()).toCharArray();

//data = (inFile.readLine()).toCharArray();


inFile.close();
}

catch (FileNotFoundException e)
{
System.out.println(e);
}
catch (IOException e)
{
System.out.println(e);
}

System.out.println(data);
System.out.println(variable);
System.out.println(clause);
}

public boolean getBoolean(int n, int pos){
pos = variable - pos;
int temp = (int)(n/Math.pow(2,(pos-1))% 2);
if (temp == 1) return true;
else return false;
}

private void startAnalysis(){
boolean test[] = new boolean[variable];
boolean answer = false;

for (int n = 0; n < Math.pow(2, variable); n++){

for (int pos = 0; pos < variable; pos++){
test [pos] = getBoolean(n,pos);

}

if (validateSent(test)){
answer = true;
break;
}
}
}
public boolean validateSent(boolean test[]){

int count = 0;

while (count <= variable){

for (int i = 0; i <= data.length; i++){

if (data[i] != '~'){
if (test[count])
data[i] = true;

}

}
}
}


public static void main (String[] args){

SATProblem abc = new SATProblem();
abc.Stringreader();
abc.startAnalysis();
}

}

startAnalysis and getBoolean basically construct the truth table where for three variables
when n = 0,test[0] = false, test[1] = false, test[2] = false
when n = 1,test[0] = false, test[1] = false, test[2] = true
when n = 1,test[0] = false, test[1] = true, test[2] = true
ETC
you get the idea. Do u think u could help?
Thanks a lot again
COurtz

Flat View: This topic has 4 replies on 1 page
Topic: Flicker with my gif image Previous Topic   Next Topic Topic: stacker, can you find the faults?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use