• Hello Guest! Did you know that ProjectKorra has an official Discord server? A lot of discussion about the official server, development process, and community discussion happens over there. Feel free to join now by clicking the link below.

    Join the Discord Server

Exercise Help

HydroMan

Verified Member
So recently I began a training with java beginning course and I came across a problem where i couldn't solve the second exercise after I completed the first. So the exercise tells me...
Problem: The Martians are very pleased with your hospitality, but they are quite amused that you hard coded the greeting message in System.out.println(“Hello Martians”); in your first exercise. They would really prefer to use a variable to hold the value of the greeting and print the variable instead of a hard coded message. Additionally they also want you to print your age. The local geek has already created two variables for you, 'greeting', and 'age', which will hold the greeting text and age respectively.. You are supposed to give values to the variables such that “Hello Martians I am 27 years old” will be printed when the program is run.

What we expect: We expect the String “Hello Martians I am 27 years old” to appear on the system console. We also expect that the variables ‘greeting’ and ‘age’ will have the appropriate values of ‘Hello Martians’ and 27 respectively.

Hint: You can print multiple values on the system console, by using the ‘+’ operator

Learning Outcomes: After completing this exercise you should have understood:
● What is a variable
● How to declare a variable that can contain a String
● How to declare a variable that can contain an integer
● How to assign a value to a variable
● How to print multiple values to the system console
After with a little help with someone, I manage to solve the problem some of the errors, but there still two more errors. Here's the set up:
Code:
public class BetterHello {

   public static void main(String[] args) {
     String greeting; "Hello Martians I am";
     int age; 27;
     
     System.out.printIn(greeting + age);
   
  }

}
And here are the final errors...
Error
File:
BetterHello.java
Line:
4
Description: not a statement String greeting; "Hello Martians I am"; ^

Error
File:
BetterHello.java
Line:
5
Description: not a statement int age; 27; ^
 

Zafkiel

Verified Member
Try removing the ";" in the fourth line (the one in the middle after "String greeting") and keep the last one
and in the 5th one remove the ";" after "int age" and keep the last one :D
So it would looks like :

public class BetterHello {

public static void main(String[] args) {
String greeting "Hello Martians I am";
int age 27;

System.out.printIn(greeting + age);

}

}
 

HydroMan

Verified Member
Try removing the ";" in the fourth line (the one in the middle after "String greeting") and keep the last one
and in the 5th one remove the ";" after "int age" and keep the last one :D
So it would looks like :

public class BetterHello {

public static void main(String[] args) {
String greeting "Hello Martians I am";
int age 27;

System.out.printIn(greeting + age);

}

}
Didn't help. It actually altered those errors. Now re-adding those ";" back for the moment.
 

Carbogen

Verified Member
So recently I began a training with java beginning course and I came across a problem where i couldn't solve the second exercise after I completed the first. So the exercise tells me...
After with a little help with someone, I manage to solve the problem some of the errors, but there still two more errors. Here's the set up:
Code:
public class BetterHello {

   public static void main(String[] args) {
     String greeting; "Hello Martians I am";
     int age; 27;
    
     System.out.printIn(greeting + age);
  
  }

}
And here are the final errors...
Error
File:
BetterHello.java
Line:
4
Description: not a statement String greeting; "Hello Martians I am"; ^

Error
File:
BetterHello.java
Line:
5
Description: not a statement int age; 27; ^
When defining a variable (such as greeting or age), you need to use the equals symbol, =.

Try the following:
Code:
public class BetterHello {

   public static void main(String[] args) {
     String greeting = "Hello Martians I am";
     int age = 27;
    
     System.out.printIn(greeting + age);
  
  }

}
Mind you, as your code is currently set up, there will not be a space between greeting and age, making it look like "Hello Martians I am27". There is a very easy fix to this, let's see if you can figure it out ;)

Best of luck!
 

Zafkiel

Verified Member
When defining a variable (such as greeting or age), you need to use the equals symbol, =.

Try the following:
Code:
public class BetterHello {

   public static void main(String[] args) {
     String greeting = "Hello Martians I am";
     int age = 27;
   
     System.out.printIn(greeting + age);
 
  }

}
Mind you, as your code is currently set up, there will not be a space between greeting and age, making it look like "Hello Martians I am27". There is a very easy fix to this, let's see if you can figure it out ;)

Best of luck!
Ah this was so Dumb I didn't realize it XD
 

Simplicitee

Staff member
Plugin Developer
Verified Member
When defining a variable (such as greeting or age), you need to use the equals symbol, =.

Try the following:
Code:
public class BetterHello {

   public static void main(String[] args) {
     String greeting = "Hello Martians I am";
     int age = 27;
    
     System.out.printIn(greeting + age);
  
  }

}
Mind you, as your code is currently set up, there will not be a space between greeting and age, making it look like "Hello Martians I am27". There is a very easy fix to this, let's see if you can figure it out ;)

Best of luck!
Pssssst hydro: greeting + " " + age
 

OmniCypher

Staff member
Lead Developer
Administrator
Plugin Developer
public class BetterHello {

public static void main(String[] args) {
String greeting = "Hello Martians I am";
int age = 27;

System.out.printIn(greeting + " " + age);

}

}

This will result in the output "Hello Martians I am 27"
 

AlexTheCoder

Staff member
Plugin Developer
Verified Member
public class BetterHello {

public static void main(String[] args) {
String greeting = "Hello Martians I am";
int age = 27;

System.out.printIn(greeting + " " + age);

}

}

This will result in the output "Hello Martians I am 27"
Slightly more in-depth than this exercise, you could request a name in your main method to replace Martians with
 

HydroMan

Verified Member
2 erros are gone ,1 appeared;
Error
File:
BetterHello.java
Line:
7
Description: cannot find symbol System.out.printIn(greeting + " " + age); ^ symbol: method printIn(String) location: variable out of type PrintStream
 

HydroMan

Verified Member
2 erros are gone ,1 appeared;
Error
File:
BetterHello.java
Line:
7
Description: cannot find symbol System.out.printIn(greeting + " " + age); ^ symbol: method printIn(String) location: variable out of type PrintStream
I alto tried to add to add space and ^ after
Code:
System.out.printIn(greeting + " " + age);
but didn't work and cause three other errors. So now i removed it
 

HydroMan

Verified Member
The Set so far is:
Code:
public class BetterHello {

   public static void main(String[] args) {
      String greeting = "Hello Martians I am";
     int age = 27;
    
     System.out.printIn(greeting + " " + age);
  
  }

}
 

HydroMan

Verified Member
I alto tried to add to add space and ^ after
Code:
System.out.printIn(greeting + " " + age);
but didn't work and cause three other errors. So now i removed it
The Set so far is:
Code:
public class BetterHello {

   public static void main(String[] args) {
      String greeting = "Hello Martians I am";
     int age = 27;
   
     System.out.printIn(greeting + " " + age);
 
  }

}
Never mind, I fixed it myself. It seem that
Code:
     System.out.printIn(greeting + " " + age);
was unnecessary after i removed it from the thing. Thank you all for all the help. couldn't have done everthing myself. :)
 
Top