If statements...same program, same problem

if statement error else without if

Page 1 of 1

10 Replies - 1187 Views - Last Post: 06 November 2008 - 11:13 PM Rate Topic: -----

#1 CherylP  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 30-September 08

If statements...same program, same problem

Post icon  Posted 06 November 2008 - 05:38 PM

Okay. I don't know what I'm doing wrong. I am working on this as well as other projects, but this one is really confusing me. Maybe it's a conceptual error by me; I don't know. I know that I have used Google, my book, and read the Java Sun site on if statements, and yet I am still getting these errors and don't know why. Here is what I have tried...

1. Making them all if statements

2. Making line 31 "else if" and line 36 "else", in which I recieved the errors "else without if" on lines 33, and "else without if", "not a statement", and "; expected" on line 38

3. Unbracketing the if statements and ending the executing statements with a semicolon

4. Including the (number / 60), (number / 3600), and (number / 86400) in the brackets in the if statement, in which I get the errors "Unexpected type" error for lines 26, 32, and 37

5. I also changed a few things around a bit, trying different methods for different things, and then I received the error "variable "totalDays" might not have been initialized" for line 30, and error "variable "totalHours" might not have been initialized" on line 36, but line 31 is fine.

Should it be this complicated? I am very new to Java, so thank you for your patience.

	public static void main(String[] args) {
	
int number; //To store the number of seconds
int totalMinutes;
int totalHours; 
int totalDays; //To store and display the total minutes
String input; //To store userís input

Scanner keyboard = new Scanner(System.in);

System.out.println("Please enter any number of seconds greater than or equal to 60.");
number = keyboard.nextInt(); 

if (number >= 86400)
totalDays = (number / 86400);
{
System.out.println("There are " + totalDays + " in " + number + "  seconds.");
}

if (number >= 3600)
totalHours = (number / 3600);
{
System.out.println("There are " + totalHours + " in " + number + " seconds.");
}
if (number >= 60)
{
totalMinutes = (number / 60);
System.out.println("There are " + totalMinutes + " in " + number + " seconds.");	
}

}




Is This A Good Question/Topic? 0
  • +

Replies To: If statements...same program, same problem

#2 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 550
  • View blog
  • Posts: 5,624
  • Joined: 20-March 08

Re: If statements...same program, same problem

Posted 06 November 2008 - 05:48 PM

Your braces are off, check those again.

if (...)
{
    totalDaysORHoursORMinutes = ...;
    System.out.println(...);
}


They should all be in that format, correct? Give it a double check. :)

This post has been edited by Locke37: 06 November 2008 - 05:50 PM

Was This Post Helpful? 0
  • +
  • -

#3 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: If statements...same program, same problem

Posted 06 November 2008 - 06:00 PM

As I remember it, you were only supposed to print the result either in days, hours or minutes depending on the size of the input value. Currently your program would print all values (days, hours and minutes). To correct this, you should put back the if/else if/else (in that order). You're on the right track though since you changed the order of the values to compare with the largest first.

This post has been edited by Gloin: 06 November 2008 - 06:02 PM

Was This Post Helpful? 0
  • +
  • -

#4 CherylP  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 30-September 08

Re: If statements...same program, same problem

Posted 06 November 2008 - 06:00 PM

View PostLocke37, on 6 Nov, 2008 - 04:48 PM, said:

Your braces are off, check those again.

if (...)
{
    totalDaysORHoursORMinutes = ...;
    System.out.println(...);
}


They should all be in that format, correct? Give it a double check. :)


Okay...I may sound like a total idiot, but we haven't covered that in the book...maybe you should dumb it down for me. I went ahead and tried replacing the variables with that, and declaring a new one (what could it hurt?), and it didn't work. The assignment is for it only to print out the number of minutes if there are enough seconds in the minutes, and so on, so it doesn't have to print out all the days, minutes, and hours if that helps in any way possible.
Was This Post Helpful? 0
  • +
  • -

#5 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 550
  • View blog
  • Posts: 5,624
  • Joined: 20-March 08

Re: If statements...same program, same problem

Posted 06 November 2008 - 06:07 PM

Oh, I get it now. This should clear it up a little.

// self-explanatory
if (number >= 86400)
{
    totalDays = number / 86400;
    System.out.println("There are " + totalDays + " in " + number " seconds.");
}

// if number is less than 86400 and greater than or equal to 3600
else if (number >= 3600)
{
    totalHours = number / 3600);
    System.out.println("There are " + totalHours + " in " + number + " seconds.");
}

