Page 2 of 2 FirstFirst 12
Results 21 to 39 of 39
  1. #21
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default

    I'm getting confused.
    Which parts of the code posted in post#20 do you want to implement in your code?
    Does the above code do what you want?

    Do you have two programs? The one posted above and another one somewhere else?

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

    Default

    Quote Originally Posted by Norm View Post
    I'm getting confused.
    Which parts of the code posted in post#20 do you want to implement in your code?
    Does the above code do what you want?

    Do you have two programs? The one posted above and another one somewhere else?
    the number 20# is the whole codes
    implement that "try catch" that solves my problem
    that will not exit the problem if a mistake number inputted.

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

    Default

    i cant really implement this code inside my program >_> since i dont know how to use it

  4. #24
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default

    I'm sorry. I thought that my post#19 was the solution. What more is there than that?

    My code was to solve this:
    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.
    What other problems are there now?

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

    Default

    Quote Originally Posted by Norm View Post
    I'm sorry. I thought that my post#19 was the solution. What more is there than that?

    My code was to solve this:


    What other problems are there now?
    here ill post a screen shot of the output of my program



    notice the error
    example 1:

    i inputted 15 as Original base
    i inputted 12F as the number to be converted

    and i committed an error
    instead of committing an error
    it will ask the user again to input a new Number to be convert
    like 12E , and etc w/ the maximum of string E

    example 2:

    i inputted 7 as Original base <-- 7 is octal & we all know that its only between 1-7 numbers only and
    i inputted 68 as the number to be converted

    and i committed an error
    instead of committing an error
    it will ask the user again to input a new Number to be convert
    like 67 , and etc w/ the maximum of int 7

    same goes to others

    here's my CODE

    Java Code:
    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-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));
    
          System.out.println("Do you want to try it again? [yes/no]: ");
          repeatChoice = kbd.nextLine();
    
        }while(repeatChoice.equalsIgnoreCase("yes"));
      }
    
    
    }

  6. #26
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default

    Does the code I posted earlier work as you want with your input?

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

    Default

    Quote Originally Posted by Norm View Post
    Does the code I posted earlier work as you want with your input?
    yes the logic is right

    my program works properly if you'll input the right numbers

    but if you wont put the right number or the original base it will poof to error
    here's some output of a wrong number inputted
    Java Code:
    Enter the Orginal Base from 2- 16 ONLY~!!!!!!: 
    15
    Enter number to convert: 
    15f
    Exception in thread "main" java.lang.NumberFormatException: For input string: "15f"
            at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
            at java.lang.Integer.parseInt(Integer.java:458)
            at java.math.BigInteger.<init>(BigInteger.java:316)
            at AnyToAny.main(AnyToAny.java:42)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 6 seconds)
    since the max string for 15 base is E
    it will really commit an error
    what i wanted is if it goes like that
    INSTEAD of SAYING THAT KIND OF ERROR
    it will let the user repeat inputting a new Number to be converted

    as what ur logic says.

  8. #28
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default

    You get the error because the data passed to BigInteger is wrong.
    When the data is wrong you get an exception
    You need to use try{}catch(){} to trap that error
    All that code must be in a loop that you exit ONLY when the data is correct.
    One way to control a loop is with a boolean that is initially true: while(gettingNbr) { ...}
    When the data is good, set gettingNumber to false.

    If you are unable to change your code, I suggest you throw it all out and start over.

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

    Default

    Quote Originally Posted by Norm View Post
    You get the error because the data passed to BigInteger is wrong.
    When the data is wrong you get an exception
    You need to use try{}catch(){} to trap that error
    All that code must be in a loop that you exit ONLY when the data is correct.
    One way to control a loop is with a boolean that is initially true: while(gettingNbr) { ...}
    When the data is good, set gettingNumber to false.

    If you are unable to change your code, I suggest you throw it all out and start over.
    as what i've mentioned i don't know how to use properly the try catch statement
    that's why i'm asking for help since i don't know how to use them

  10. #30
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default

    Here's the code from the earlier post. It uses try catch:
    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);
       }
    }

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

    Default

    i'm having a little idea
    and i tried to implement that to my code
    here it is but im getting some sort of misplace of brakets or whatsoever it is
    can you fix it for me? if you dont mind.

    Java Code:
     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-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());
    
          boolean getNumber = true;
          while(getNumber) {
             try{
                int val = Integer.parseInt(number, originalBase);
                getNumber = false;  // exit
                 }catch(NumberFormatException e) {
               System.out.println("Enter number to convert Again: ");
                number = kbd.next();
                 
                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"));
    
    
          }
    
      
          }

  12. #32
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default

    im getting some sort of misplace
    Please copy and paste full text of error messages here.

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

    Default

    Quote Originally Posted by Norm View Post
    Please copy and paste full text of error messages here.
    Java Code:
    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - reached end of file while parsing
            at AnyToAny.main(AnyToAny.java:20)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)

  14. #34
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default

    Does your IDE have a tool to find matching {}. Look at the menus and see if it does.
    Mine works by putting the cursor on a { or ( and then pressing CTL+] and the cursor jumps to the matching } or ).

    Look at line 20.

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

    Default

    Quote Originally Posted by Norm View Post
    Does your IDE have a tool to find matching {}. Look at the menus and see if it does.
    Mine works by putting the cursor on a { or ( and then pressing CTL+] and the cursor jumps to the matching } or ).

    Look at line 20.
    nothing happened when i did that thing
    and i keep on searching how to fix my program and you haven't fixed the codes i posted =(

  16. #36
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default

    What IDE are you using? Can you go to a forum for that IDE and ask there how to find matching {} etc

  17. #37
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Syfer View Post
    ...and you haven't fixed the codes i posted =(
    That's not his or any of our jobs. He's given you great suggestions though on how you can fix your own code.

    Luck.

  18. #38
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default

    I'm beginning to think that programming may not be your "cup of tea".
    Not everyone's brain works the same way as a computer.

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

    Default

    Quote Originally Posted by Norm View Post
    I'm beginning to think that programming may not be your "cup of tea".
    Not everyone's brain works the same way as a computer.
    tbh is im only good at logics but i cant apply it in codes hehehe *thats why im always beside my friend who is good in programming but poor in logics*
    and im good at basic programming not the complicated one's

    and btw
    i already fixed it and implemented the try catch that u made
    and poof it work w/ a few poof poof

    my brain really works sometimes >_>

    anyhoo how can now i apply the yes and no choice
    if a user wants to repeat converting again w/o pressing the shift+ f6 or to run again?

    where can i prolly put it here
    Java Code:
        System.out.println("Enter the Orginal Base from 2- 16 ONLY~!!!!!!: ");
          int originalBase = Integer.parseInt(kbd.next());
          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());
    
          boolean getNumber = true;
          while(getNumber) {
             try{
                 int val = Integer.parseInt(number, originalBase);
                 getNumber = false;  
             }catch(NumberFormatException fre) {
               System.out.println("Enter number to convert Again:");
                number = kbd.next();
             }
          }
    
          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));
    
       }
    }
    }
    }

    and btw i tried the do while repeatChoice but
    bammm it didnt work and it messed up my program.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Game assistance.
    By Sean_J in forum New To Java
    Replies: 2
    Last Post: 03-16-2010, 05:05 PM
  2. Error assistance
    By bobbychiken in forum New To Java
    Replies: 2
    Last Post: 11-21-2009, 11:54 PM
  3. Looking for assistance
    By s_dawg101 in forum New To Java
    Replies: 32
    Last Post: 11-04-2009, 03:49 AM
  4. New to the forum + assistance :)
    By quemadissimo in forum New To Java
    Replies: 4
    Last Post: 10-31-2009, 07:41 AM
  5. In need of some assistance
    By Boer84 in forum New To Java
    Replies: 2
    Last Post: 07-08-2008, 05:14 PM

Posting Permissions

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