I need to recognize if the base given by the user is correct

I need to recognize if the base given by the user is correct

Page 1 of 1

3 Replies - 646 Views - Last Post: 23 November 2010 - 08:16 PM Rate Topic: -----

#1 rockerboy23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 11-November 10

I need to recognize if the base given by the user is correct

Posted 23 November 2010 - 05:33 PM

this is what I have so far, please... someone help me with the rest :((((((

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


Is This A Good Question/Topic? 0
  • +

Replies To: I need to recognize if the base given by the user is correct

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: I need to recognize if the base given by the user is correct

Posted 23 November 2010 - 07:51 PM

Ouf a bit complicated for nothing :)

		Scanner input = new Scanner(System.in);
		System.out.println("Enter base: ");
		String line = input.nextLine();
		int base = line.charAt(0) - '0';
		if(base != 2 && base != 8) {
			System.out.println("Sorry only bases 2 and 8 permitted");
			return;
		}
		System.out.println("Enter number in base " + base + ": ");
		line = input.nextLine();
		// translate line in a serie of char
		char[] digit = line.toCharArray();
		int total = 0;
		for(int i = 0; i < digit.length; i++) {
			int num = digit[i] - '0';
			if(num >= base) {
				System.out.println("Digit '" + digit[i] + "' invalid in base " + base);
				return;
			}
			total *= base;
			total += num;
		}
		
		System.out.println("Decimal equivalent: " + total);
	}


Was This Post Helpful? 0
  • +
  • -

#3 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5234
  • View blog
  • Posts: 27,024
  • Joined: 10-May 07

Re: I need to recognize if the base given by the user is correct

Posted 23 November 2010 - 08:12 PM

** renamed title ** Sad faces & "need help ASAP* are not very descriptive.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: I need to recognize if the base given by the user is correct

Posted 23 November 2010 - 08:16 PM

Actually no reason to limit yourself to 2 and 8

		Scanner input = new Scanner(System.in);
		System.out.println("Enter base: ");
		String line = input.nextLine();
		int base = line.charAt(0) - '0';

		System.out.println("Enter number in base " + base + ": ");
		line = input.nextLine();
		// translate line in a serie of char
		char[] digit = line.toCharArray();
		int total = 0;
		for(int i = 0; i < digit.length; i++) {
			int num = digit[i] - '0';
			if(num >= base) {
				System.out.println("Digit '" + digit[i] + "' invalid in base " + base);
				return;
			}
			total *= base;
			total += num;
		}
		
		System.out.println("Decimal equivalent: " + total);
	}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1