Page 1 of 2 12 LastLast
Results 1 to 20 of 22

Thread: Need help

  1. #1
    Syfer is offline Member
    Join Date
    Jun 2010
    Posts
    34
    Rep Power
    0

    Default Need help

    i dont know where to post this but its all about java.
    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);
        }
    }
    }
    problems.
    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*

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,574
    Rep Power
    23

    Default

    how can i play/start from the start again
    Put the code in a loop and use the continue statement to go back to the start.

    convert any *decimal,octal,hex* to any *decimal,octal,hex*
    There are methods in the Integer class that allow you to specify the radix.

  3. #3
    Syfer is offline Member
    Join Date
    Jun 2010
    Posts
    34
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Put the code in a loop and use the continue statement to go back to the start.
    how can i implement that since my yes or no codes are in string and
    i just want if the user choses yes it will start from the start again.

  4. #4
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    4

    Default

    Quote Originally Posted by Syfer View Post
    how can i implement that since my yes or no codes are in string and
    i just want if the user choses yes it will start from the start again.
    What do you mean by "my yes or no codes are in string", and how will this prevent you from looping as Norm suggests?

  5. #5
    Syfer is offline Member
    Join Date
    Jun 2010
    Posts
    34
    Rep Power
    0

    Default

    Quote Originally Posted by curmudgeon View Post
    What do you mean by "my yes or no codes are in string", and how will this prevent you from looping as Norm suggests?
    i'm not sure if i need a for loop in my code
    because i'm just asking the user if he or she wants to repeat and convert again
    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.

  6. #6
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    4

    Default

    Quote Originally Posted by Syfer View Post
    i'm not sure if i need a for loop in my code because i'm just asking the user if he or she wants to repeat and convert again
    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.
    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.
    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.

  7. #7
    Syfer is offline Member
    Join Date
    Jun 2010
    Posts
    34
    Rep Power
    0

    Default

    Quote Originally Posted by curmudgeon View Post
    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 ill use while or do while
    how can i code the YES and no in it?

  8. #8
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    4

    Default

    Quote Originally Posted by Syfer View Post
    so if ill use while or do while
    how can i code the YES and no in it?
    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.

  9. #9
    Syfer is offline Member
    Join Date
    Jun 2010
    Posts
    34
    Rep Power
    0

    Default

    Quote Originally Posted by curmudgeon View Post
    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.
    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"));
    is it like this?
    so how can i make the program repeat again from the top if the user chooses
    yes and exit if no.

  10. #10
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    4

    Default

    Quote Originally Posted by Syfer View Post
    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"));
    is it like this?
    Try it and see.

    so how can i make the program repeat again from the top if the user chooses
    yes and exit if no.
    As Norm already mentioned: you place all that you want to repeat in the body of the loop. So below the
    Java Code:
    do {
    and above the
    Java Code:
    System.out.println("Do you want to try it again? [yes/no]: ");
    You have all the statements that you want looped. Consider refactoring the code into more manageable methods though.

  11. #11
    Syfer is offline Member
    Join Date
    Jun 2010
    Posts
    34
    Rep Power
    0

    Default

    Quote Originally Posted by curmudgeon View Post
    Try it and see.



    As Norm already mentioned: you place all that you want to repeat in the body of the loop. So below the
    Java Code:
    do {
    and above the
    Java Code:
    System.out.println("Do you want to try it again? [yes/no]: ");
    You have all the statements that you want looped. Consider refactoring the code into more manageable methods though.
    i already put the codes inside the do but it will only repeat once. o.O

  12. #12
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    4

    Default

    Quote Originally Posted by Syfer View Post
    i already put the codes inside the do but it will only repeat once. o.O
    I don't see where you've done this which limits my ability to help. Please show this code.

  13. #13
    Syfer is offline Member
    Join Date
    Jun 2010
    Posts
    34
    Rep Power
    0

    Default

    Quote Originally Posted by curmudgeon View Post
    I don't see where you've done this which limits my ability to help. Please show this code.
    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"));

  14. #14
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    4

    Default

    To quote myself, please note the bold text:
    Quote Originally Posted by curmudgeon View Post
    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.
    Do you see your problem now?

  15. #15
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    4

    Default

    Or to put it another way: where inside of the loop do you ever set/change the value of repeatChoice?

  16. #16
    Syfer is offline Member
    Join Date
    Jun 2010
    Posts
    34
    Rep Power
    0

    Default

    i still dont get it >_>

  17. #17
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    4

    Default

    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?

  18. #18
    Syfer is offline Member
    Join Date
    Jun 2010
    Posts
    34
    Rep Power
    0

    Default

    Quote Originally Posted by curmudgeon View Post
    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?

    yes i see the difference but when i tried to implement that
    it stops before i can answer yes or no.

  19. #19
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    4

    Default

    Quote Originally Posted by Syfer View Post
    yes i see the difference but when i tried to implement that
    it stops before i can answer yes or no.
    You should know by now that when you make statements like that above without showing us how you've changed your code, we've no idea of just what you're doing wrong. Please don't make us keep asking you for your code changes. Thanks.

  20. #20
    Syfer is offline Member
    Join Date
    Jun 2010
    Posts
    34
    Rep Power
    0

    Default

    Quote Originally Posted by curmudgeon View Post
    You should know by now that when you make statements like that above without showing us how you've changed your code, we've no idea of just what you're doing wrong. Please don't make us keep asking you for your code changes. Thanks.
    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"));
      }
    }

Page 1 of 2 12 LastLast

Posting Permissions

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