Reputation: 21
I devided this code into three parts; Main, DisSystem, DisIndexerUI. Its purpose is to calculate a Discomfort Index. The calculation system works perfectly, but I find out that the printed result has something wrong. This is the result:
input temperature
36
input humidity
42
Discomfort Index:96.75999999999999
Very very uncomfortable
It is
I think it should print "It is very very uncomfortable", but the result I printed is
Very very uncomfortable
It is
It's a very weird situation. I would like to know why it happens.
Of course I know that, if I fix it like this:
System.out.println("Discomfort Index:"+ ui.input());
System.out.print("It is ");
ui.status();
It will print "It is Very very uncomfortable". But it is only a temporary solution, I think. Why is it line breaking ?
public class Main {
public static void main(String[] args) {
double temp = 0;
double humidity = 0;
DisSystem dis = new DisSystem();
DisIndexerUI ui = new DisIndexerUI(dis);
System.out.println("Discomfort Index:"+ ui.input());
System.out.println("It is" + ui.status());
}
}
==================================
public class DisSystem {
public static double Formula(double temp, double humidity) {
double formula = 0.72 * (temp + humidity) + 40.6;
return formula;
}
}
=================================
import java.util.Scanner;
public class DisIndexerUI {
DisSystem disSystem;
double formula = 0;
public DisIndexerUI(DisSystem disSystem) {
this.disSystem = disSystem;
}
public double input() {
Scanner scanner = new Scanner(System.in);
System.out.println("input temperature");
double temp = Double.parseDouble(scanner.nextLine());
System.out.println("input humidity");
double humidity = Double.parseDouble(scanner.nextLine());
formula = disSystem.Formula(temp, humidity);
return formula;
}
public String status() {
String str = "";
if (formula >= 86) {
System.out.println("Very very uncomfortable");
} else if (formula >= 83) {
System.out.println("Very uncomfortable");
} else if (formula >= 80) {
System.out.println("uncomfortable");
} else if (formula >= 75) {
System.out.println("nomal");
} else if (formula >= 70) {
System.out.println("good");
} else if (formula >= 68) {
System.out.println("very good");
}
return str;
}
}
Upvotes: 1
Views: 88
Reputation: 8086
Quick fix:
Replace System.out.println("It is" + ui.status());
with ui.status()
;
Make System.out.print("It is ");
as first statement in status
method.
Better way: Follow Thomas's advice - comment on question.
Edit:
Actually, the code isn't breaking, its behaving as expected.
In the main
method, a call is made to System.out.println("It is" + ui.status());
. The point to note here is that status
method will first execute and return a value. Then, the returned value(which is empty string as declared in that method) will be concatenated with the string - "It is". So, when execution jumps to status
method, you are printing strings depending on conditions. So, first any of those strings will be printed then, a value will be returned. Hence, you are seeing the output which is as expected.
Upvotes: 0
Reputation: 1109
This statement
System.out.println("It is" + ui.status());
makes a call to status member of DisIndexerUI instance ui which first prints the the literal and a line breaker then returns empty string which is concatenated to the first operand and then the evaluated "It is" + "" is printed. Changing the status() method of DisIndexerUI to return the literal instead of printing it will fix the issue.
public String status() {
String str = "";
if (formula >= 86) {
str = "Very very uncomfortable";
} else if (formula >= 83) {
str = "Very uncomfortable";
} else if (formula >= 80) {
str = "uncomfortable";
} else if (formula >= 75) {
str = "nomal";
} else if (formula >= 70) {
str = "good";
} else if (formula >= 68) {
str = "very good";
}
return str;
}
Upvotes: 1
Reputation: 2444
You just return an empty string in the status
method, but print the actual value.
public String status() {
if (formula >= 86) {
return "Very very uncomfortable";
} else if (formula >= 83) {
return "Very uncomfortable";
} else if (formula >= 80) {
return "uncomfortable";
} else if (formula >= 75) {
return "nomal";
} else if (formula >= 70) {
return "good";
} else if (formula >= 68) {
return "very good";
} else {
return "";
}
}
Furthermore check whether the empty string in the else statement is correct for your use case.
Upvotes: 2
Reputation: 1923
You are using System.out.println("Text"); which move the cursor to the next line after printing the string. if you want the string to be in the same line via multiple print statement then use System.out.print("Text");.
Upvotes: 1