• (3 Pages)
• 1
• 2
• 3

32 Replies - 2240 Views - Last Post: 20 June 2010 - 03:27 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=178192&amp;s=fecd75ed287d58726f2822f66b6a0da8&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 NoobProgramer

Reputation: -4
• Posts: 191
• Joined: 15-June 09

Posted 18 June 2010 - 03:36 AM

here's the code
```  System.out.println("NOTE: if your gonna input hexadecimal numbers be sure its only 1-9 and A-F/a-f");
System.out.println();
System.out.println("Enter number to convert: ");

int originalBase = -2;
while(originalBase < 2 || originalBase > 16){
System.out.println("Enter the Orginal Base: ");
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: ");
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));
String yes = "Y";
String no = "N";

System.out.println("Do you want to try it again? [Y/N]: ");
System.exit(0);
}
}

}

```

Questions:
1. How can i put a restriction if a user input G and beyond the program will ask him again since its an invalid hexa?
2. about the yes and no last batch of codes
im almost done w/ it but how can i rerun the program if the user choose Y = yes? that means he want to repeat it again to convert more numbers
w/o pressing shift + f6 "netbeans"
i left the if yes statement blank since i dont know what to put inside of it.

3. if i want a long method algorithm to get the results of any to any conversions? what codes do i needed or can somebody help me out for that long method thing for getting the result and so on.

This post has been edited by NoobProgramer: 18 June 2010 - 04:49 AM

Is This A Good Question/Topic? 0

Replies To: Restrictions about hexa and etc.

#2 bcranger

• D.I.C Lover

Reputation: 252
• Posts: 1,199
• Joined: 01-February 10

Re: Restrictions about hexa and etc.

Posted 18 June 2010 - 06:10 AM

#1 do a try/catch statement; a NumberFormatExceptionerror means it was invalid, not A-F, 0-9

#2 Put your program in a while loop; after asking yes or no, if no, then break from infinite loop

#3 Don't quite understand your question, but your code should do conversions from any base to any base 2-16 if you followed my code in an earlier post

#3 NoobProgramer

Reputation: -4
• Posts: 191
• Joined: 15-June 09

Re: Restrictions about hexa and etc.

Posted 18 June 2010 - 07:25 AM

-edited-

This post has been edited by NoobProgramer: 18 June 2010 - 08:38 AM

#4 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12297
• Posts: 45,399
• Joined: 27-December 08

Re: Restrictions about hexa and etc.

Posted 18 June 2010 - 07:50 AM

We know that for bases 2-16, we will have digits from 0-F. So if we have a String or an array with the ordering 0123456789ABCDEF, we can check to see if a digit is invalid based on the base. So the largest digit will be at index base-1. So if we're working in base 8, then the largest number will reside at index 7.

As for your second question, you'll want to use an outer loop along the lines of:
```do{
//evaluate number bases part

//then prompt for user input regarding play
//again
}while(playAgain);

```

Duplicate topics merged. Please avoid duplicate posting.

#5 NoobProgramer

Reputation: -4
• Posts: 191
• Joined: 15-June 09

Re: Restrictions about hexa and etc.

Posted 18 June 2010 - 08:05 AM

bcranger, on 18 June 2010 - 05:10 AM, said:

#1 do a try/catch statement; a NumberFormatExceptionerror means it was invalid, not A-F, 0-9

#2 Put your program in a while loop; after asking yes or no, if no, then break from infinite loop

#3 Don't quite understand your question, but your code should do conversions from any base to any base 2-16 if you followed my code in an earlier post

1. i never used or experience using a try catch code so its kinda hard for me to use it but im reading some info's about it at uncle google.

2. which part of my program? and i already put a if statement below my codes
so if the user chose no then it will exit but about YES i haven't figured it out but anyways switch part?

i was wandering if incase i want to change the algorithm of the codes that u gave me to a Long Method algorithm
like the times , minus , divide thingy to convert any numbers to any numbers conversion

since the math.biginterger is kinda shortcut.

macosxnerd101, on 18 June 2010 - 06:50 AM, said:

We know that for bases 2-16, we will have digits from 0-F. So if we have a String or an array with the ordering 0123456789ABCDEF, we can check to see if a digit is invalid based on the base. So the largest digit will be at index base-1. So if we're working in base 8, then the largest number will reside at index 7.

As for your second question, you'll want to use an outer loop along the lines of:
```do{
//evaluate number bases part

//then prompt for user input regarding play
//again
}while(playAgain);

```

Duplicate topics merged. Please avoid duplicate posting.

sorry for double posting Coz' my internet is really slow.

so ill change the if statement to do while statement?

#6 m-e-g-a-z

• Winning

Reputation: 497
• Posts: 1,457
• Joined: 19-October 09

Re: Restrictions about hexa and etc.

Posted 18 June 2010 - 08:05 AM

You can use the regex class for pattern matching, heres a quick example I mocked up.

```import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class matching {

public static void main (String [] args){
//instantiate scanner
Scanner s = new Scanner(System.in);
//print
String a=s.next();
//instantiate a Pattern invoking compile with pattern as parameters
Pattern p = Pattern.compile("[0-9a-fA-F]");
//we then invoke the matcher() method on the String a to see if it matches 0-9 a-f A-F
Matcher m = p.matcher(a);
//if it can find pattern
if(m.find()){

//carry on
}
else

do{
//we then ask for the user input if it is not valid
System.out.println("Not valid enter again:");
a = s.next();
//check if it matches pattern
m = p.matcher(a);

}
//while it cannot find pattern
while(!m.find());
}
}

```

Edit - Way too late

This post has been edited by m-e-g-a-z: 18 June 2010 - 08:06 AM

#7 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12297
• Posts: 45,399
• Joined: 27-December 08

Re: Restrictions about hexa and etc.

Posted 18 June 2010 - 08:57 AM

Quote

so ill change the if statement to do while statement?

Not exactly. You'll want the loop to contain basically the rest of your program.

Quote

1. i never used or experience using a try catch code so its kinda hard for me to use it but im reading some info's about it at uncle google.

If you don't want to use try-catch, you can implement the approach I described for this.

Quote

2. which part of my program? and i already put a if statement below my codes
so if the user chose no then it will exit but about YES i haven't figured it out but anyways switch part?

More or less, this is the idea.
```while(the user input is not yes or no)
//prompt for play again

```

#8 NoobProgramer

Reputation: -4
• Posts: 191
• Joined: 15-June 09

Re: Restrictions about hexa and etc.

Posted 18 June 2010 - 09:11 AM

m-e-g-a-z, on 18 June 2010 - 07:05 AM, said:

You can use the regex class for pattern matching, heres a quick example I mocked up.

```import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class matching {

public static void main (String [] args){
//instantiate scanner
Scanner s = new Scanner(System.in);
//print
String a=s.next();
//instantiate a Pattern invoking compile with pattern as parameters
Pattern p = Pattern.compile("[0-9a-fA-F]");
//we then invoke the matcher() method on the String a to see if it matches 0-9 a-f A-F
Matcher m = p.matcher(a);
//if it can find pattern
if(m.find()){

//carry on
}
else

do{
//we then ask for the user input if it is not valid
System.out.println("Not valid enter again:");
a = s.next();
//check if it matches pattern
m = p.matcher(a);

}
//while it cannot find pattern
while(!m.find());
}
}

```

Edit - Way too late

i tried and run your program
and i tried to input 13G-Z

Quote

2. which part of my program? and i already put a if statement below my codes
so if the user chose no then it will exit but about YES i haven't figured it out but anyways switch part?

More or less, this is the idea.
```while(the user input is not yes or no)
//prompt for play again

```

[/quote]

sorry if incase that i irritate you w/ my questions
so ill add a while statement before my if statement? or?

#9 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12297
• Posts: 45,399
• Joined: 27-December 08

Re: Restrictions about hexa and etc.

Posted 18 June 2010 - 11:58 AM

Quote

sorry if incase that i irritate you w/ my questions
so ill add a while statement before my if statement? or?

Think about exactly what you want to repeat. Do you want to only repeat getting input, then evaluate the validated input after the loop? Do you want to process the input and respond appropriately (ie., if the user entered no, then exit) inside the loop?

