Results 1 to 20 of 22
Thread: Need help
- 06-18-2010, 06:08 PM #1
Member
- Join Date
- Jun 2010
- Posts
- 34
- Rep Power
- 0
Need help
i dont know where to post this but its all about java.
problems.Java 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("Enter number to convert: "); String number = kbd.next(); Pattern p = Pattern.compile("[09a-fA-F]"); Matcher m = p.matcher(number); if(m.find()){ } else do{ System.out.println("Enter number to convert Again: "); number = kbd.next(); 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()); 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()); 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]: "); String answer = kbd.next(); if(answer.equals(yes)){ //prompt to play again// }else if(answer.equals(no)){ System.out.println("Thank You For Using The Program"); System.exit(0); } } }
1. how can i play/start from the start again after the user inputted or chose yes?
2. im having a problem about the hex thing its still accepting 13G -Z
or any Digits like 132G , purpose is it shouldn't accept any string that is G - Z
since hex are until a-f and 1-9 only
3. if i want a long method of algorithms anyone can help me to change my code to long method? convert any *decimal,octal,hex* to any *decimal,octal,hex*
- 06-18-2010, 08:14 PM #2
Put the code in a loop and use the continue statement to go back to the start.how can i play/start from the start again
There are methods in the Integer class that allow you to specify the radix.convert any *decimal,octal,hex* to any *decimal,octal,hex*
- 06-19-2010, 01:49 AM #3
Member
- Join Date
- Jun 2010
- Posts
- 34
- Rep Power
- 0
- 06-19-2010, 01:52 AM #4
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
- 06-19-2010, 01:59 AM #5
Member
- Join Date
- Jun 2010
- Posts
- 34
- Rep Power
- 0
- 06-19-2010, 02:01 AM #6
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
No one mentioned anything about a for loop. A better loop would be a while loop or do-while loop, since you do not know on loop start just how many times the loop will occur.
Again, I don't see where your problem is. Try a while loop or do-while loop, and if it still doesn't work, you may want to give more details on just what's wrong.so if yes it will go on top again and repeat the program and if no it will exit the program.
my codes are been posted above.
- 06-19-2010, 02:12 AM #7
Member
- Join Date
- Jun 2010
- Posts
- 34
- Rep Power
- 0
- 06-19-2010, 02:19 AM #8
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
You would declare a String variable, let's call it repeatChoice, before the do-while loop and get the user's choice from within the loop at the bottom of the loop and place this reply into the repeatChoice variable. Then in the while expression below the loop block, check if repeatChoice.equalsIgnoreCase("yes"), or however you want to check it.
- 06-19-2010, 02:24 AM #9
Member
- Join Date
- Jun 2010
- Posts
- 34
- Rep Power
- 0
is it like this?Java Code:String repeatChoice=""; do{ System.out.println("Do you want to try it again? [yes/no]: "); String answer = kbd.next(); }while(repeatChoice.equalsIgnoreCase("yes"));
so how can i make the program repeat again from the top if the user chooses
yes and exit if no.
- 06-19-2010, 02:29 AM #10
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
Try it and see.
As Norm already mentioned: you place all that you want to repeat in the body of the loop. So below theso how can i make the program repeat again from the top if the user chooses
yes and exit if no.
and above theJava Code:do {
You have all the statements that you want looped. Consider refactoring the code into more manageable methods though.Java Code:System.out.println("Do you want to try it again? [yes/no]: ");
- 06-19-2010, 02:37 AM #11
Member
- Join Date
- Jun 2010
- Posts
- 34
- Rep Power
- 0
- 06-19-2010, 02:39 AM #12
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
- 06-19-2010, 02:41 AM #13
Member
- Join Date
- Jun 2010
- Posts
- 34
- Rep Power
- 0
you and him told me to put the codes i want to repeat inside the do statement
Java Code:String repeatChoice=""; System.out.println("Do you want to try it again? [yes/no]: "); String answer = kbd.next(); 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"));
- 06-19-2010, 02:43 AM #14
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
- 06-19-2010, 02:52 AM #15
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
Or to put it another way: where inside of the loop do you ever set/change the value of repeatChoice?
- 06-19-2010, 03:11 AM #16
Member
- Join Date
- Jun 2010
- Posts
- 34
- Rep Power
- 0
i still dont get it >_>
- 06-19-2010, 03:23 AM #17
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
Let's simplify your code greatly so that we can see the essense of your problem. Here in a nutshell is your current code, just focusing on getting user input and using this to decide on whether to loop or not:
Java Code:import java.util.Scanner; public class SimpleLoop { public static void main(String[] args) { Scanner kbd = new Scanner(System.in); String repeatChoice = ""; System.out.println("Do you want to try it again? [yes/no]: "); String answer = kbd.next(); do { System.out.print("Enter number to convert: "); String numberToConvert = kbd.nextLine(); System.out.println("you entered: " + numberToConvert); // where inside of the loop do we ask if the user wants to try // again, and then use that response to change repeatChoice? } while (repeatChoice.equalsIgnoreCase("yes")); } }
Now, contrast that with my version:
Java Code:import java.util.Scanner; public class SimpleLoop { public static void main(String[] args) { Scanner kbd = new Scanner(System.in); String repeatChoice = ""; do { System.out.print("Enter number to convert: "); String numberToConvert = kbd.nextLine(); System.out.println("you entered: " + numberToConvert); // Let's get the user's input from [b][color="red"]within[/color][/b] the loop: System.out.print("Do you want to try it again? [yes/no]: "); repeatChoice = kbd.nextLine(); } while (repeatChoice.equalsIgnoreCase("yes")); } }
Do you see the difference between the two bits of code? More importantly, do you see why the change makes a difference?
- 06-19-2010, 03:55 AM #18
Member
- Join Date
- Jun 2010
- Posts
- 34
- Rep Power
- 0
- 06-19-2010, 03:56 AM #19
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
- 06-19-2010, 04:02 AM #20
Member
- Join Date
- Jun 2010
- Posts
- 34
- Rep Power
- 0
i just put the code inside the do while statement
like your example.
Java Code:import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.math.BigInteger; public class Conversion { public static void main(String[] args) { Scanner kbd = new Scanner(System.in); String repeatChoice = ""; do { System.out.println("Enter number to convert: "); String number = kbd.next(); 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(); 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()); 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()); 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)); // Let's get the user's input from within the loop: System.out.println("Do you want to try it again? [yes/no]: "); repeatChoice = kbd.nextLine(); } while (repeatChoice.equalsIgnoreCase("yes")); } }


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks