Results 1 to 5 of 5
Thread: Adding convertions
 07012010, 07:44 AM #1Member
 Join Date
 Jun 2010
 Posts
 34
 Rep Power
 0
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 = l1; 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 >
 07012010, 01:13 PM #2input not less than 2 and not more than 16 as base but my problem is how can i implement it inside my code.
how can i edit my code that it will ask the base of the first number to be inputted and checks if its valid
Ask for a number
check if the number if valid
Then what if its not valid?
 07012010, 02:49 PM #3Member
 Join Date
 Jun 2010
 Posts
 34
 Rep Power
 0
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 = l1; 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; } }
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
Java Code:Enter How many Numbers to be added: 2 Enter Original Base: 1: 1 17 its too low Enter Number 1:
and again it needs to be inputted 2x
 07012010, 02:54 PM #4
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.
 07012010, 03:31 PM #5
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,943
 Blog Entries
 7
 Rep Power
 22
Similar Threads

Problem with adding
By sanox in forum New To JavaReplies: 5Last Post: 09082009, 11:04 AM 
Adding
By natep67 in forum New To JavaReplies: 2Last Post: 04222009, 07:51 AM 
adding GUI to Inventory
By voyager91 in forum New To JavaReplies: 8Last Post: 02012009, 06:20 AM 
Help in adding some validation..
By tornado in forum New To JavaReplies: 5Last Post: 11302008, 02:36 AM 
Adding taglibs in JSP
By Java Tip in forum Java TipReplies: 0Last Post: 01142008, 12:43 AM
Bookmarks