Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Do While Loop cant get this one working

I cant figure out how to ask the question again because I cant redefine time as a string then integer again can I? The Loop gets to "what time is it now?" And continues to loop even if time is above 18

import java.io.Console;

public class TreeStory {

    public static void main(String[] args) {
        Console console = System.console();

  String name = console.readLine("Enter your name ");
      String food = console.readLine("Enter your favourite food ");
      String timeAsString = console.readLine("Enter the time ");
      int time = Integer.parseInt(timeAsString);

      if (time<18){
       console.printf("It is not dinner time yet\n"); 
      }
      do {
          timeAsString=console.readLine("What time is it now? \n");
      } while(time<18);
      console.printf("%s its time to eat %s its Dinner time!", name, food);

} 
}    

1 Answer

Never mind i figured it out!

import java.io.Console;

public class TreeStory {

    public static void main(String[] args) {
        Console console = System.console();

  String name = console.readLine("Enter your name ");
  String food = console.readLine("Enter your favourite food ");
  int time;
    String timeAsString;
      do {
       timeAsString = console.readLine("Enter the time ");
       time = Integer.parseInt(timeAsString);
      if (time<18){
       console.printf("It is not dinner time yet\n"); 
      }
      } while(time<18);
      console.printf("%s its time to eat %s its Dinner time!", name, food);   
} 
}