1

I was trying to get a few separated lines of text within one System.out. There was supposed to be 4 separate lines and instead of that program only prints the last one.

I was thinking about replacing it with separate System.out for each of the lines but i'm not sure if it's the fastest solution and what is wrong with the current one.

Method that contains above mentioned code:

   public double getTotal(Addition addition1, Addition addition2, Addition addition3, Addition addition4){
        this.additionPrices = ("First addition - " + addition1.getName() + " costs " + addition1.getPrice() + "\r" +
                              "Second addition - " + addition2.getName() + " costs " + addition2.getPrice() + "\r" +
                              "Third addition - " + addition3.getName() + " costs " + addition3.getPrice() + "\r" +
                              "Fourth addition - " + addition4.getName() + " costs " + addition4.getPrice());
        this.total = this.price + addition1.getPrice() + addition2.getPrice() + addition3.getPrice() + addition4.getPrice();
        return (this.price + addition1.getPrice() + addition2.getPrice() + addition3.getPrice() + addition4.getPrice());
    }

Main class where it's used:


    public static void main(String[] args) {

        Meat meat = new Meat();
        Roll roll = new Roll();

        Addition tomato = new Tomatos(2);
        Addition lettuce = new Lettuce(1);
        Addition pickle = new Pickle(3);
        Addition onion = new Onion(1.5);

        Hamburger hamburger = new Hamburger(meat,roll,20);


        hamburger.getTotal(onion,onion, tomato, onion);
        hamburger.showPrice();


    }
}

Result:


Base price equals: 20.0
Total price equals: 26.5
Fourth addition - Onion costs 1.5

Process finished with exit code 0

Sorry if that's something obvious. It's my first question here and I couldnt find the answer in old questions.

CrazyCoder
  • 389,263
  • 172
  • 990
  • 904
CowOO
  • 31
  • 1
  • 6
  • 1
    Does this answer your question? [What is the difference between a "line feed" and a "carriage return"?](https://stackoverflow.com/questions/12747722/what-is-the-difference-between-a-line-feed-and-a-carriage-return). The `\r` is working exactly as intended and keeps overwriting the same line. If you want separate lines, you have to use `\n` – that other guy Jul 23 '20 at 20:53
  • It does, thanks. I was using wrong keywords when searching. – CowOO Jul 23 '20 at 21:14

2 Answers2

1

Use line feed \n instead of \r.

In terminals that support it, the carriage return character \r returns the cursor to the beginning of the line. It gives you a way to delete and overwrite the current line.

In comparison, the line feed character \n moves the cursor to the next line.

Joni
  • 108,737
  • 14
  • 143
  • 193
0

I was trying to get a few separated lines of text within one System.out.

Lines in unix are separated by the \n character instead of the \r character which resets the cursor to the front of the line. In your case, each of your lines was overwriting the previous one.

But you really should not have to do the \n at all. You should do something like:

System.out.println("First addition - " + addition1.getName() + " costs "
      + addition1.getPrice());
System.out.println("Second addition - " + addition2.getName() + " costs "
      + addition2.getPrice() + "\r" );

println(...) prints the message and then adds the "line-separator" character which is portable so it works under other operating systems which don't use \n for their end-of-line. This means that you don't have to use the \n character directly.

I was thinking about replacing it with separate System.out for each of the lines but i'm not sure if it's the fastest solution and what is wrong with the current one.

This is textbook premature optimization. Don't worry about the multiple System.out calls since they are doing IO anyway so the minuscule extra resources that may be used (you are talking about nanoseconds if any) aren't going to be noticed. That you had a bug in the optimized code is a perfect example of why we should work for maintainable and readable code and only optimize when necessary.

Gray
  • 115,027
  • 24
  • 293
  • 354