Results 1 to 5 of 5
  1. #1
    Syfer is offline Member
    Join Date
    Jun 2010
    Posts
    34
    Rep Power
    0

    Default Adding convertions

    Java Code:
    
    package syfah;
    import java.util.Scanner;
    
    public class Adding{
               public static int convert(char c){
                          if(c == 'A') return 10;
                          else if(c == 'B') return 11;
                          else if(c == 'C') return 12;
                          else if(c == 'D') return 13;
                          else if(c == 'E') return 14;
                          else if(c == 'F') return 15;
                        return c;
                    }
        public static void main(String [] args){
            Scanner kbd = new Scanner(System.in);
    
            System.out.print("Enter How many Numbers to be added: ");
            int inputting = Integer.parseInt(kbd.next());
            String [] num = new String[inputting];
            int []x = new int[inputting];
            int a = 0;
            int ans= 0;
            int z = 0;
            
            for(int i = 0; i<inputting; i++){
               System.out.print("Enter Number "+(i+1) + ": ");
                num[i]= kbd.next();
                
                int l = num[i].length();
                int v = 0;
                for(int j = l-1; j>=0; j--){
                    if(Character.isLetter(num[i].charAt(v))){
                         a = convert(num[i].charAt(v));
                    }
                       else
                        a = Integer.parseInt(Character.toString(num[i].charAt(v)));
                    z = z + (a*(int)Math.pow(x[i], j));
                    v++;
                }
               ans = ans + z;
                z = 0;
            }
            System.out.print("Enter Base of Sum: ");
            int Lastbase = Integer.parseInt(kbd.next());
            int x2 = ans;
            int t = 0;
            String LastAns = "";
            if( Lastbase >= 10){
                while(x2 != 0){
                    t = x2% Lastbase;
                    x2 = x2/ Lastbase;
                    if(t >= 10){
                       if(t == 10)
                            LastAns = "A" + LastAns;
                        else if(t == 11)
                            LastAns = "B" + LastAns;
                        else if(t == 12)
                            LastAns= "C" + LastAns;
                        else if(t == 13)
                            LastAns = "D" + LastAns;
                        else if(t == 14)
                            LastAns = "E" + LastAns;
                        else if(t == 15)
                            LastAns = "F" + LastAns;
                    }
                    else
                        LastAns = Integer.toString(t) +LastAns;
                }
            }
            else{
                while(x2 != 0){
                    t = x2% Lastbase;
                    x2 = x2/ Lastbase;
                   LastAns = Integer.toString(t) + LastAns;
                }
            }
            String output = "Summation of the Following Numbers:";
            for(int i = 0; i < inputting; i++){
                output = output +"\n"+num[i]+" base "+x[i];
            }
            output = output+"\nSum = "+LastAns;
           System.out.println(output+" with base "+ Lastbase);
        }
    
    
    }



    Problems:

    1. well i know how to put restriction that the user should only input not less than 2 and not more than 16 as base but my problem is how can i implement it inside my code.

    2. how can i edit my code that it will ask the base of the first number to be inputted
    and checks if its valid

    ex.
    i input originalbase of: 2
    so if i inputted 1st number to be added like this: 10129223
    it will make the user to input a new 1st number to be added to the 2nd and so on.

    or simply it check if it matches the base inputted.

    the desired out put is kinda like this:

    Java Code:
    Enter How many Numbers to be added: 3
    Enter Number 1: f5
    Enter Original Base: 1: 16
    <--- and so on ---->

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,431
    Rep Power
    25

    Default

    input not less than 2 and not more than 16 as base but my problem is how can i implement it inside my code.
    You can do that with an if(condition) test. where the condition tests if the number is in the correct range.
    how can i edit my code that it will ask the base of the first number to be inputted and checks if its valid
    Why can't you do just that:
    Ask for a number
    check if the number if valid

    Then what if its not valid?

  3. #3
    Syfer is offline Member
    Join Date
    Jun 2010
    Posts
    34
    Rep Power
    0

    Default

    ok let me first fix it step by step here..
    i already coded for the restriction of bases entered
    to restrict the less than 2 or greater than 16
    but somehow i kinda fail
    Java Code:
     int originalBase = -2;
            while(originalBase <2 || originalBase >16){
                for(int i = 0 ; i< inputting; i++){
                System.out.println("Enter Original Base: "+(i+1) + ": ");
                originalBase = Integer.parseInt(kbd.next());
                x[i]= Integer.parseInt(kbd.next());
                if(originalBase <2){
                    System.out.println("its too low");
                }else if(originalBase >16){
                    System.out.println("its too high");
    
                }
               System.out.println("Enter Number "+(i+1) + ": ");
                num[i]= kbd.next();
               
    
    
                int l = num[i].length();
                int v = 0;
                for(int j = l-1; j>=0; j--){
                    if(Character.isLetter(num[i].charAt(v))){
                         a = convert(num[i].charAt(v));
                    }
                       else
                        a = Integer.parseInt(Character.toString(num[i].charAt(v)));
                    z = z + (a*(int)Math.pow(x[i], j));
                    v++;
                }
               ans = ans + z;
                z = 0;
            }
            }
    the output was like this
    if i inputted the right one it needs 2x to move to inputting the number to be added
    Java Code:
    Enter How many Numbers to be added: 
    2
    Enter Original Base: 1: 
    15
    15
    Enter Number 1: 
    2321
    Enter Original Base: 2: 
    15
    15
    Enter Number 2: 
    2321
    Enter Base of Sum: 
    16
    Summation of the Following Numbers:
    2321 base 15
    2321 base 15
    Sum = 3A40 with base 16
    and this is the output if i inputted the wrong one
    Java Code:
    Enter How many Numbers to be added: 
    2
    Enter Original Base: 1: 
    1
    17
    its too low
    Enter Number 1:
    instead of asking the user again since its too high or too low it goes straight in asking the number to be added
    and again it needs to be inputted 2x

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,431
    Rep Power
    25

    Default

    Have you tried designing your program using something like a flowchart to work out the logic BEFORE writing the code?
    Your problem seems to be a logic problem. You need to work out your logic BEFORE writing the code.
    Take a piece of paper and a pencil and design the logic THEN write the code.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,528
    Blog Entries
    7
    Rep Power
    20

    Default

    Doesn't this thread belong in the 'New To Java' forum? imho radix conversion is CE 101.

    kind regards,

    Jos

Similar Threads

  1. Problem with adding
    By sanox in forum New To Java
    Replies: 5
    Last Post: 09-08-2009, 11:04 AM
  2. Adding
    By natep67 in forum New To Java
    Replies: 2
    Last Post: 04-22-2009, 07:51 AM
  3. adding GUI to Inventory
    By voyager91 in forum New To Java
    Replies: 8
    Last Post: 02-01-2009, 05:20 AM
  4. Help in adding some validation..
    By tornado in forum New To Java
    Replies: 5
    Last Post: 11-30-2008, 01:36 AM
  5. Adding taglibs in JSP
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-13-2008, 11:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •