Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: What's wrong with my code?

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Post What's wrong with my code?

    Hello, I'm working on my Computer Science project but I can't figure out what's wrong with my code!


    for (int x = 1; x <= Expo; x++)
    {
    for (int i = 1; i <= Expo; i++)
    {
    Output.setText(Base + " to the exponent " + i + " = " + pow(Base,i) + "\n");
    }
    }

    The output I'm trying to get looks something like this (assuming the base is 2 and the exponent is 10):

    2 to the power of 1 = 2
    2 to the power of 2 = 4
    2 to the power of 3 = 8
    2 to the power of 4 = 16
    2 to the power of 5 = 32
    2 to the power of 6 = 64
    2 to the power of 7 = 128
    2 to the power of 8 = 256
    2 to the power of 9 = 512
    2 to the power of 10 = 1024

    But the output that I'm getting (still assuming the base is 2 and the exponent is 10):

    2 to the power of 10 = 1024

    Please help! Thanks, Rara


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,198
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: What's wrong with my code?

    Are you saying that your code only prints out one line and not 10 lines?

    What class is the Output variable? What does the setText() method do?
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Rara (April 6th, 2013)

  4. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: What's wrong with my code?

    Yah the code will only print out one line, and setText(); is just to output the code into the designated output area, I'm using netbeans. I've tried System.out.println(); but the same thing happens, just one line. And Base along with Expo are just the names of the inputs. I have to go to bed right now but I look forward to a response, thanks.

    ~Ryan

  5. #4
    Junior Member
    Join Date
    Apr 2013
    Posts
    25
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: What's wrong with my code?

    Hi,

    You just need to use one loop that goes until the expo = 10 is reached.
    so you need to modify your code as
    say if your input variable base = 2 and Expo = 10

    for (int x = 1; x <= Expo; x++)
    {

    System.out.println(base + " to the exponent " + x + " = " + Math.pow(base,x) + "\n");
    }

    hope this will solve your problem

  6. The Following User Says Thank You to hope_08 For This Useful Post:

    Rara (April 6th, 2013)

  7. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,198
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: What's wrong with my code?

    You missed answering these question;

    What class is the Output variable?
    What does the setText() method do?

    I've tried System.out.println(); but the same thing happens, just one line.
    Are you sure? I get 100 lines.
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    Rara (April 6th, 2013)

  9. #6
    Junior Member
    Join Date
    Apr 2013
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: What's wrong with my code?

    I tried it with your code it worked but it went into the console rather than the application. When I used System.setText(); only one line shows up and I'm not sure why. When I use your code it will show all of the exponents which I wanted. So I'm not sure how to fix the error that's occurring in Netbeans. Thanks for helping me though, it means a lot!

    --- Update ---

    Sorry for missing the questions Norm. I'm new to Java so I'm not sure what class the Output variable is, but the setText(); method is to take the code from above and put it into a specific location in the application.

    private void CalculateActionPerformed(java.awt.event.ActionEven t evt) {
    String text1, text2;
    int Base, Expo;
    text1 = Ba.getText();
    text2 = Ex.getText();
    Base = Integer.parseInt(text1);
    Expo = Integer.parseInt(text2);

    for (int x = 1; x <= Expo; x++)
    {

    Output.setText(Base + " to the exponent " + x + " = " + Math.pow(Base,x) + "\n");
    }



    }

    I think the class that it's in is CalculateActionPreformed.

  10. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,198
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: What's wrong with my code?

    I'm not sure what class the Output variable is
    You must find the definition for Output to get what class it is.
    When you find the class, then you must look in the API doc for that class and see what the setText() method does.

    The API doc is at: Java Platform SE 7
    If you don't understand my answer, don't ignore it, ask a question.

  11. #8
    Junior Member
    Join Date
    Apr 2013
    Posts
    25
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: What's wrong with my code?

    If Output is as swing component like JLabel then you can use this setText method to set its text to the output you got earlier.
    You just need to use for eg if you are directing your output to be shown on a Jlabel

    JLabel Output= new JLabel();
    int Base=2;
    for (int x = 1; x <= 10; x++)
    {
    string = string + Integer.toString(Base) + "to the exponent " + Integer.toString(x) + " = " + Double.toString(Math.pow(Base,x)) ;

    Output.setText( string );

    }

    The result will be displayed on the label. Hope this solves your problem now

  12. #9
    Junior Member
    Join Date
    Apr 2013
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: What's wrong with my code?

    I contacted my teacher and the solution to my problem was this:

    Output.setText(Output.getText() + Base + " to the exponent " + x + " = " + Math.pow(Base,x) + "\n");

    I appreciate all of the time you put in to help me, it's good to know people are willing to help others that they don't know. Thanks a lot!

Similar Threads

  1. What Am I doing Wrong In This Code?
    By RadiusUnknown in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 14th, 2012, 03:00 PM
  2. What's wrong with my code?
    By mjballa in forum What's Wrong With My Code?
    Replies: 8
    Last Post: November 19th, 2011, 02:57 PM
  3. what's wrong with my code
    By gcsekhar in forum Java Networking
    Replies: 2
    Last Post: July 19th, 2011, 09:31 AM
  4. Wrong code
    By whattheeff in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 4th, 2011, 04:30 PM