Results 1 to 20 of 39
Thread: Assistance about this.
 06192010, 07:27 AM #1Member
 Join Date
 Jun 2010
 Posts
 34
 Rep Power
 0
Assistance about this.
the output
Java Code:as for octal: Input Orginal Base : 8 Input a number to be converted: 278 Input a number again: as for binary: Input Orginal Base: 2 Input a number : 123 Input a number again: as for hexa: Input Orginal Base: 16 Input a number : 123g Input a number again:
1.since its 278
the program will ask the number to be converted again
since it exceeded same goes to hexa if it goes to G it will
ask the same question again also with binary if the user inputted 1234
and he chooses 2 as his original base which is a binary base it will ask the question again.
2. and if i choose 15 as a original base therefore the F shouldnt be accepted
until it reaches 10 it goes to 1
and incase that the user inputs 15 as its orginal base and the user inputted a F43 the program will ask the user to input a new number to be converted again until the max is only C.
main code:
Java Code:do { System.out.println("Enter number to convert: "); String number = kbd.next(); kbd.nextLine(); Pattern p = Pattern.compile("[09afAF]"); Matcher m = p.matcher(number); if (m.find()) { } else do { System.out.println("Enter number to convert Again: "); number = kbd.next(); kbd.nextLine(); m = p.matcher(number); } while (!m.find()); int originalBase = 2; while (originalBase < 2  originalBase > 16) { System.out.println("Enter the Orginal Base: "); originalBase = Integer.parseInt(kbd.next()); kbd.nextLine(); if (originalBase < 2) { System.out.println("Too low"); } else if (originalBase > 16) { System.out.println("Too high"); } } BigInteger n = new BigInteger(number, originalBase); int base = 2; while (base < 2  base > 16) { System.out.println("Enter base FROM 2  16 ONLY: "); base = Integer.parseInt(kbd.next()); kbd.nextLine(); if (base < 2) { System.out.println("Too low"); } else if (base > 16) { System.out.println("Too high"); } } System.out.println("Result is:" + n.toString(base)); System.out.println("Do you want to try it again? [yes/no]: "); repeatChoice = kbd.nextLine(); } while (repeatChoice.equalsIgnoreCase("yes")); } }
the program should ask the orginal base first
before asking the number to be converted
so it can check if the number inputted matches the original base
my problem is how can i code it out and edit my previous codesLast edited by Syfer; 06192010 at 07:30 AM.
 06192010, 12:45 PM #2my problem is how can i code it out and edit my previous codes
 06192010, 01:15 PM #3Member
 Join Date
 Jun 2010
 Posts
 34
 Rep Power
 0
Here my previous or latest code
the first to be asked is the Number to be converted and after it asked the original base
my problem is
how can i change my code to
this kind of output
Java Code:Input OriginalBase: 7 Input Number to be converted: 78
so the original base and the number to be converted should be match
same goes to the hexa and binary .. repeating it until the number to be converted matches the orginal base
my code is posted above.

Original poster, I have two suggestions given in the spirit of helping you to get better help here:
1) Try to use a more helpful thread title. The title should be a headline telling us just what your problem is, such as "Getting prompt to repeat until valid number entered", or something like that. "Assistance about this" tells us zip about what your problem is.
2) You have a reply in your previous thread that has not been replied to or acknowledged, and it's always good form to reply to outstanding threads before asking a new question.
And in reply to your question, why not just simply swap code blocks so that you're running the block that requests the base first and then have the block that requests the number second? Or am I oversimplifying the problem.
Much luck.Last edited by Fubarable; 06192010 at 02:18 PM.
 06192010, 02:25 PM #5Member
 Join Date
 Jun 2010
 Posts
 34
 Rep Power
 0
