0

I'm having trouble with my school Java code - I'm trying to create a constructor that takes a boolean value, and then uses it later, but for some reason it's always true. Could anyone tell me what I'm doing wrong?

EDIT: I'm posting my entire code, I thought redacting it a little would make my problem clearer, but it seems it only created confusion.

        import java.util.*;
public class L3_Z6 {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    class FunnyString{
        private boolean ascii;
        private String slowo;
        private char separator;


        public FunnyString (String slowo, char separator, boolean ascii){
            this.slowo=slowo;
            this.separator=separator;
            this.ascii=ascii;

        }
        public FunnyString (String slowo, char separator){
            this(slowo, separator, false);
        }
        public FunnyString (String slowo, boolean ascii){
            this(slowo,'-', ascii);
        }
        public FunnyString (String slowo){
            this(slowo,'-', false);
        }
        public void setAscii (boolean a){
            ascii=a;
        }
        public void setChar (char b){
            separator=b;
        }

        public String toString (){

            int dlugosc = slowo.length();
            int licznik = 0;
            String wynik="";
            do{
                if(ascii=false){
                wynik+=slowo.charAt(licznik);
                }
                if(ascii=true){
                    wynik+=(int)slowo.charAt(licznik);
                }
                if(licznik!=dlugosc-1)
                    wynik+=separator;
                licznik++;
            }while (licznik!=dlugosc);
            wynik+="\n";

            return wynik;
        }
    }
          FunnyString w1=new FunnyString("Kaktus");
          FunnyString w2=new FunnyString("Eukaliptus",'*');  
          FunnyString w3=new FunnyString("Yuka",true);

          System.out.println(w1);

          System.out.println(w2);

          System.out.println(w3);

          w1.setAscii(true);
          w1.setChar('*');
          System.out.println(w1);

          w3.setAscii(false);
          w3.setChar('!');
          System.out.println(w3);



}


}
7
  • 3
    How did you confirm that ascii is always true? Also, what is slowo? This is incomplete code. How do you expect us to solve the issue with incomplete code? Commented May 12, 2015 at 17:15
  • 1
    show the later part we dont know how you tried to print? Commented May 12, 2015 at 17:17
  • 3
    MCVE, please. Commented May 12, 2015 at 17:18
  • I cut out parts where I actually used all of those values. If i'd just create a if(ascii=true){System.out.println("it's true"), it would always print out that string. Commented May 12, 2015 at 17:23
  • Now I feel really dumb, but it works. Thanks Toumash! Commented May 12, 2015 at 17:30

2 Answers 2

2

In your if conditions, you're using assignment instead of comparison.

Change this

if(ascii=false){
    wynik+=slowo.charAt(licznik);
}
if(ascii=true){
    wynik+=(int)slowo.charAt(licznik);
}

to this

if(ascii==false){
    wynik+=slowo.charAt(licznik);
}
if(ascii==true){
    wynik+=(int)slowo.charAt(licznik);
}
Sign up to request clarification or add additional context in comments.

Comments

0

You may be using your option

FunnyString w3=new FunnyString("something",true);

and once you've set your boolean variable to true, you may be not re-sseting it, so it keeps the 'true' value.

Default value for boolean is 'false', so debug your code, I'm sure it will help you find where is your error.

If you want to, post here your entire code, and we will help you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.