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 12 of 12

Thread: pythagorean triples using a for loop

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default pythagorean triples using a for loop

    I have generated many errors in my code for this program but I particularly want to focused on the first error that comes up. Here is the outline of my code:

    //a java application that prints the table of pythagorean triples that go up to 500 using a for loop
    public class luby{
     
    	//main method executes java application
     
    	public static void main(String args[]){
     
    //declare variables
     
     
     
    for(int side_1=1; int side_1 <=500;  side_1++){
     
    	for(int side_2=1; int side_2 <=500; side_2++){
     
    		for(int hypotenuse=1; int hypotenuse <=500; hypotenuese++){
     
    			Math.pow(hypotenuese,2)=Math.pow(side_1,2)+Math.pow(side_2,2);
     
    			System.out.printf("n/%d/n", Math.pow(side_1,2),"                 ", Math.pow(side_2,2),"                 ", Math.pow(hypotenuse,2));
     
     
     
    } //end first for loop
     
    }//end second for loop
     
    }
     
    	 //end third for loop
     
    }//end main
     
    }//end class

    Here is the output of my code when I attempt to compile it:


    C:\Users\Courtney\Desktop\chapter five exercises for java\luby.java:12: error: '.class' expected
    for(int side_1=1; int side_1 <=500; side_1++){
    ^
    C:\Users\Courtney\Desktop\chapter five exercises for java\luby.java:12: error: illegal start of expression
    for(int side_1=1; int side_1 <=500; side_1++){
    ^
    C:\Users\Courtney\Desktop\chapter five exercises for java\luby.java:12: error: ')' expected
    for(int side_1=1; int side_1 <=500; side_1++){
    ^
    C:\Users\Courtney\Desktop\chapter five exercises for java\luby.java:12: error: ';' expected
    for(int side_1=1; int side_1 <=500; side_1++){
    ^
    C:\Users\Courtney\Desktop\chapter five exercises for java\luby.java:14: error: '.class' expected
    for(int side_2=1; int side_2 <=500; side_2++){
    ^
    C:\Users\Courtney\Desktop\chapter five exercises for java\luby.java:14: error: illegal start of expression
    for(int side_2=1; int side_2 <=500; side_2++){
    ^
    C:\Users\Courtney\Desktop\chapter five exercises for java\luby.java:14: error: ')' expected
    for(int side_2=1; int side_2 <=500; side_2++){
    ^
    C:\Users\Courtney\Desktop\chapter five exercises for java\luby.java:14: error: ';' expected
    for(int side_2=1; int side_2 <=500; side_2++){
    ^
    C:\Users\Courtney\Desktop\chapter five exercises for java\luby.java:16: error: '.class' expected
    for(int hypotenuse=1; int hypotenuse <=500; hypotenuese++){
    ^
    C:\Users\Courtney\Desktop\chapter five exercises for java\luby.java:16: error: illegal start of expression
    for(int hypotenuse=1; int hypotenuse <=500; hypotenuese++){
    ^
    C:\Users\Courtney\Desktop\chapter five exercises for java\luby.java:16: error: ')' expected
    for(int hypotenuse=1; int hypotenuse <=500; hypotenuese++){
    ^
    C:\Users\Courtney\Desktop\chapter five exercises for java\luby.java:16: error: ';' expected
    for(int hypotenuse=1; int hypotenuse <=500; hypotenuese++){
    ^
    12 errors

    Tool completed with exit code 1



    The first error I want to focus on is the class error; I will focus on the remaining errors once this error is resolved


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: pythagorean triples using a for loop

    What is the contents of the commandline when you get these errors?
    Can you copy the full contents of the command prompt window and paste it here?

    When you post errors with the ^ showing where the error is located, be sure to wrap the messages in code tags to preserve the location of the ^

    To wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    May 2014
    Posts
    36
    Thanks
    6
    Thanked 3 Times in 3 Posts

    Default Re: pythagorean triples using a for loop

    You may want to look over your loops for some spelling errors..
    English is not my native language (Typo alert).

  4. #4
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: pythagorean triples using a for loop

    If I try to compile this code I get very different errors. Are you sure you copied the right output?

  5. #5
    Junior Member
    Join Date
    Apr 2014
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: pythagorean triples using a for loop

    Fixed most of my errors. Now only one error is generated when I attempt to compile the code:

    //a java application that prints the table of pythagorean triples that go up to 500 using a for loop
    public class luby{
     
    	//main method executes java application
     
    	public static void main(String args[]){
     
    //declare variables
     
     
     
    for(int side_1=1;  side_1 <=500;  side_1++){
     
    	for(int side_2=1;  side_2 <=500; side_2++){
     
    		for(int hypotenuse=1;  hypotenuse <=500; hypotenuse++){
     
    			Math.pow(hypotenuse,2)=Math.pow(side_1,2)+Math.pow(side_2,2);
     
    			System.out.printf("n/%d/n", Math.pow(side_1,2),"                 ", Math.pow(side_2,2),"                 ", Math.pow(hypotenuse,2));
     
    }
     
    }//end second for loop
     
    }
     
    	 //end third for loop
     
    }//end main
     
    }//end class




    C:\Users\Courtney\Desktop\chapter five exercises for java\luby.java:18: error: unexpected type
    Math.pow(hypotenuse,2)=Math.pow(side_1,2)+Math.pow (side_2,2);
    ^
    required: variable
    found: value
    1 error

    Tool completed with exit code 1

  6. #6
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: pythagorean triples using a for loop

    Could you try to explain to us what you are trying to do in the erroneous line?
    Try to explain it step by step:
    Math.pow(hypotenuse,2)=Math.pow(side_1,2)+Math.pow (side_2,2);

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: pythagorean triples using a for loop

    When you post errors with the ^ showing where the error is located, be sure to wrap the messages in code tags to preserve the location of the ^. Otherwise the ^ is in the first column, not under the error.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Apr 2014
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: pythagorean triples using a for loop

    Quote Originally Posted by Cornix View Post
    Could you try to explain to us what you are trying to do in the erroneous line?
    Try to explain it step by step:
    Math.pow(hypotenuse,2)=Math.pow(side_1,2)+Math.pow (side_2,2);
    I want to displayed a table of pythagorean triples that until the values reach 500.The code above is supposed to be the equation for the pythagorean theorem and when I attempt to compile it, java doesn't recognize it as a statement.

  9. #9
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: pythagorean triples using a for loop

    Because it isnt.
    You have a value on the left hand side of the assignment operator, and a value on the right hand side.

    It is as if you would write "5 = 2 + 2". Its not an assignment, its not even a valid statement in java.
    The assignment operator must be used with a variable on the left hand side and an expression on the right hand side.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: pythagorean triples using a for loop

    It looks like a condition to be tested, not an assignment statement.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Apr 2014
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: pythagorean triples using a for loop

    Quote Originally Posted by Cornix View Post
    Because it isnt.
    You have a value on the left hand side of the assignment operator, and a value on the right hand side.

    It is as if you would write "5 = 2 + 2". Its not an assignment, its not even a valid statement in java.
    The assignment operator must be used with a variable on the left hand side and an expression on the right hand side.
    I used double equal signs
    Math.pow(hypotenuse,2)==Math.pow(side_1,2)+Math.po w(side_2,2);
    and that didn't even work

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: pythagorean triples using a for loop

    The == operator returns a boolean value: true or false. The code needs have a statement that uses that boolean value to make a decision.

    See the tutorial: The if-then and if-then-else Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Pythagorean theorem java applet problem
    By voodoo664 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 3rd, 2014, 08:51 AM
  2. [SOLVED] A linearithmic (at most) complexity algorithm to generate Pythagorean triples
    By angstrem in forum Algorithms & Recursion
    Replies: 4
    Last Post: May 26th, 2013, 07:20 PM
  3. For loop, the first command in the loop does not get executed the 2nd time..
    By lina_inverse in forum Loops & Control Statements
    Replies: 1
    Last Post: October 16th, 2012, 09:00 PM
  4. Pythagorean Triples Help- I'm a Beginner :/
    By tryingtolearn in forum Loops & Control Statements
    Replies: 9
    Last Post: July 1st, 2012, 01:27 AM
  5. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 08:57 PM