Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a programming assignment (in java, I use net beans dos to write this). This assignment needs to show the binary conversion as listed within the code. The issue is, the output displays the Base 2, Base 5, Base 7, and Base 8 conversions backwards/ in the wrong order and gives a weird long dialog message (PLEASE TEST THIS IN NETBEANS FIRST SO YOU CAN SEE THE ISSUE). So could someone take a look at what I have now, and fix it to where it displays the assignment correctly in the output. Thanks. Please also show the output.


Java
package base.project;
import javax.swing.*;
public class BaseProject {
	public static String output = "";
	private static void convertobase(int n, int i){
		output += "\n the number " + n + " converted to base " + i;
		while (n > 0){
			output += " " + n%i;
			n = n / i;

		}

		output += "\n is " + output;
		JOptionPane.showMessageDialog(null, output);


	}
	public static void main(String[] args) {
		while (true){
			String input = JOptionPane.showInputDialog("enter any integer number \n");
			int n = Integer.parseInt(input);
			String in = JOptionPane.showInputDialog("\n Make the folowing selection"
			+ "\n to convert to decimal number"
			+ "\n2 to conver to base 2"
			+"\n5 to convert to base 5"
			+ "n7 to conver to base 7"
			+ "\n8 to convert to base 8");
			int selection = Integer.parseInt(in);
			switch(selection){
				case 2:
				convertobase(n, 2);
				break;
				case 5:
				convertobase(n, 5);
				break;
				case 7:
				convertobase(n, 7);
				break;
				case 8:
				convertobase(n, 8);
				break;
			}

		}
	}
}


What I have tried:

I have completed the assignment however it's not working properly.
Posted
Updated 21-Apr-16 11:20am
v3
Comments
Richard MacCutchan 22-Apr-16 3:29am    
Why not try desk checking the logic in your convertobase method? The error is obvious.

1 solution

I think it is time for you to stop guessing what your code is doing. It is time to see your code executing and ensuring that it does what you expect.

The debugger is your friend. It will show you what your code is really doing.
Follow the execution step by step, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

Pay special attention at the place where yu build the answer.
If the answer is in reverse order, you build it in reverse order !
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900