Tuesday, January 17, 2017

Programming By Doing Lesson 8

I ran into an error that I could not resolve using the command prompt on this one. The error I received was:

NumbersAndMath.java:7: error: unexpected type
        System.out.println( "Hens " = (25 + 30 /6 ) );
                            ^
  required: variable
  found:    value
1 error

I added a screenshot below since the copy/paste put the arrow in a different location:




I kept looking at the quote and the word Hen and was unable to figure out what the issue was.  It was not until I copied my code and pasted into Eclipse that I realized my error. In Eclipse it gave the error "The left-hand side of an assignment must be a variable". BINGO. It was then that I noticed I had accidentally typed an equals sign in this code instead of the + sign. Problem solved. I corrected and reran the code with no issues as below.

public class NumbersAndMath
{
public static void main (String[] args )
{
System.out.println( "I will now count my chickens:" );

System.out.println( "Hens " + (25 + 30 /6 ) );
System.out.println( "Roosters " + ( 100 - 25 * 3 % 4) );

System.out.println( "Now I will count the eggs:" );
System.out.println( 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6 );

System.out.println( "Is it true that 3 + 2 < 5 - 7?" );
System.out.println( 3 + 2 < 5 - 7 );

System.out.println( "What is 3 + 2? " + ( 3 + 2) );
System.out.println( "What is 5 - 7? " + (5 - 7) );
        System.out.println( "Oh, that's why it's false." );

System.out.println( "How about some more." );
System.out.println( "Is it greater? " + ( 5 > -2 ) );
System.out.println( "Is it greater or equal? " + ( 5 >= -2 ) );
System.out.println( "Is it less or equal? " + ( 5 <= -2 ) );
}
}


No comments:

Post a Comment