Sorry about that i'll reply to it after posting this.
Java Code:System.out.println("Enter the Orginal Base from 2 16 ONLY~!!!!!!: "); int originalBase = Integer.parseInt(kbd.next()); kbd.nextLine(); System.out.println("Enter number to convert: "); String number = kbd.next(); kbd.nextLine(); Pattern p = Pattern.compile("[09afAF]"); Matcher m = p.matcher(number); if(m.find()) { }else do{ System.out.println("Enter number to convert Again: "); number = kbd.next(); kbd.nextLine(); m = p.matcher(number); }while(!m.find()); BigInteger n = new BigInteger (number, originalBase); int base = 2; while (base < 2  base > 16) { System.out.println("Enter base FROM 2  16 ONLY: "); base = Integer.parseInt(kbd.next()); kbd.nextLine(); if (base < 2) { System.out.println("Too low"); }else if (base > 16) { System.out.println("Too high"); } } System.out.println("Result is:" + n.toString(base)); } }
about asking first the originalbase and next is the convert number
now my last problem is
here:
if i inputted original base of 15
so the max number or string should be ae its 1
so it shouldn't accept 13F or F above specially G which is outside of hex's concern same goes to binary if i inputted original base of 2
so it means the number to be converted is between 10 only if it doesnt match the original base it will repeat to ask what number to be converted.

To figure out how to validate input when the base or radix can change, you might want to parse the in put using an overload of Integer.parseInt, the one that accepts two parameters, a String that needs to be parsed and an int that is the radix or base of the number. If you place this code into a try/catch(NumberFormatException nfe) block, you should be able to trap use of inappropriate numbers.
Have a look at the Integer API for more details: http://java.sun.com/javase/6/docs/ap...ring,%20int%29

For example,
Java Code:public class ParseExample { public static void main(String[] args) { String[] numberStrings = {"1", "2", "3", "5", "10", "AA", "1F", "4E"}; tryParse(numberStrings, 8); tryParse(numberStrings, 15); tryParse(numberStrings, 16); } public static void tryParse(String[] numberStrings, int base) { for (int i = 0; i < numberStrings.length; i++) { try { String numbString = numberStrings[i]; System.out.println("Attempting to parse " + numbString + " with base " + base); int number = Integer.parseInt(numbString, base); System.out.println("Parse successful. Number is: " + number); } catch (NumberFormatException e) { System.out.println("Parse not successful"); } System.out.println(); } } }
 06192010, 06:22 PM #8Member
 Join Date
 Jun 2010
 Posts
 34
 Rep Power
 0
 06192010, 07:46 PM #9i still dont get it
 06202010, 03:59 AM #10Member
 Join Date
 Jun 2010
 Posts
 34
 Rep Power
 0
my Last problem might be is like this:
The Original Base should match the Inputted number
like for example
Original base : 7 < octal so the inputted number to be converted shouldn't exceed to 7
Original base :15 < so the maximum string is E since 16 is F its  1
so if it goes 14 therefore the maximum string is D
so if a USER inputted like this
Original base: 7
Input number to be converted: 68
the program will ask him again for new number to be converted since its invalid same goes to the other original bases.
 06202010, 01:11 PM #11so if a USER inputted like this
Original base: 7
Input number to be converted: 68
the program will ask him again for new number to be converted since its invalid
repeating it until the number to be converted matches the orginal base
Outside the loop you get the base.
Then enter the loop and stay there until the user enters a number that "matches the orginal base".
 06202010, 01:39 PM #12Member
 Join Date
 Jun 2010
 Posts
 34
 Rep Power
 0
the original base is 7
and if a user inputs 68
the user will suffer from error or error will occur
since there's no 8 in octa
so instead of committing error the program will use the user to input a new number that matches the original base of 7 and same goes to the other base
yes since the program will ask first the original base before the number to be converted.
 06202010, 02:40 PM #13
Is this the logic:
1Ask user for base number to use.
2Get base to use.
3Get number from user (that number must be of the base entered in 2)
4Test that number entered in 3 is of base given in 2
5If number is not correct base, go to step 3
6Convert number from step 3 from base given in 2 to base 10.
 06202010, 03:05 PM #14Member
 Join Date
 Jun 2010
 Posts
 34
 Rep Power
 0
 06202010, 03:37 PM #15
Have you solved your problem yet?
If not, could you explain what it is now?
 06202010, 03:47 PM #16Member
 Join Date
 Jun 2010
 Posts
 34
 Rep Power
 0
