Page 1 of 2 12 LastLast
Results 1 to 20 of 39
  1. #1
    Mythreadings is offline Member
    Join Date
    Oct 2010
    Posts
    17
    Rep Power
    0

    Question [SOLVED] What is wrong with this code?

    *cue jeopardy music* :confused:

    Less "What is wrong with it" and more a question of, "how can I fix it." I have no trouble knowing what's WRONG with it, my compiler has with piercing enthusiasm given me plenty of examples as to what is WRONG with it...
    Any help? :D
    The purpose of this coding is to take a single decimal number, and return three equal numbers-- in binary, octal, and hexadecimal.
    If you need any additional information, such as how it is supposed to work, feel free to ask, just answer quickly, I need to email this in for homework by tonight.

    public class Converter
    {
    public static void main (String[] args)
    {
    import java.util.Scanner;
    Scanner kb= new Scanner (System.in);
    System.out.println("Enter a valid integer: ");
    int num = kb.nextInt();
    String dec = convert(num,10);
    String bin = convert(num,2);
    String oct = convert(num,8);
    String hex = convert(num,16);
    System.out.println("Decimal: " + dec);
    System.out.println("Binary: " + bin);
    System.out.println("Octal: " + oct);
    System.out.println("Hexidecimal: " + hex);
    }
    public static String convert(int val,int base){

    String s=" ";
    while (val>0){

    int rem = val%base;
    s = rem +s;
    val = val/base;
    }

    return s;
    }
    }
    Last edited by Mythreadings; 11-18-2010 at 03:19 AM. Reason: The end...?

  2. #2
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default

    Where do you declare the variable "num"? Also, could you post what errors the compiler says you have?
    Sincerely, Joshua Green
    Please REP if I help :)

  3. #3
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    :D great humour

    Few things wrong....
    Java Code:
    num=kb.nextint();
    what type is num (should be an int), .nextint()? should be .nextInt()

    this whole function (see my added comments)
    Java Code:
    // semi colon should not be there, return type should be String not void and
    // get rid of main (there should be only 1)
    public static void main convert(int val,int base);
    String s=" ";
    while (val>0){
    
    int rem = val%base;
    s = rem +s;
    val = val/base;
    }
    
    return s;
    // put a closing brace }

  4. #4
    Mythreadings is offline Member
    Join Date
    Oct 2010
    Posts
    17
    Rep Power
    0

    Default :)

    no prob-

    "num=kb.nextint();" right above the first String.

    "'(' expected" on line 17.
    6 problems on line 19, including "<identifier> expected", "illegal start of type", and "';' expected"
    "<identifier> expected" each on lines 21, 22, and 23
    "class, interface, or enum expected" on 26 and 27.

  5. #5
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default

    How to declare an integer:

    Java Code:
    int num = SOME NUMBER;

    How to read an integer from a line:

    Java Code:
    SOME SCANNER.nextInt();

    Combine these:

    Java Code:
    int num = SOMESCANNER.nextInt();
    Sincerely, Joshua Green
    Please REP if I help :)

  6. #6
    Mythreadings is offline Member
    Join Date
    Oct 2010
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by al_Marshy_1981 View Post
    :D great humour

    Few things wrong....
    Java Code:
    num=kb.nextint();
    what type is num (should be an int), .nextint()? should be .nextInt()

    this whole function (see my added comments)
    Java Code:
    // semi colon should not be there, return type should be String not void and
    // get rid of main (there should be only 1)
    public static void main convert(int val,int base);
    String s=" ";
    while (val>0){
    
    int rem = val%base;
    s = rem +s;
    val = val/base;
    }
    
    return s;
    // put a closing brace }
    Ok, well, I fixed what you told me too...(You might noticed, I've edited them on my original post as well.)
    but I seem to still have a few problems- many of the same problems, in fact. Could you possibly lower yourself from your lofty tower of Java success to help a poor student out, once more? *begging on knees*

  7. #7
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default

    Remove the semicolon from after the main method declaration, also you are forgetting brackets on your convert method. {....}
    Sincerely, Joshua Green
    Please REP if I help :)

  8. #8
    Mythreadings is offline Member
    Join Date
    Oct 2010
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by joshdgreen View Post
    How to declare an integer:

    Java Code:
    int num = SOME NUMBER;

    How to read an integer from a line:

    Java Code:
    SOME SCANNER.nextInt();

    Combine these:

    Java Code:
    int num = SOMESCANNER.nextInt();
    Done, but I still have more of less the same problems. D'ya see anything else? :(

  9. #9
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default

    Read comment above ^ ^ ^ I made those changes and your program worked for me.
    Sincerely, Joshua Green
    Please REP if I help :)

  10. #10
    Mythreadings is offline Member
    Join Date
    Oct 2010
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by joshdgreen View Post
    Remove the semicolon from after the main method declaration, also you are forgetting brackets on your convert method. {....}
    How should the brackets look?

  11. #11
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Quote Originally Posted by Mythreadings View Post
    Ok, well, I fixed what you told me too...(You might noticed, I've edited them on my original post as well.)
    but I seem to still have a few problems- many of the same problems, in fact. Could you possibly lower yourself from your lofty tower of Java success to help a poor student out, once more? *begging on knees*
    :D lofty tower (I thought it was a kingdom)....and no you did not fix what I told you, see joshdgreen post

  12. #12
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default

    Quote Originally Posted by Mythreadings View Post
    How should the brackets look?

    You have no starting bracket...
    Sincerely, Joshua Green
    Please REP if I help :)

  13. #13
    Mythreadings is offline Member
    Join Date
    Oct 2010
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by joshdgreen View Post
    You have no starting bracket...
    I'm asking for an example, I already have (), I don't know where {} would go.

  14. #14
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default

    Sincerely, Joshua Green
    Please REP if I help :)

  15. #15
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Java Code:
    public static String convert(int val,int base) // here...where is the {
    String s=" ";
    while (val>0){
    
    int rem = val%base;
    s = rem +s;
    val = val/base;
    }
    
    return s;
    }
    read the comment I placed in your code

  16. #16
    Mythreadings is offline Member
    Join Date
    Oct 2010
    Posts
    17
    Rep Power
    0

    Default

    Hokay, only one more error left- I thought mine looked like your example, but perhaps not? What is wrong with this Scanner, here?

    " Scanner kb= new Scanner (System.in);
    System.out.println("Enter a valid integer: ");
    int num = kb.nextInt(); "

  17. #17
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    what is the error you are getting? I can see it, but I am asking so not to just dump the code on you.

  18. #18
    Mythreadings is offline Member
    Join Date
    Oct 2010
    Posts
    17
    Rep Power
    0

    Default :(

    Cannot find symbol class Scanner.

  19. #19
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default

    You need to import the scanner.

    Java Code:
    import java.util.Scanner;

    The very first lines of your code should always be import statements.
    Sincerely, Joshua Green
    Please REP if I help :)

  20. #20
    Mythreadings is offline Member
    Join Date
    Oct 2010
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by joshdgreen View Post
    You need to import the scanner.

    Java Code:
    import java.util.Scanner;

    The very first lines of your code should always be import statements.
    Done, and edited in the first post, but...still two errors on it.
    illegal start of expression
    not a statement

Page 1 of 2 12 LastLast

Similar Threads

  1. What Could be Wrong with This code????
    By Manfizy in forum New To Java
    Replies: 9
    Last Post: 08-22-2010, 11:28 AM
  2. Please tell me what i am doing wrong in this code
    By Basit781 in forum CLDC and MIDP
    Replies: 11
    Last Post: 05-20-2010, 05:08 PM
  3. What's going wrong with this code?
    By Suurisa in forum New To Java
    Replies: 5
    Last Post: 10-19-2009, 11:59 PM
  4. pls tell wat wrong with my code???
    By low224 in forum New To Java
    Replies: 13
    Last Post: 01-11-2009, 07:40 AM
  5. What is wrong with this code
    By rosh72851 in forum New To Java
    Replies: 13
    Last Post: 10-31-2008, 01:50 AM

Tags for this Thread

Posting Permissions

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