Reputation: 125
I'm trying to write a conditional statement such that if the conditions are met, then string oranges ="yes please". If not, then it should say "no, thanks". I intend to use the string oranges later on in a print statement like System.out.println("y"+ "m" + "d" + "oranges"). y, m and d are user inputs. Here's the relevant part of my code. What am I doing wrong?
(Experience level: just started out with Java a few days ago)
String oranges;
if ((y > 2777) && (m > 2) && (d > 22)){
String oranges = "yes please";
}
else
{String oranges = "no, thanks";
}
Upvotes: 1
Views: 221
Reputation: 189
Here's some code which you can use to achieve your objective and includes the System.out.printf(String, Object...)
method for printing (this method provides a lot more control over output format than System.out.println(String)
alone; printf
documentation can be seen h̲e̲r̲e̲):
public class Solution {
public static void main(String[] args) {
// Code in which the variables y, m, and d are created and assigned proper values goes here.
String oranges = ((y > 2777) && (m > 2) && (d > 22)) ? "yes please" : "no, thanks";
System.out.printf("%d %d %d %s", y, m, d, oranges);
}
}
Upvotes: 0
Reputation: 541
You should use your code as
String oranges="";
//declare y,m and d and initialize with some value or read data from user
//int y,d,m;
if ((y > 2777) && (m > 2) && (d > 22)){
oranges = "yes please";
}
else
{
oranges = "no, thanks";
}
System.out.println(y+" "+m+" "+d+" "+oranges);
Upvotes: 0
Reputation: 1335
The problem is the redeclaring the datatype of orange, the first declarations is fine then the second issue is your output should not have oranges in quotes as that will not output the value of oranges variable. Putting quotes will result in oranges being outputted as a string.
Here is an example of what I just explained
public class JavaApplication1 {
public static void main(String[] args) {
String oranges = "Any thing you want";
int y = 0;
int m = 0;
int d = 0;
if ((y > 2777) && (m > 2) && (d > 22)){
oranges = "yes please";
}
else{
oranges = "no, thanks";
}
System.out.println(y+ m + d + oranges);
}
}
Upvotes: 1
Reputation: 3953
You could also go the cool route and try a ternary operator approach
String oranges = ((y > 2777) && (m > 2) && (d > 22)) ? "yes please" : "no, thanks";
syntax is ?=then, : = else
In this approach you only need one string variable.
Happy Java Coding!
Upvotes: 2
Reputation: 344
Please try:
String oranges,afterToday;
int x, y , d; //here code goes to get user input
if ((y > 2777) && (m > 2) && (d > 22)){
oranges = "yes please";
}else{
afterToday = "no, thanks";
}
System.out.println("Y = "+y "M = "+m + "D ="+d + "Oranges: "+oranges);
Upvotes: 0
Reputation: 1475
Your code can not complie successfully, you have declared oranges
twice. Take a look at global variable and local variable. The right should be this:
String oranges;
if ((y > 2777) && (m > 2) && (d > 22)){
oranges = "yes please";
}
else{
oranges = "no, thanks";
}
System.out.println(oranges);
Upvotes: 1