Search code examples
javaline-breaks

I don't know why this code does line breaking by itself


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;

    }

}    

Solution

  • 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.