// if number is less than 3600 and greater than or equal to 60
else if (number >= 60)
{
    totalMinutes = number / 60;
    System.out.println("There are " + totalMinutes + " in " + number + " seconds.");
}

// if it's lower than 60
else
{
    // if NONE of the above are true, which means
    // they entered less than 60, give them
    // an error message or something.
}


Hope this helps! :D

This post has been edited by Locke37: 06 November 2008 - 06:09 PM

Was This Post Helpful? 0
  • +
  • -

#6 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: If statements...same program, same problem

Posted 06 November 2008 - 06:08 PM

It sounds like you don't quite understand how the if-statements work, would you like an explanation?
Was This Post Helpful? 0
  • +
  • -

#7 CherylP  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 30-September 08

Re: If statements...same program, same problem

Posted 06 November 2008 - 06:31 PM

View PostGloin, on 6 Nov, 2008 - 05:08 PM, said:

It sounds like you don't quite understand how the if-statements work, would you like an explanation?


No, thanks though. :D I understand how they work, but honestly, I have been trying a little bit of everything to try and get this code to work. Originally, I had the if, else if, and else statements in the proper places, but thought changing things up might help...now, I am back to getting the original errors I was dealing with. Here is what I added, and I put the error codes next to the else if statements that are causing them.

	public static void main(String[] args) {
	
int number; //To store the number of seconds
int totalMinutes;
int totalHours; 
int totalDays; //To store and display the total minutes
String input; //To store userís input

Scanner keyboard = new Scanner(System.in);

System.out.println("Please enter any number of seconds greater than or equal to 60.");
number = keyboard.nextInt(); 

if (number >= 86400)
totalDays = number / 86400;
{
System.out.println("There are " + totalDays + " in " + number + "  seconds.");
}
else if (number >= 3600) //ERROR: ELSE WITHOUT IF
totalHours = number / 3600;
{
System.out.println("There are " + totalHours + " in " + number + " seconds.");
}
else if (number >= 60) //ERROR: ELSE WITHOUT IF
{
totalMinutes = number / 60;
System.out.println("There are " + totalMinutes + " in " + number + " seconds.");	
}
else (number <= 59) //ERROR: NOT A STATEMENT,; EXPECTED
{
System.out.println("Invalid number, please try again.");
}

}

	}



Was This Post Helpful? 0
  • +
  • -

#8 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: If statements...same program, same problem

Posted 06 November 2008 - 06:32 PM

This is why I'm asking if you need them explained. You're obviously not getting thm right.
Was This Post Helpful? 0
  • +
  • -

#9 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 550
  • View blog
  • Posts: 5,624
  • Joined: 20-March 08

Re: If statements...same program, same problem

Posted 06 November 2008 - 06:33 PM

Hint:

Replace all of your code, starting with those if statements and ending before the main method/class ends, with mine.

:lol:

Just look at the code I posted.
Was This Post Helpful? 0
  • +
  • -

#10 CherylP  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 30-September 08

Re: If statements...same program, same problem

Posted 06 November 2008 - 06:56 PM

First off, thank you very much! The code works perfectly.

Secondly, just so I understand...I didn't have an else statement, my arithmetic was supposed to be included in the brackets, to tell the program what to execute, instead of outside, and I didn't have the else statement to tell the program what to do in the case of someone entering a number less than 60...I guess kind of like closing off the if statements.

I apologize if I am being sorely repetitive, and if I've hit my mark, no need to reply. This is what I want to do for a living when I am finished with school, and I want to make sure I completely understand.

No on to comparing string objects! (And thanks to both, once again, for being so patient!)

This post has been edited by CherylP: 06 November 2008 - 06:57 PM

Was This Post Helpful? 0
  • +
  • -

#11 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 550
  • View blog
  • Posts: 5,624
  • Joined: 20-March 08

Re: If statements...same program, same problem

Posted 06 November 2008 - 11:13 PM

You don't need the else statement, however, it's nice to have only so your program doesn't just exit when the user puts in 59 or lower. The user knows to do something else next time. ;)

View PostCherylP, on 6 Nov, 2008 - 05:56 PM, said:

No on to comparing string objects! (And thanks to both, once again, for being so patient!)


First off...don't make the rookie (including me, at first) mistake of using the == with Strings...

String str1 = "HELLO";
String str2 = "hello";

if (str.equals(str2)) // this will ALWAYS return false in this case.


Make sure to use the equals() method! :D

some_string.equals(another_string_possibly_a_variable)

:)

This post has been edited by Locke37: 06 November 2008 - 11:18 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1