Program To Convert A Base 10 Integer To Any Base 2 - 16
May 5, 2013
I am writing a program to convert a base 10 integer to any base 2-16. Here are the terms:"The method is to convert the decimal value the user selected into whatever base the user selected and print the converted value one place value at a time by using an index into an array of characters 0-9 amd A-F that is initialized to contain those characters.In the method you will find the largest place value (i.e. power of the base) that will divide into the decimal number.
Then you can set up a loop that will operate from that power down to and including the 0th power to determine how many times each place value goes into the decimal number. Using the loop counter, index into the character array to print the character that corresponds to the quotient number and then subtract the product of the place value and the quotient from the decimal number. With the power (loop index) decreased, repeat until you finish the 0th place value. Here's what I have so far:
import java.io.*;
import java.util.Scanner;
public class ConvertIt
{//start program
public static void main(String[] args)
{//start main
Scanner input = new Scanner(System.in);
System.out.println("Enter a positive integer from 0 to 10000.");
int number = input.nextInt();
So i'm writing a for loop to convert any number entered to base 10 with any base provided as well. My code does not work because I need a way to reverse the code order, so the new number is printed correctly with the given base. My code so far:
public static void main (String[] args) { Scanner kb = new Scanner (System.in); System.out.print("Enter a number :: "); int numOriginal = kb.nextInt(); System.out.print("Enter a base :: "); int base = kb.nextInt();
[Code] .....
newBase has a problem with how it calculates the new number, looking for correct newBase code for conversion?
I know how to do this program it is just not coming to me. The whole point is to calculate and display the base (base-2 or binary, base-8 or octal and base-16 or hexadecimal) in representation of 'N'. The symbols A, B, C, D, E, F should display 10, 11, 12, 13, 14, and 15 in hexadecimal system.
import java.io.*; import java.util.Scanner; public class ChangeBase { public static void main(String[]args) { double num;
The idea behind this program is that the program prompts the user to input an initial base (2-36), which checks to ensure that it is a valid int, then asks for a number to convert (which is taken as a String), then it asks which desired base the user would like to convert said number to. I have a basic program that is not complete, but allows me to do a few conversions using convertTo. I believe that I am going to have abandon this method and try mathematically converting every number. This leads me to what I can and cannot do. I am unable to use the initialBase as a condition to know what kind of number i an converting. For instance, I don't know how to make program know that if "2" is the initialBase, that that means that the String is a binary number. THAT is what I'm having problems with.
Here is my initial program that has a few things that are copied and pasted from other bits of my code in my program:
XML Code: Url...
I have broken down what I (think I) need to do here: Check to see if the input base is 2, 8, 10, 16, or 32. Hint: Put the possible bases in an array, and check the input base against the array. Check to see if the input number is valid for the base. Hint: Create a String "0123456789ABC...V" and compare each input character with the first "base" characters of the String. Check to see if the output base is 2, 8, 10, 16, or 32. Hint: Use the same possible bases array you used in step 1 to verify the input base.Check to see if the input base is equal to the output base. If so, print the input number. Perform a conversion from the input base to base 10. Perform a conversion from base 10 to the output base. You do this in 2 steps because it's easier to check each conversion separately.Output the converted number.
I am struggling getting my java app to open a console window on either MacOS or windows and run a command. On windows I can get the cmd.exe program to open, but it won't execute the command. On MacOS, I cannot get it to even open the terminal.
String run = "c: s34bil.exe elap5.exe" + in + rst + out; //in, rst, out are parameters for the relpa5.exe file. try { Runtime rt = Runtime.getRuntime(); rt.exec(new String[]{"cmd.exe","/c",run,"start"}); } catch (IOException ex) { Logger.getLogger(issrsUI.class.getName()).log(Level.SEVERE, null, ex); }
I had to write a program for class to request user input for base salary, number of years worked, and total sales. Then use the data to find out the employee's paycheck when including a bonus. I have a few issues with the code, as I have one bug, then it won't calculate anything. what I'm missing?
package chapterone; import java.util.Scanner; public class Examplelab { static Scanner console = new Scanner(System.in); public static void main(String[] args){ double baseSalary; double noOfServiceYears; double totalSales;
which works fine. The only issue is that I had to place this in the login page. Is there a way I can only set the path to the base url upon server start up?
I have been developing what I intent to be a base class for several forms that will allow the user for adding / editing / deleteing records. These records could be customers, products, suppliers etc.
I have designed a basic form that has an add, edit and delete button. For the add button, I would want to clear all the values in all of the controls (textboxes, combox etc) in preperation for adding a new record.
My question is this. Is this something I should do in the base class OR should it be handled in the classes that will extend from the base class? Perhaps if the controls were datalinked to the data they will clear themselves (I haven't got that far yet so I dont know). I thought maybe I could write code in the base class that could loop through all of the controls and call this from the extended classes.
I am writing a program that converts any base 10 number to bases 2-16. I have the code for everything up through hexadecimal conversion, for that requires the use of letters. I understand an array list may be of use however I do not understand how to use that in this code. Below is what I have so far
import java.util.*; import java.io.*; public class convertBase { public static void main(String[] args) { int base; int number; String newNum;
[code].....
I commented out the hexadecimal portions.How would I go about coding for letters?
This is what I need to do: Write a method called scientific that accepts two real numbers as parameters for a base and an exponent and computes the base times 10 to the exponent, as seen in scientific notation. For example, the call of scientific(6.23, 5.0) would return 623000.0 and the call of scientific(1.9, -2.0) would return 0.019.
This is the method I have written:
public double scientific(double num1, double num2); { double base = num1; double exp = num2; double scientific = base * Math.pow(10, exp); System.out.println(scientific); return scientific; }
These are the following error messages I received when trying to compile the code:
Line 4 missing method body, or declare abstract missing method body, or declare abstract public double scientific(double num1, double num2);
I'm working on this program for a class to create objects of a commissioned employee and union employee. Everything seems to work ok, but when I run my final pay calculation, one of my getter functions will not pass the variable for the pay into a class specific variable called check. here is the code in this function.
When I run, this function works as it returns the value when i print it to test.
however, when I do the part that says check = getWeekPay() above, it doesn't change the check variable. The only thing I have on the check variable is the dues taken out of it at the end, so it ends up being a negative number.
I have a similar problem with the other derived class's check variable. Both classes have the same variable as private but one is check the other checkC.
I have to make a programm where the user gives you the bank sorting code and the account number and you give him the IBAN. That was so far no problem and I was done within minutes except of one thing that I simply can't figure out even though im trying since weeks. At some point I have to convert a string to integer. My research told me its with parseInt() and I dont get a syntax error when I compile my programm (using BlueJ). But when executing the programm stops and gives me some weird bug message. Here is code and bug message:
Java Code:
public class IBAN { public IBAN(String Bankleitzahl, String Kontonummer) { Bankleitzahl=Bankleitzahl.replace(" ",""); // Die Leerzeichen werden entfernt int Anzahl=Bankleitzahl.length(); // Auf der Variabel Anzahl wird die Anzahl der Zeichen von der Bankleitzahl gespeichert
If I use the class DecimalFormat to format long number, how can I convert it back to integer?
DecimalFormat longFormat = new DecimalFormat("#,###"); long testLong=11000; String strLong=longFormat.format(testLong); System.out.println("NUM : " + strLong); //Assume that at this point I don't have //testLong, I have only the strLong value... long newLong=Long.parseLong(strLong) * 2; //java.lang.NumberFormatException: For input string: "11,000
I am working with a JFormattedTextField. After adding the text of the FormattedTextField to an LinkedList i want to read it out and sum it up. So I have a problem to convert the String to and integer...
Example:
23.00 - to 23.00 + 11.00 - to 11.00 --> 34.00
I have tried it with splitting the string but it didn't work. How to do it?
So I have to write a java program that converts hexadecimals to decimals without using the whole "integer.parseInt(AB1, 16)" method. I tried looking up how to do this but every forum/site I went to used this same method.
I am using a static method to convert a string to an Integer object. Next using a instance method to convert Integer object to an int.
Compiler is giving me two "cannot find symbol" errors:
One pointing to the dot operator between "Integer.valueOf(s)"
The other pointing to the dot operator between "obj.intValue()"
I have latest JDK installed: jdk-7u51-windows-x64.exe
Looks like JCL installed correctly with rt.jar file located in "lib" directory under "Program Files"
Following is source code:
Java Code:
public class StringToInt { public static void main (String args []) { String s = "125"; Integer obj = Integer.valueOf(s); int i = obj.intValue(); i += 10; System.out.println(i); } } mh_sh_highlight_all('java');