It looks like you're new here. If you want to get involved, click one of these buttons!
import java.util.Scanner;
public class Test {
private static int decimalNum = 0;
private static String binary = "";
private static void getInput() {
Scanner sc = new Scanner(System.in);
System.out.println("Please type in a number");
decimalNum = sc.nextInt();
}
private static void convert() {
int decimalNumber = decimalNum;
String binaryNumber;
if (decimalNumber <= 0)
System.out.println("ERROR: entered integer is nonpositive.");
else {
binaryNumber = "";
while (decimalNumber != 0) {
// add spaces to separate 4-digit groups
if (binaryNumber.length() % 5 == 0)
binaryNumber = "" + binaryNumber;
// extract last digit in binary representation
// and add it to binaryNumber
binaryNumber = (decimalNumber % 2) + binaryNumber;
// cut last digit in binary representation
decimalNumber /= 2;
}
binary = binaryNumber;
System.out.println("Binary: " + binaryNumber);
}
}
public static void count()
{
String s = binary + "";
System.out.println("Binary number: " + s);
int temp1Block = 0;
int temp0Block = 0;
int maxBlock = 0;
for(int i=0; i < s.length(); i++)
{
if((s.charAt(i) == '1') && (i < s.length()))
{
temp0Block = 0;
temp1Block++;
}
if((s.charAt(i) == '0') && (i < s.length()))
{
temp1Block = 0;
temp0Block++;
}
}
if(maxBlock < temp0Block)
{
maxBlock = temp0Block;
}
if(maxBlock < temp1Block)
{
maxBlock = temp1Block;
}
System.out.println("Maxblock " + maxBlock);
}
public static void main(String[] args)
{
getInput();
convert();
count();
}
}
I am resetting the tempBlocks when I should not be. And can someone help me format my code please, I don't know how to put the code tags.
Guild Wars 2's 50 minutes game play video:
http://n4g.com/news/592585/guild-wars-2-50-minutes-of-pure-gameplay
Everything We Know about GW2:
http://www.mmorpg.com/discussion2.cfm/thread/287180/page/1
Comments
for(int i=0; i < s.length(); i++)
{
if((s.charAt(i) == '1') && (i < s.length()))
{
temp0Block = 0;
temp1Block++;
if (maxBlock < temp1Block)
maxBock = temp1Block;
}
else if((s.charAt(i) == '0') && (i < s.length()))
{
temp1Block = 0;
temp0Block++;
if (maxBlock < temp0Block)
maxBlock = temp0Block;
}
}
You were not checking those conditions every time. So, you would go through the entire string and as soon as you hit a new character, you would throw away your previous count.
So, if you had:
1110
Your count would be 1. Since, you threw away the 3.
Also, use Else If where needed. It's not required, but it's proper coding style.
Got a Game Idea? Look Here!
Thank you! It is working. I thought I had those but I guess I must have done something wrong with the >, <.
Guild Wars 2's 50 minutes game play video:
http://n4g.com/news/592585/guild-wars-2-50-minutes-of-pure-gameplay
Everything We Know about GW2:
http://www.mmorpg.com/discussion2.cfm/thread/287180/page/1