i havent solved it XD
still the same ..
instead of exiting after inputting a invalid number that doesnt match the original base
it will let the user repeat and input a new number to be converted
like this
First Attempt:
Input a Original base: 2
Input number to be converted: 99
since its 99 and the original base is binary
the program will ask the user again until it input 11011 or a binary number
same goes to the other bases.
other info's are already posted and i think i explained it briefly xD
 06202010, 03:54 PM #17exiting after inputting a invalid number that doesnt match the original base
 06202010, 04:02 PM #18Member
 Join Date
 Jun 2010
 Posts
 34
 Rep Power
 0
the update code is at #5 post
you can run it and try this to input
Java Code:Input a Original base: 2 Input a Number to be converted : 1202
and what i wanted is it wont exit you but it ask you again to input a number to be converted  same goes to the other bases
 06202010, 04:48 PM #191Ask user for base number to use.
2Get base to use.
3Get number from user (that number must be of the base entered in 2)
4Test that number entered in 3 is of base given in 2
5If number is not correct base, go to step 3
6Convert number from step 3 from base given in 2 to base 10.
Java Code:import java.math.*; public class TestProblem1 { public static void main(String[] args) { System.out.println("Enter the Orginal Base from 2 16 ONLY~!!!!!!: 2"); int originalBase = 2; //Integer.parseInt(kbd.next()); System.out.println("Enter number to convert: 1202"); String number = "1202"; //kbd.next(); boolean getNumber = true; while(getNumber) { try{ int val = Integer.parseInt(number, originalBase); getNumber = false; // exit }catch(NumberFormatException fre) { System.out.println("Enter number to convert Again: 1111"); number = "1111"; //kbd.next(); } } // end while System.out.println("number=" + number + ", oB=" + originalBase); BigInteger n = new BigInteger (number, originalBase); System.out.println("n=" + n); } }
Last edited by Norm; 06202010 at 05:38 PM. Reason: Fix code
 06202010, 05:39 PM #20Member
 Join Date
 Jun 2010
 Posts
 34
 Rep Power
 0
here try to run this code
Java Code:import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.math.BigInteger; public class AnyToAny { public static void main(String[]args){ Scanner kbd = new Scanner(System.in); String repeatChoice = ""; do { System.out.println("Enter the Orginal Base from 2 16 ONLY~!!!!!!: "); int originalBase = Integer.parseInt(kbd.next()); kbd.nextLine(); System.out.println("Enter number to convert: "); String number = kbd.next(); kbd.nextLine(); Pattern p = Pattern.compile("['0', '1', '2', '3'.....'9', 'A', 'B', 'C', .... 'X', 'Y', 'Z']"); Matcher m = p.matcher(number); if(m.find()) { }else do{ System.out.println("Enter number to convert Again: "); number = kbd.next(); kbd.nextLine(); m = p.matcher(number); }while(!m.find()); BigInteger n = new BigInteger (number, originalBase); int base = 2; while (base < 2  base > 16) { System.out.println("Enter base FROM 2  16 ONLY: "); base = Integer.parseInt(kbd.next()); kbd.nextLine(); if(base < 2) { System.out.println("Too low"); }else if (base > 16) { System.out.println("Too high"); } } System.out.println("Result is:" + n.toString(base)); System.out.println("Do you want to try it again? [yes/no]: "); repeatChoice = kbd.nextLine(); }while(repeatChoice.equalsIgnoreCase("yes")); } }
Similar Threads

Game assistance.
By Sean_J in forum New To JavaReplies: 2Last Post: 03162010, 05:05 PM 
Error assistance
By bobbychiken in forum New To JavaReplies: 2Last Post: 11212009, 11:54 PM 
Looking for assistance
By s_dawg101 in forum New To JavaReplies: 32Last Post: 11042009, 03:49 AM 
New to the forum + assistance :)
By quemadissimo in forum New To JavaReplies: 4Last Post: 10312009, 07:41 AM 
In need of some assistance
By Boer84 in forum New To JavaReplies: 2Last Post: 07082008, 04:14 PM
Bookmarks