3

i'm trying to fix the script i wrote:

import java.util.Scanner;
public class Line2
{
    public static void main (String [] args)

    {
        Scanner scan = new Scanner (System.in);
        System.out.println ("Please enter 4 integers");
        int x1 = scan.nextInt();
        int y1 = scan.nextInt();
        int x2 = scan.nextInt ();
        int y2 = scan.nextInt ();
        double distance;

        //Asking the user to insert coordinates of both points and setting double
        // on distance in order to properly calculate the square root

        distance = Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
        System.out.print( "the length of the line between the points" (int x1, int x2) "and" (int y1, int y2) "is" distance);

       //Telling the program to calculate the distance of two points given by user
    }//end of method main

}

I'm trying to make x1 x2 y1 and y2 appear inside, however it doesn't allow me - gives Y (sort of) expected... What can i do in order for it to appears no matter what the int is? (other than that the program runs pretty well, i think..) Thank you

Óscar López
  • 232,561
  • 37
  • 312
  • 386

4 Answers4

6

Try this:

System.out.printf(
    "The length of the line between the points (%d, %d) and (%d, %d) is %f%n",
    x1, x2, y1, y2, distance);

The simplest solution for these cases is to use a formatted string, as shown in the code above. See more details of the syntax of such as string in the documentation.

In the above snippet, each character after a % indicates that the corresponding parameter in that position (after the string, in left-to-right order) must be formatted accordingly. In particular:

  • %d : This is going to be an integer. The first four parameters are the ints x1, y1, x2, y2
  • %f : This is going to be a decimal number. The fifth parameter is the double called distance
  • %n : This is going to be a platform-specific new line character

The printf method takes care of replacing each format character with the corresponding parameter value, creating and printing a String as expected. This is much easier and less error-prone than concatenating the string parts with the + operator, interspersing the required variables.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • +1, This is the best answer. The only thing to note is that using `printf` isn't usually taught this early on in beginner computer science courses, so OP might encounter his/her professor asking about it. – FThompson Nov 27 '12 at 19:38
1

To print multiple values combined, you can use the addition operator to append the values' string representations together.

System.out.print("here is a point(" + x1 + ", " + x2 + " )");
FThompson
  • 28,352
  • 13
  • 60
  • 93
0

Use "+" to concatenate your Strings.

System.out.print("the length of the line between the points (int"
+ x1 + ", int " + x2 + ") and (int " + y1 + ", int " + y2 + ") is " + distance);
rgettman
  • 176,041
  • 30
  • 275
  • 357
0

To combine integers with strings, you can do this:

x = 5;
y = 6;
System.out.println("This script exists to print the point ("+x+","+y+")");
Curlystraw
  • 219
  • 3
  • 9