-1

Below is the code I've written thus far. I'm struggling on the System.out.println where I'm attempting to output "The area of the triangle is (area)." I'm very new to Java coming from C++. I thought it best to post my work thus far here and see if anyone could offer a potential solution, as I'm so close but continue to receive the following error:

The method format(String, double) is undefined for the type AreaOfATriangle

import java.util.Scanner;

public class AreaOfATriangle {
   public static double getDistance(double x1, double y1, double x2, double y2) {
       return Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
   }
   public static void main(String[] args) {
       double x1, y1, x2, y2, x3, y3;
       Scanner sc = new Scanner(System.in);
       System.out.print("Enter three points for a triangle: ");
       x1 = sc.nextDouble();
       y1 = sc.nextDouble();
       x2 = sc.nextDouble();
       y2 = sc.nextDouble();
       x3 = sc.nextDouble();
       y3 = sc.nextDouble();
       double side1 = getDistance(x1, y1, x2, y2);
       double side2 = getDistance(x2, y2, x3, y3);
       double side3 = getDistance(x3, y3, x1, y1);
       double s = (side1 + side2 + side3) / 2.0;
       double area = Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
       System.out.println("The area of the triangle is " + format("%.2f",area) + ".");
   }
}

Any help would be greatly appreciated!

Ollivander
  • 11
  • 4
  • 1
    What is `format`? – Dave Newton Jun 04 '20 at 18:08
  • 1
    What do you think `+ format("%.2f",area) +` should do and why do you think so? – Savior Jun 04 '20 at 18:08
  • 1
    It's `String.format`, a static member of the `java.lang.String` class. – nanofarad Jun 04 '20 at 18:08
  • 2
    It would be if it was statically imported or qualified. – khelwood Jun 04 '20 at 18:09
  • @Savior I thought it would format the area as a floating point number up to two decimal places. – Ollivander Jun 04 '20 at 18:13
  • It looks like it's trying to find the `format` method in your current class and that method has not been defined. I'm guessing you want to use the `String.format` method so here's a [link to details how to use it](https://www.geeksforgeeks.org/java-string-format-examples/). – Tim Hunter Jun 04 '20 at 18:14
  • 1
    Identifiers in a Java program have to refer to something and the language has very few "implicit" identifiers (essentially only what's imported from `java.lang`). `format` is not one of those. – Savior Jun 04 '20 at 18:15
  • System.out is PrintStream, has a printf() that allows the exact same format. JAVA String has had the overloaded '+' operator much longer, so if you are not stifling precision or adding commas (dots), most use that. The '+' operator seems to know how to make a string of everything. If you are not printing any Strings, you can still say ' + "" ' at the right end to get use of '+' on all your non-Strings. I'd use either '+' or System.out.printf(format,...), and as, in this case, we are controlling precision, the latter. – David G. Pickett Jun 04 '20 at 18:35
  • @HarshalParekh I might be missing it, but I'm failing to see any comments asking for clarity in the title or a mcve (the OP literally has his entire code in the body, how is that not a mcve?). I DO see a lot of comments about his reasoning on the usage of the `format` method though. That seems like a pretty clear chain of evidence of what people's issue with the question is, but you're right... I'm willing to speculate otherwise if the evidence in this thread has other possible interpretations. – Tim Hunter Jun 04 '20 at 18:50
  • @TimHunter, `System.out.println("The area of the triangle is " + format("%.2f", 0.5) + ".");` - is what I would consider as a Minimal, Reproducible Example. – Harshal Parekh Jun 04 '20 at 18:53
  • @HarshalParekh Your edit in for further clarity made it understandable to me. I see your point, and I would agree if not for the follow up question asking about what `format` is supposed to be by Dave. I understand the appeal of reducing it by that amount, but personally I think that needs more flexibility when it comes to individuals who are extremely new to a language since they have fundamental misunderstanding about the usage of a language that a mcve doesn't accurately convey. I can understand if you don't agree though. – Tim Hunter Jun 04 '20 at 18:59
  • 1
    @TimHunter I do. I personally do not downvote anymore and leave comments for this very reason. But A lot of people confuse "I'm new to a technology" with "I don't know how to ask a good question" - from [here](https://meta.stackoverflow.com/a/262447/8430155). – Harshal Parekh Jun 04 '20 at 19:03
  • 1
    @TimHunter While I didn't downvote, if I had, the mitigating factor would have been that this is *trivially* searchable, especially for someone who already has experience, which it appears the OP has. (Yes, the title should be edited, but I doubt anyone down-voted for that.) Re: "pretentious" -- I didn't really see any here, although I'd agree things can appear that way even when it's not intended (of which I am guilty of fairly frequently). – Dave Newton Jun 04 '20 at 19:07
  • @DaveNewton Then in that case I would say a link to a Google search would be an appropriate answer if you feel the question lacked that step in the search for an answer so far, but that's considered improper answering etiquette here so... eh. As for considering attitudes pretentious, sorry but that's a purely subjective conclusion whether someone deserves the title so even if you as an insider consider it not so does not mitigate the evidence of others considering it otherwise as outsiders. – Tim Hunter Jun 04 '20 at 19:13
  • @TimHunter Links to google searches are explicitly disallowed. I'm not an "insider". *Obviously* it's subjective, and I provided my opinion, which I am allowed to do. This question is trivially searchable--that's a simple fact. That said: opinion or not, there was nothing in the comment thread that's even remotely pretentious--there are legitimate questions regarding the OP's expectations, which were answered, and an explanation given by you and others. That's all that happened here. – Dave Newton Jun 04 '20 at 19:31
  • @DaveNewton That's partly what I mean by "improper answering etiquette" isn't it? As for being an insider, since you're an individual who has spent copious amounts of time on this site getting familiar with the social expectations and interactions here as evident by your profile info, that makes a strong case of the label "insider" being appropriate for you. As for the last part, I didn't say this thread was an example of why people consider it pretentious (depending on people's reason for the down votes anyways, but not a mind reader so can't use that). – Tim Hunter Jun 04 '20 at 19:37
  • @TimHunter *shrug* Ok. – Dave Newton Jun 04 '20 at 20:31

1 Answers1

0

You have to add import static java.lang.String.format; at top.

Alternatively, you could use.

System.out.println(String.format("The area of the triangle is %.2f.",area));
Piyush Patel
  • 1,646
  • 1
  • 14
  • 26