Results 1 to 12 of 12
  1. #1
    DrKush's Avatar
    DrKush is offline Member
    Join Date
    Feb 2009
    Location
    Charloot NC
    Posts
    14
    Rep Power
    0

    Default CompileError que pason?

    Hi yes, I am making a program for class but I keep getting 2 error messages and I have no clue, in theory I think my code should work but the compiler says it wont. I've been rewriting the code for a few hrs still no idea. Any help would be great, thanks. The program takes a number and checks if its positive or negative or both and multiples it by powers.

    error messages:
    C:\Users\Kush\Documents\Power.java:16: ';' expected
    numb = String newnumb;
    ^
    C:\Users\Kush\Documents\Power.java:16: not a statement
    numb = String newnumb;
    ^
    2 errors

    Tool completed with exit code 1

    code:
    import java.util.*;
    import java.math.*;

    public class Power
    {
    public static void main(String[]args)
    {
    System.out.print("Please enter a number: ");

    Scanner in = new Scanner(System.in);
    int numb = in.next();
    String newnumb;
    numb = String newnumb;

    if(newnumb = 0)
    newnumb = numb * numb;
    System.out.println("Your number squared is: " +numb);
    if(newnumb = 1)
    newnumb = numb * numb * numb;
    System.out.println("Your number cubed is: " +numb);
    if(newnumb = 0 && numb = 1);
    newnumb = numb * numb * numb * numb * numb * numb;
    System.out.println("Your new number is: " +numb);
    }
    }

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

    Default

    That's because
    Java Code:
    numb = String newnumb;
    is not valid Java syntax. What are you trying to do here?

    Also, you appear to be using the wrong Scanner method to get integer input. Instead of in.next(), you're much better off calling in.nextInt() if you want to process int input.

  3. #3
    DrKush's Avatar
    DrKush is offline Member
    Join Date
    Feb 2009
    Location
    Charloot NC
    Posts
    14
    Rep Power
    0

    Default

    trying to:
    Write a program that prompts a user to enter a number. If the user inputs an even number, then your program should compute and display the number squared. If the user inputs a number that is divisible by 3, then your program should compute and display the number cubed. If the user inputs a number that is divisible by both 2 & 3, then your program should compute and display the number raised to the 6th power. Otherwise, your program should not display anything.

    not much help from the teacher, this is mainly online class

    I deleted

    numb = String newnumb;

    now i get 18+ errors

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

    Default

    not much help from the teacher, this is mainly online class
    then you need to review the basics in a bad way. Go to the source, the Sun tutorials and start reading. Else you're in for a bumpy and short ride.

    Trail: Learning the Java Language (The Java™ Tutorials)

  5. #5
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default The numb program

    Your compares are wrong. You can't compare using the equals "=", you have to use the compare "==" operator
    Java Code:
    if(newnumb == 0)
    You're also trying to print the wrong variable... you don't want to print your variable "numb", you want to printout "newnumb".
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  6. #6
    DrKush's Avatar
    DrKush is offline Member
    Join Date
    Feb 2009
    Location
    Charloot NC
    Posts
    14
    Rep Power
    0

    Default

    Got the code working but i think the math logic is wrong because it wont compute the right answers

    trying to:
    Write a program that prompts a user to enter a number. If the user inputs an even number, then your program should compute and display the number squared. If the user inputs a number that is divisible by 3, then your program should compute and display the number cubed. If the user inputs a number that is divisible by both 2 & 3, then your program should compute and display the number raised to the 6th power. Otherwise, your program should not display anything.

    import java.util.*;
    import java.math.*;
    public class Power
    {
    public static void main(String[]args)
    {
    String numb;
    System.out.print("Please enter a number: ");
    Scanner in = new Scanner(System.in);
    numb = in.next();
    int a = Integer.parseInt(numb);
    if(a % 2 ==0)
    {
    a = a * a;
    System.out.println("Your number squared is: "+a);
    }
    if(a %3 ==0)
    {
    a = a * a * a;
    System.out.println("Your number cubed is: "+a);
    }
    if(a % 2 ==0 && a %3 == 0 )
    {
    a = a*a*a*a*a*a;
    System.out.println("Your number raised to the 6th power is:
    " +a);
    }
    }
    }

    so close, no idea whats going on with the math here.

  7. #7
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    first, i'm gonna go ahead and assume that your specs imply that you should only output one thing, that is either x^2, x^3, or x^6. the way you've set it up, you're gonna print all 3 if the latter case is true. if you aren't sure, ask someone about it, because an error like that can cost you a whole grade.

    second, what cases are you getting incorrect answers? the way it looks, you're practically mauling the case where the input is divisible by both 2 and 3. say the input = 6. you will out put 36, 46656, and 10314424798490535546171949056. at least i think that's how it would go. this would also not be a problem if my original assumption is correct.

  8. #8
    DrKush's Avatar
    DrKush is offline Member
    Join Date
    Feb 2009
    Location
    Charloot NC
    Posts
    14
    Rep Power
    0

    Default

    good news:
    fixed the code, had to use else to stop from displaying other output
    bad news:
    code shows incorrect answer due to text format within livelab system
    ty all who helped me. cheers

    import java.util.*;

    public class Power
    {
    public static void main(String[]args)
    {

    String numb;


    System.out.println("Please enter a number: ");

    Scanner in = new Scanner(System.in);
    numb = in.nextLine();

    int a = Integer.parseInt(numb);


    if(a % 2 ==0 && a %3 == 0 )
    {
    a = a*a*a*a*a*a;
    System.out.println(numb+" to the 6th power is: "+a);
    }
    else
    if(a % 2 ==0)
    {
    a = a * a;
    System.out.println(numb+" squared is: "+a);
    }
    else
    if(a %3 == 0)
    {
    a = a * a * a;
    System.out.println(numb+" cubed is: "+a);
    }
    }
    }
    Last edited by DrKush; 02-25-2009 at 02:58 AM.

  9. #9
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Question...

    What happens when the user inputs a number that is not a multiple of 2 or 3? Like 7 ...

    Luck,
    CJSL
    Last edited by CJSLMAN; 02-25-2009 at 03:57 AM. Reason: making it readable...
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  10. #10
    DrKush's Avatar
    DrKush is offline Member
    Join Date
    Feb 2009
    Location
    Charloot NC
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by DrKush View Post
    Otherwise, your program should not display anything.

    er...nothing lol

  11. #11
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    Is that by design/requirement? Just curious...

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  12. #12
    DrKush's Avatar
    DrKush is offline Member
    Join Date
    Feb 2009
    Location
    Charloot NC
    Posts
    14
    Rep Power
    0

Posting Permissions

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