Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Basic Java Question, variable in a sentence?

Hey ya'll, I'm in a Java class at school and I completed the assignment. It went like this...

public class DaysOld

{

public static void main(String[] args)

{

int myAge = 18;

int days = myAge*365;

int hours = days*24;

int minutes = hours*60;

int seconds = minutes*60;

System.out.println("I am " + myAge);

System.out.println("Days old is " + days);

System.out.println("Hours old is " + hours);

System.out.println("Minutes old is " + minutes);

System.out.println("Seconds old is " + seconds);

}

}

My question is I want to change the sentence structure to say ("I am " + myAge "years old"); but i am getting an error when i try and insert the variable in the middle of the sentence, how can I accomplish this?

Thanks!

1 Answer

Relevance
  • 1 decade ago
    Favorite Answer

    If you are using:

    System.out.println("I am " + myAge "years old");

    Then you need a plus (+) between myAge and the next string literal:

    System.out.println("I am " + myAge + "years old");

Still have questions? Get your answers by asking now.