Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Jesse Black   on Dec 31 In Java Category.

  
Question Answered By: Adel Fischer   on Dec 31

Beleive it or not, I have put together a program. I have worked on
it since before I posted it! It runs, and compiles. The thing is, I
am having trouble converting from numbers to letters. I am going to
post it here, with the hope someone can put a band aid on it for me,
or tell me how to take care of it. This has been a night mare. Since
I am in school, I cant afford to buy any more books! (I am poor this
time of year!) so I hung out in the bookstores and got segments of
code from differnt books, and somehow I come up with this! I have
commented out the troubled area, so if anyone can help me figure out
what the deal is, I would be greatful. Maybe I am off, but I beleive
this is right.

import java.util.*; //
import java.lang.*; //
import javax.swing.*; //


public class RemainderStack {

public static void main (String[] args) { // Begin main

String choice = "";
int quotient;
Integer Remainder = null; // Create integer object
Stack remainderStack = new stack  (); // Create a new,
empty stack

try { // Begin try

while (!(choice.equalsIgnoreCase("N"))) { // Begin while
loop

String remainderString = "";

String N = JOptionPane.showInputDialog(
"Enter a decimal number. \n" + "Enter a negative number
to exit. \n");

if (Integer.parseInt(N) < 0)
System.exit(0);


String B = JOptionPane.showInputDialog(
"Enter a number base (between 2 - 16)");

if (Integer.parseInt(B) < 2 || Integer.parseInt(B) > 16)
{ // Begin if

String message = "Invalid base number. \n" + "Restart
program.";

JOptionPane.showMessageDialog(null, message, "Error!",
JOptionPane.ERROR_MESSAGE);

System.exit(0);

} // End if

int decimalNumber = Integer.parseInt(N);
int baseNumber = Integer.parseInt(B);

quotient = (int) decimalNumber / baseNumber;

Remainder = new Integer (decimalNumber % baseNumber);

remainderStack.push(Remainder);


while (quotient != 0) { // Begin while loop

Remainder = new Integer (quotient % baseNumber);

remainderStack.push(Remainder);

quotient = (int) quotient / baseNumber;

} // End while loop


while (! remainderStack.empty()) { // Begin while loop

/// if (Remainder = 10) Remainder = 065;

// if (Remainder = 11) Remainder = "B";

// if (Remainder = 12) Remainder = "C";

// if (Remainder = 13) Remainder = "D";

// if (Remainder = 14) Remainder = "E";

// if (Remainder = 15) Remainder = "F";

remainderString += remainderStack.pop();


} // End while


String message = "Base 10 number: " + decimalNumber + "\n"
+ "Number in base " + baseNumber + " is: " +
remainderString
+ "\n Continue (Y/N)?\n";

choice = JOptionPane.showInputDialog(null, message, "Number
Base Conversion",
JOptionPane.INFORMATION_MESSAGE);

} // End while loop

} // End try statement

catch (NullPointerException e) { // Catch if user presses
Cancel

System.exit(0);

} // End catch statement


catch(NumberFormatException e) { // Begin catch

String message = "Invalid number. \n" + "Restart program.";

JOptionPane.showMessageDialog(null, message, "Error!",
JOptionPane.ERROR_MESSAGE);

System.exit(0);

} // End catch statement


while (!(choice.equalsIgnoreCase("Y"))) { // Begin while loop

System.exit(0);

} // End while loop

} // End main

} // End class

Share: 

 

This Question has 8 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on where I can get some info on using stacks? Or get search suggestion and latest updates.


Tagged: