# Thread: Assistance about this.

1. Member
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:```
problems:
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("[0-9a-fA-F]");
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"));
}
}```
note:

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 codes
Last edited by Syfer; 06-19-2010 at 07:30 AM.

2. my problem is how can i code it out and edit my previous codes
I don't understand this problem of yours. Can you explain?

3. Member
Join Date
Jun 2010
Posts
34
Rep Power
0
Originally Posted by Norm
I don't understand this problem of yours. Can you explain?
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```
note: if the number is invalid the program will ask the user to input a new number to be converted that will not exceed 7 since 78 is invalid
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.

4. 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 over-simplifying the problem.

Much luck.
Last edited by Fubarable; 06-19-2010 at 02:18 PM.

5. Member
Join Date
Jun 2010
Posts
34
Rep Power
0
Originally Posted by Fubarable
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 over-simplifying the problem.

Much luck.

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("[0-9a-fA-F]");
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));
}
}```
i just solved 1 of my problems
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 a-e 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 1-0 only if it doesnt match the original base it will repeat to ask what number to be converted.

6. 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

7. 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();
}
}
}```

8. Member
Join Date
Jun 2010
Posts
34
Rep Power
0
Originally Posted by Fubarable
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: Integer (Java Platform SE 6)
Originally Posted by Fubarable
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();
}
}
}```
i still dont get it >_> it seems that im poor in analyzing such things like that

9. i still dont get it
Can you state the problem that you are trying to solve in simple terms?

10. Member
Join Date
Jun 2010
Posts
34
Rep Power
0
Originally Posted by Norm
Can you state the problem that you are trying to solve in simple terms?
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.

11. 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
What base is the number that is entered? Is the 68 supposed to be base 7?
repeating it until the number to be converted matches the orginal base
Sounds like a loop.
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".

12. Member
Join Date
Jun 2010
Posts
34
Rep Power
0
Originally Posted by Norm
What base is the number that is entered? Is the 68 supposed to be base 7?
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

Originally Posted by Norm
Then enter the loop and stay there until the user enters a number that "matches the orginal base".
yes since the program will ask first the original base before the number to be converted.

13. Is this the logic:
1-Ask user for base number to use.
2-Get base to use.
3-Get number from user (that number must be of the base entered in 2)
4-Test that number entered in 3 is of base given in 2
5-If number is not correct base, go to step 3
6-Convert number from step 3 from base given in 2 to base 10.

14. Member
Join Date
Jun 2010
Posts
34
Rep Power
0
Originally Posted by Norm
Is this the logic:
1-Ask user for Original base number to use.
2-Ask the User to input a Number to be converted (that number mustmuch the number 1 *its rules* like octal base= 1-7 | hexa base from 1-9 and a-f |binary base 1-0 | 15 base, 1-9 a-e | 12 base , 1-9 a-d and so on -1)
4-Check if the inputted number matches the number 1 step
5-If number is doesnt match the original base, go to step 2
6-Convert number from step 3 from base given in 2 to base of any choice.
fixed.
10letters

as addition:

sample output
Java Code:
```Input Original base: 15
Input number : 235F
Input Number again: 23E

notice: its F so the program will ask for a new number to be converted since base 15 max is from 1-9 and a-E from the logic number 2```

15. Have you solved your problem yet?
If not, could you explain what it is now?

16. Member
Join Date
Jun 2010
Posts
34
Rep Power
0
Originally Posted by Norm
Have you solved your problem yet?
If not, could you explain what it is now?
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

17. exiting after inputting a invalid number that doesnt match the original base
Could you post the code where the invalid number is detected and where you want the exit to happen? Exit from where? The program, method or loop?

18. Member
Join Date
Jun 2010
Posts
34
Rep Power
0
Originally Posted by Norm
Could you post the code where the invalid number is detected and where you want the exit to happen? Exit from where? The program, method or loop?
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```
that will surely gets a error and exits you from the program
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

19. 1-Ask user for base number to use.
2-Get base to use.
3-Get number from user (that number must be of the base entered in 2)
4-Test that number entered in 3 is of base given in 2
5-If number is not correct base, go to step 3
6-Convert number from step 3 from base given in 2 to base 10.
The code:
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; 06-20-2010 at 05:38 PM. Reason: Fix code

20. Member
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"));
}
}```
but the question is how? how could i implement that to my code

Page 1 of 2 12 Last

#### Posting Permissions

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