Need help with a change of base program - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Need help with a change of base program

weziwweziw Posts: 20Member
My program does not display the first 15 lines of answers . I can't seem to find the bug;
[code]
I need help with the display of my programs output. The program works fine as far as converting, however I can't catch my mistake as far as out putting the results to the screen. All numbers from 1 to 16 are missing. The code:

[code]
/*
* Name: Andrew Wanda
* Description: Question 5_Mark. Program to write out a table of values in binary, octal and hexadecimal
* when given the decimal equivelent
*/
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;

public class quest5_Mark
{

public static void main(String[] args)
{
String stDecimalF, //First decimal
stDecimalL, //Last decimal in the range
stBinOut = "", //The binary output result
stOctOut = "", //The octal result
stHexOut = "", //The hexadecimal result
stTemp = "";

int firstDec, lastDec;

//Create the output area
JTextArea baseTable = new JTextArea(15,35);
JScrollPane scrollTable = new JScrollPane(baseTable);

//Initial line in the base table
baseTable.setText("Decimal Binary Octal Hexadecimal
");

//Get the integer range limits
stDecimalF = JOptionPane.showInputDialog("Enter first number in the range: ");
firstDec = Integer.parseInt(stDecimalF);

stDecimalL = JOptionPane.showInputDialog("Enter the last number: ");
lastDec = Integer.parseInt(stDecimalL);

for(int i = firstDec; i <= lastDec; i++)
{
//****************convert to binary*********************
int tmp = i;
while(tmp != 0)
{
stTemp += tmp % 2;
tmp /= 2;
}

//Reverse the binary string
for(int n = stTemp.length(); n > 0 ; n--)
{
stBinOut += stTemp.substring(n-1,n);
}//for
//Reset the string
stTemp = "";

//*****************convert to octal***********************
int tmpOct = i;
while(tmpOct != 0)
{
stTemp += tmpOct % 8;
tmpOct /= 8;
}

//Reverse the octal string
for(int n = stTemp.length(); n > 0; n--)
{
stOctOut += stTemp.substring(n-1,n);
}

//Reset the temporary string
stTemp = "";

//*************convert to hexadecimal*********************
int tmpHex = i;
int hexRemainder;
while(tmpHex != 0)
{
hexRemainder = tmpHex % 16;

switch(hexRemainder)
{
case 10 : stTemp += "A"; break;
case 11 : stTemp += "B"; break;
case 12 : stTemp += "C"; break;
case 13 : stTemp += "D"; break;
case 14 : stTemp += "E"; break;
case 15 : stTemp += "F"; break;
default : stTemp += hexRemainder;
}//end switch()
tmpHex /= 16;

}//end while

//Reverse the hexadecimal string for numbers greater than 16
if(i >= 16)
{
for(int n = stTemp.length(); n> 0; n--)
{
stHexOut += stTemp.substring(n-1,n);
}
}//end if

//Append to the output text area
baseTable.append(i +" "+ stBinOut+" "+ stOctOut+" "+ stHexOut+ "
" );

//Reset the all string variables
stTemp = "";
stBinOut = "";
stOctOut = "";
stHexOut = "";

}//end for

//Show the result
JOptionPane.showMessageDialog(null,scrollTable,"Backward binary result",
JOptionPane.INFORMATION_MESSAGE);

}//end main

}//end quest5_mark
[/code]
[/code]

Comments

  • arb123arb123 Posts: 696Member
    Use functions for reuse and make unit testing easier.

    [code]
    private static char toHexChar(int n) {
    if (n < 0 || n > 15) {
    throw new IllegalArgumentException("" + n);
    }

    if (n < 10) {
    return (char) ('0' + n);
    } else {
    return (char) ('A' + n - 10);
    }
    }

    public static String toHex(final int i) {
    String stHexOut = "";
    final int base = 16;
    int n = i;
    while (n > 0) {
    stHexOut = toHexChar(n % base) + stHexOut;
    n = n / base;
    }
    return stHexOut;
    }
    [/code]

    ---------------------------------
    [size=1](Its just my sig)
    HOWTO ask questions: http://catb.org/~esr/faqs/smart-questions.html[/size]

Sign In or Register to comment.