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 8601499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:51:31+00:00 2026-06-12T01:51:31+00:00

The error in this program is the while statement in the main method. It

  • 0

The error in this program is the while statement in the main method. It was working before in the sense that when I invalid input value it would stop and exit program and when I input proper value it would loop through the while statement once but suddenly after more work too my code it loops infinity for any input below 1025. Ignore the sloppiness of the output of the program as I am still editing that, the main problem is why it does not stop looping in the while statement.

import java.util.Scanner;

public class DNS
{
    public static void main(String[] args)
    {
        int y;
        Scanner input = new Scanner( System.in);

        System.out.println("java DisplayNumberSystems");
        System.out.println("Enter a decimal value to display to: ");
        y = input.nextInt();

        while(y !=1025)
        {

            for(int x=0; x <=y; x++)
            {
                System.out.println("Decimal");
                System.out.println(x);
            }


            for(int i=0; i <=y; i++)
            {
                System.out.println("");
                System.out.println("Binary");
                convertToBinary(i);
            }

            System.out.println("");             

            for(int z=0; z <=y; z++)
            {
                System.out.println("Hex");
                convertToHex(z);
            }

            System.out.println("");

            for(int d=0; d <=y; d++)
            {
                System.out.println("Octal");
                convertToOctal(d);
                System.out.println("");
            }

        }
    }

    public static void convertToBinary(int x)
    {

        if(x >0)
        {

            convertToBinary(x/2);
            System.out.printf(x%2 + "");

        }

    }

    public static void convertToOctal(int x)
    {
        int rem; 

        while(x >0)
        {
            rem = x%8;
            System.out.printf("%d", rem);
            x=x/8;
        }
    }

    public static void convertToHex(int x)
    {
        int rem;

        while(x >0)
        {

        rem = x%16;
            switch(rem)
            {
                case 1: 
                    System.out.printf("1");
                    break;

                case 2: 
                    System.out.printf("2");
                    break;

                case 3:
                    System.out.printf("3");
                    break;

                case 4: 
                    System.out.printf("4");
                    break;

                case 5:
                    System.out.printf("5");
                    break;

                case 6:
                    System.out.printf("6");
                    break;

                case 7: 
                    System.out.printf("7");
                    break;

                case 8:
                    System.out.printf("8");
                    break;

                case 9:
                    System.out.printf("9");
                    break;

                case 10: 
                    System.out.printf("A");
                    break;
                case 11: 
                    System.out.printf("B");
                    break;

                case 12: 
                    System.out.printf("C");
                    break;

                case 13: 
                    System.out.printf("D");
                    break;

                case 14: 
                    System.out.printf("E");
                    break;

                case 15: 
                    System.out.printf("F");
                    break;
            }
            x=x/16;
        }
        System.out.println("");

    }

}
  • 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-06-12T01:51:32+00:00Added an answer on June 12, 2026 at 1:51 am

    Why do you need a while loop if you are going to execute it only once ? Consider if instead.

    Never mind, how about breaking the while loop ?
    You can use break; wherever applicable, to stop execution of a loop.

    For example :

    while (y != 1025) {
    
        for (int x = 0; x <= y; x++) {
            System.out.println("Decimal");
            System.out.println(x);
        }
    
        for (int i = 0; i <= y; i++) {
            System.out.println("");
            System.out.println("Binary");
            convertToBinary(i);
        }
    
        System.out.println("");
    
        for (int z = 0; z <= y; z++) {
            System.out.println("Hex");
            convertToHex(z);
        }
    
        System.out.println("");
    
        for (int d = 0; d <= y; d++) {
            System.out.println("Octal");
            convertToOctal(d);
            System.out.println("");
        }
        break;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Writing a python program, and I came up with this error while using the
Everytime I run the program, this mysterious error pops up saying that I have
When I try to compile this program , I get an unreachable statement error
Im trying to use Date Formatters (NSDateFormatter), but I keep getting this error: Program
I am getting ERROR when I am running this program at the line static
I got this error Unhandled exception at 0x0049b946 in Program.exe: 0xC0000005: Access violation reading
I encounter error like this in my sql in running my program, How can
I am trying to run a program and SOMETIMES I'm getting this error when
My iPhone app started logging this error: lo->hi recycling invariant violated! followed by: Program
when I'm trying to compile my c program it gives me this error warning:

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.