Austin Ray
Austin Ray

Reputation: 11

How do you put multiple inputs on one line?

This is probably a easy question for most of you but the answer has evaded me for the most part. I'm writing a program to sort three numbers from lowest to highest and in the command prompt the inputs must be all on one line. I have the program working but for whatever reason I cannot get the inputs to show on one line. Instead I get something like:

Please enter three numbers: 1

2

3

Sorted numbers are: 1, 2, 3

Where it should show

Please enter three numbers: 1 2 3

Sorted numbers are: 1, 2, 3

My code:

import java.util.Scanner;
public class Ch5PA1
{
public static void main(String[] args) {
// Declarations
Scanner input = new Scanner(System.in);

System.out.print("Enter three values: ");
double num1 = input.nextDouble();
double num2 = input.nextDouble();
double num3 = input.nextDouble();
displaySortedNumbers(num1, num2, num3);
}

/** Sort Numbers */
public static void displaySortedNumbers(double num1, double num2, double num3){
double highest = num1 > num2 && num1 > num3 ? num1 : num2 > num1 && num2 > num3 ? num2 : num3;
double lowest = num1 < num2 && num1 < num3 ? num1 : num2 < num1 && num2 < num3 ? num2 : num3;
double middle = num1 != highest && num1 != lowest ? num1 : num2 != highest && num2 != lowest ? num2 : num3;
System.out.println("The sorted numbers are " + lowest + " " + middle + " " + highest);
}
}

Upvotes: 1

Views: 935

Answers (2)

ave4224
ave4224

Reputation: 19

You don't need to change your code. Just separate your doubles with whitespace. (Spaces)

Upvotes: 0

Abdul Jabbar
Abdul Jabbar

Reputation: 5931

You can take input from user like provide 3 numbers in comma seperated or space seperated. And split the string into array.

Upvotes: 1

Related Questions