Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5845095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:23:10+00:00 2026-05-22T12:23:10+00:00

import java.util.Scanner; public class Test { private static int decimalNum = 0; private static

  • 0
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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T12:23:10+00:00Added an answer on May 22, 2026 at 12:23 pm

    I think your loop would be better off like this: (I’ve remove some reduncant checks on ‘i’ and simplified the checking for ‘maxBlock’):

    for(int i = 0; i < s.length(); ++i) {
        if(s.charAt(i) == '1') {
            // in case we were just keeping track of 0's
            maxBlock = Math.max(maxBlock, temp0Count);
            temp0Count = 0;
    
            temp1Count++;
        }
        else {
            // in case we were just keeping track of 1's
            maxBlock = Math.max(maxBlock, temp1Count);
            temp1Count = 0;
    
            temp0Count++;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

import java.util.Arrays; public class Test { public static void main(String... args) { String[] strings
public static void check(){ String name; System.out.println(Enter Customer Name to CHECK RESERVATION ticket for
Using the standard java logging API (import java.util.logging.Logger), after the construction: Logger l =
Can you please explain why it is possible to do: import java.util.ArrayList; import java.util.List;
Could someone please explain what the for loop in this class does? Specifically the
I have made an object named Skill and the main program. Skill : import
I am asked to print multiples of 2 only with a never ending loop.
I'm trying to write a program that reads 2 numbers from the user and
I am trying to generate an array with N integer values between 0 and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.