user1857619
user1857619

Reputation: 31

input int at printin

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

Upvotes: 3

Views: 117

Answers (4)

Óscar López
Óscar López

Reputation: 236114

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.

Upvotes: 6

Curlystraw
Curlystraw

Reputation: 229

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+")");

Upvotes: 0

rgettman
rgettman

Reputation: 178303

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);

Upvotes: 0

FThompson
FThompson

Reputation: 28697

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 + " )");

Upvotes: 1

Related Questions