1
import java.util.Scanner;
public class Tipping {

  public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    System.out.print("Please enter initial price followed by tip percentage: ");
    double Price = s.nextDouble(); double TipRate = s.nextDouble();
    double TipAmount;
    TipAmount = (Price*TipRate)/100;
    System.out.println("Tip amount: " + TipAmount);
    System.out.print("Total price: " + (TipAmount + Price) +".");
  }}

I always get the price on the first line and the tiprate on the other.

Zelldon
  • 5,396
  • 3
  • 34
  • 46
A. Freeman
  • 21
  • 9
  • 1
    If you want to print something without a line break, don't use println. – Raistlin Apr 18 '17 at 11:33
  • I am not using println for the price, look closely – A. Freeman Apr 18 '17 at 11:35
  • Side note: you want to read about java naming conventions. Variable names go camelCase; like tipAmount; not TipAmount! – GhostCat Apr 18 '17 at 11:56
  • Oh was that a bad thing ? Sorry but I am new to this, I just started today... – A. Freeman Apr 18 '17 at 12:10
  • Your decision is *perfectly* fine. You accept that answer that you found to be the most helpful. Of course I am angry with *myself* because I didn't think of putting a **space** into the input as delimiter. But none of that is your problem. – GhostCat Apr 18 '17 at 12:13
  • 1
    Oh don't be angry at yourself for something that you didn't do on purpose because that is bad for you. Mark that instead as a positive experience to keep you going forward --->. – A. Freeman Apr 18 '17 at 12:17

3 Answers3

1

Use instead of System.out.println (which stands for print line) with System.out.print. The System.out.println will print an line break after the given string on the output stream.

You could also combine the print's with a single print line.

For example you could use this:

System.out.println(String.format("Tip amount: %f Total Price: %f", TipAmount, (TipAmount + Price)));

Update

Simple solution for the scanner problem would be to input the numbers in one line separated with a whitespace.

Zelldon
  • 5,396
  • 3
  • 34
  • 46
1

Sure, avoid calling:

System.out.println()

As the name (somehow) indicates, that prints a whole "line"; ending with a line break.

So, either call System.out.print() multiple times; or better practice: call System.out.println() once, but give all the strings you wanted to be printed to that call.

And as I just saw your comment: you are calling println() - in the line before you call print()!

Given your comment: you have to understand how that scanner works. The scanner reads strings; and by default, line by line.

In other words: when you use nextDouble() then the scanner assumes that you will enter a string that represents a number - but that your input is "terminated" by the user pressing ENTER. In other words; those line breaks are inevitable!

So, simply deal with them like:

System.out.print("Please enter initial price: ");
double Price = s.nextDouble(); 
System.out.print("Please enter tip percentage: ");
double TipRate = s.nextDouble();

double TipAmount = (Price*TipRate)/100;
System.out.println("Tip amount: " + TipAmount + " results in Total price: " + (TipAmount + Price) +".");

Long story short: when using the console in/out like this; you can't prevent certain line breaks. Simply accept that, and don't get to much into perfectionism.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • I am talking about the line 4 where the user should enter the price followed by the tiprate, the tiprate always jumps to the next line – A. Freeman Apr 18 '17 at 11:47
  • I think that as a developer, I cannot just settle with things that I don't like, but seek how to change them. Not getting too much into perfectionism is unfortunately not an option that I consider. Thank you for your answer ;) – A. Freeman Apr 18 '17 at 12:03
  • Please read carefully: using the console directly, such things are hard. If your requirement is "perfect" user experience, you probably have to create a GUI or, maybe, use a library giving you [curses](http://stackoverflow.com/questions/439799/whats-a-good-java-curses-like-library-for-terminal-applications) capabilities. In other words: perfection is possible, but it doesn't come for free! – GhostCat Apr 18 '17 at 12:05
  • Yes exactly. You have been very helpful. A great many thanks to you. I will start looking into those curses you speak of. :) – A. Freeman Apr 18 '17 at 12:07
1

Judging by your wording, you don't like that when input is handled via the Scanner, pressing Enter on the first double value (The Price) it advances to the next line and waits for the second double value (The TipRate). First of all, the character "Enter" is essentially the system dependent new-line character, so the Scanner immediately splits the input when the enter key is pressed. Specifically, the Scanner uses the Character.isWhiteSpace(char ch); function to evaluate where to split the tokens.

It all depends on how you want your input handled. Sadly, without importing a Curses-esque library and using a custom command prompt, the "\n" character cannot be reversed. What you can do is write your input and separate it by a space, for example writing "23.4 12.3" will write the input in the same line and correctly break the line into the 2 tokens you want.

If it is purely for aesthetic reasons you could catch the token as a String, evaluate if the "\n" character exists within and ask the user to re-input the variables in a single line or, if the "\n" character doesn't exist, split the String on the " " character and then use the Double.valueOf(String str); function.

Beware not to use the (Scanner in).setDelimiter(String str) since that will avoid breaking the tokens on the "\n" character (If the str String is for example " ") and will only freeze your program until the user splits 2 inputs with the space character and then throw an InputMismatchException because 12.3\n12.3 32.3 would be "valid" input but the token "12.3\n12.3" can't be evaluated to a single Double variable.