I need the part in the middle which checks if the number given by user is in the base the user specified.
**
* Dea Dodi
*
* 11/17/10
* This program is supposed to ask the user to enter a number and then specify what base it is in.
* The number will be read as a string and the program will check if it's the specified base. If it is the base
* the user specified, the number will be converted to base ten. If not, the user will have to enter the right
* base number and it will then convert the number to base ten.
*/
import java.util.Scanner;
class BaseTenP2 {
public static void main (String [ ] args) {
int baseTen = 0; // will contain actual base two conversion to base ten
String userNumber; // the number entered by the user
int index = 0 ; // stores index
int digits ;
int exponent;
int len; // store length of the number entered by user
int userBase; // base first entered by user
String base;
char charNum;
int intNum;
Scanner input = new Scanner (System.in);
System.out.println ("Enter a base ");
base = input.nextLine ( );
userBase = base.charAt ( 0) -'0';
System.out.println ("Enter the number in base " + userBase );
userNumber = input.nextLine ( );
len= userNumber.length ( ); // calculates length of userNumber
while ( index < userBase) { // haven't reached the end
if (userNumber.charAt(index) >= userBase) {// numbers higher or equal to the user base
System.out.println (" The base you entered is incorrect, please try again. ");
base = input.nextLine ( );
}
// index = 0 ; // start at beginning of new input
// len= userBase.length ( ); // new number has new length
// } // numbers higher or equal to the user base
// else if (userNumber.charAt (index) < userBase) { // when user enters right base
// index ++; // move to index of next character
// } // when user enters right base
// } // haven't reached the end
exponent = len -1; // first exponent is same as last index
for (index= 0; index < userNumber.length(); index++) {// go through userNumber
charNum =userNumber.charAt(index);
intNum= charNum - '0' ;
digits += (int) Math.pow (userBase, exponent) * intNum; // multiply each one with its base value
baseTen += digits; // add all of the base values of one
exponent --; // decrease exponent
} // go through baseTwo
System.out.println ( userNumber + " in base " +userBase + " is " + baseTen + " in base ten ") ;// report sum to user as an int
}
} // end main
} // end class
This post has been edited by macosxnerd101: 23 November 2010 - 07:30 PM
Reason for edit:: Please use code tags

New Topic/Question
Reply




MultiQuote



|