Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

If statement  RSS feed

 
Stephen Donovn
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again

I'm almost there in a program I've writing for an assignment.

I'm stuck in 2 places and appreciate any help.

1. If statement (line 93-107) - Depending on the time (timeis) if it's 6.00 > 20.00 I want to draw the happy face otherwise the sad face. I've written the code as below but getting error 'else without the if'. What am I doing wrong?
2. I'm also trying to add either am or pm depending what was inputted (line28) to the timeis text field but not sure where I should do this.... line 31?

It's probably something obvious but I can't see it.

Thanks

Steve

 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy

I see you are using the AWT version of Label and TextField etc rather than the Swing version JLabel, JTextField etc.

To get the ampm textfield text, you can do ampm.getText() in the action listener.

Similarly once you get the timeis or time variable, you can append the ampm text to it to display accordingly. All done in the action listener. Once this is set, the label3 variable can be updated.


Edit: About the if block in line 93-107, I suggest you use the hour rather than the timeis variable. This makes things a little simpler I think.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephen Donovn wrote:I've written the code as below but getting error 'else without the if'. What am I doing wrong?

Code blocks require both an opening and closing curly brace and an if else statement consists of two code blocks.
 
Stephen Donovn
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies though I'm still having difficulty.

I'm trying to figure out the 'if/else' problem first before my other issue and taken the feedback onboard.

I'm using 'hours' as suggested and think I'm indenting correctly but still getting error - AssignmentIf.java:104: error: not a statement else (hours < 6 || hours >20).

Pasted the amended bit of code below. Appreciate I'm probably being stupid but getting myself in a right twist

[/code]
 
Rob Spoor
Sheriff
Posts: 21095
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 11 is missing a }.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've got an opening curly brace on line 7 but no matching closing curly brace.
Also you don't need the condition on line 11 - just the else.

If you had needed to test another condition on line 11, the syntax would have been you were missing the if
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also (again) the logic of your condition on line 6 is wrong. There is no value of hours for which that condition will be false.
 
Stephen Donovn
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
again thanks.

Inserted the } and changed to 'else if' but getting another error now -

AssignmentIf.java:96: error: incompatible types: String cannot be converted to int
int hours= timeis.getText();

The output of 'timeis' (as per first code posted) is hours and mins.

For my If/Else statement I want to get the hours from this field.

So if the mistake is at int hours= timeis.getText(); ... what should I put? I've tried different things for example getting the output from the string but still no joy.




 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The getText method returns a String - you can't assign a String to an int variable. The Integer class has methods for converting Strings to ints.
 
Stephen Donovn
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK thanks both for the help

I've managed to convert my string (timeis) back to int.... and added a System.out and can see the correct number is coming out.

I'll take a look tomorrow at the if statement for the drawing part.... Slowly getting there.

Cheers

Steve

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!