Programming and algorithm development isn't about adding this here and that there to patch together a quick fix. It's about thinking about the problem and developing a working solution that is easy for others maintain.

#10 NoobProgramer

Reputation: -4
• Posts: 191
• Joined: 15-June 09

Re: Restrictions about hexa and etc.

Posted 18 June 2010 - 05:10 PM

macosxnerd101, on 18 June 2010 - 10:58 AM, said:

Think about exactly what you want to repeat. Do you want to only repeat getting input, then evaluate the validated input after the loop? Do you want to process the input and respond appropriately (ie., if the user entered no, then exit) inside the loop?

i want to repeat the whole program if the user chooses YES

#11 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12297
• Posts: 45,399
• Joined: 27-December 08

Re: Restrictions about hexa and etc.

Posted 18 June 2010 - 05:50 PM

Great. So try writing the code for this, or at least some pseudo-code. Those questions were intended to get you thinking about a solution.

#12 NoobProgramer

Reputation: -4
• Posts: 191
• Joined: 15-June 09

Re: Restrictions about hexa and etc.

Posted 18 June 2010 - 06:11 PM

macosxnerd101, on 18 June 2010 - 04:50 PM, said:

Great. So try writing the code for this, or at least some pseudo-code. Those questions were intended to get you thinking about a solution.

i did some do while loop
but my problem is how can i repeat the program from the top if the users choose a YEs *geez no is more easier *

```String repeatChoice="";
do{
System.out.println("Do you want to try it again? [yes/no]: ");
}while(repeatChoice.equalsIgnoreCase("yes"));

```

#13 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12297
• Posts: 45,399
• Joined: 27-December 08

Re: Restrictions about hexa and etc.

Posted 18 June 2010 - 06:14 PM

So wouldn't it make sense to have the logic of your program inside the loop?

#14 NoobProgramer

Reputation: -4
• Posts: 191
• Joined: 15-June 09

Re: Restrictions about hexa and etc.

Posted 18 June 2010 - 06:23 PM

macosxnerd101, on 18 June 2010 - 05:14 PM, said:

So wouldn't it make sense to have the logic of your program inside the loop?

i tried this one but its kinda long
```String repeatChoice="";
System.out.println("Do you want to try it again? [yes/no]: ");
do{
System.out.println("Enter number to convert: ");
number = kbd.next();
Pattern c = Pattern.compile("[0-9a-fA-F]");
Matcher d = c.matcher(number);
if(m.find()){
} else
do{
System.out.println("Enter number to convert Again: ");
number = kbd.next();
m = c.matcher(number);
}while(!m.find());

originalBase = -2;
while(originalBase < 2 || originalBase > 16){
System.out.println("Enter the Orginal Base: ");
originalBase = Integer.parseInt(kbd.next());
if(originalBase <2 ){
System.out.println("Too low");
}else if(originalBase >16){
System.out.println("Too high");
}
}
BigInteger y = new BigInteger(number,originalBase);
base =-2;
while(base < 2 ||base > 16){
System.out.println("Enter base FROM 2 - 16 ONLY: ");
base = Integer.parseInt(kbd.next());
if(base <2){
System.out.println("Too low");
}else if(base >16){
System.out.println("Too high");

}

}System.out.println("Result is:"+y.toString(base));

}while(repeatChoice.equalsIgnoreCase("yes"));

```

the problem is it only repeat once.
and haven't put a no.

#15 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12297
• Posts: 45,399
• Joined: 27-December 08

Re: Restrictions about hexa and etc.

Posted 18 June 2010 - 06:29 PM

Can you work on your formatting and indentation conventions so that I can better follow your logic?

Also, do-loops execute first, then evaluate the while(condition). So regardless of what the user enters first, the loop will execute. Better to prompt at the end of the loop.
```String repeatChoice="";
System.out.println("Do you want to try it again? [yes/no]: ");

```

So along the lines of:
```do{

while(input is not YES or NO){
//prompt for input
}
}while(the user wants to play again);

//the user has input no, so we are out of
//the loop and quitting the program

```