Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## Party Bits problem

Alright So I am working on the first problem of this assignment and I have hit a road block. When my answer prints out it puts both a 1 and a 0 on and I only want it to do wone or the other depending on whether or not the number of ones is even or odd.
This is my code
```public class message
{
private String parity1;
private int countOfOnes;
public message(String Parity1)
{
parity1 = Parity1;
int countOfOnes;

}

public String getParity()
{
return parity1;
}

public void setParity(String Parity1)
{
parity1 = Parity1;
}

public int countOnes()
{
String abc = parity1;
int countOfOnes = abc.length()-abc.replaceAll("1","").length();
return countOfOnes;
}

public String sendWithEvenParity()
{

if(countOfOnes %2 == 0)
{
parity1 = parity1 + "1";

}

else
{
parity1 = parity1 + "0";
}
return parity1;

}

public String sendWithOddParity()
{

if(countOfOnes%2 == 0 )
{
parity1 = parity1 + "0";
}
else
{   if(countOfOnes%2 == 1 )
parity1 = parity1 + "1";
}

return parity1;
}

public String toString()
{
return parity1;
}

}```

2. ## Re: Party Bits problem

Nowhere in your code do I see a print statement.

3. ## Re: Party Bits problem

It has a driver class that out puts the info
```public class parityFun
{
public static void main(String[]args)
{
message parity = new message("1111111");
message parity2 = new message("1111110");

System.out.println("Original Message: "+ parity);

parity.sendWithEvenParity();
parity.getParity();
System.out.println("\tWith even Parity: " + parity);

parity.sendWithOddParity();
parity.getParity();
System.out.println("\tWith odd Parity: " + parity);

System.out.println("Original Message: "+ parity2);

parity2.sendWithEvenParity();
parity2.getParity();
System.out.println("\tWith even Parity: " + parity2);

parity2.sendWithOddParity();
parity2.getParity();
System.out.println("\tWith odd Parity: " + parity2);

}
}```

4. ## Re: Party Bits problem

So the error occurs in the code you didn't originally post! Do you think posting relevant information would make it easier for us to help you?

When you want to do one thing or another what kind of statement would you use?

5. ## Re: Party Bits problem

Well I assumed my error occurred in the other class not the driver class. I would use an if-else statement if I wanted to do one thing or another

6. ## Re: Party Bits problem

What happened when you tried?

7. ## Re: Party Bits problem

Original Message: 1111111
With even Parity: 11111111
With odd Parity: 111111110
Original Message: 1111110
With even Parity: 11111101
With odd Parity: 111111010

that is what happened the first output is correct but all others thereafter are incorrect I'm not sure why this is.