• 0

Java Class String/Boolean Error


Question

Hey all, I've got an assignment in my compsci class to make a program where the user inputs values for a, b, and c, and the program identifies it as either a right triangle or a not a right triangle.

As part of the assignment, I have to incorporate some if/else statements (where I decided to inject a bit of fun), but that is where I begin seeing some errors... I think I may have failed to import a class, or perhaps I should be using "String" instead of "Boolean"? I've tried quite a few things, but my efforts thus far remain fruitless.

So, check out my code and see if you can find something that I haven't? TIA :D

import java.util.Scanner;
import java.lang.Math;
import java.lang.Boolean;

public class Triangle {
 public static void main (String [] args){
  Scanner input = new Scanner(System.in);
  double a; //first leg of triangle
  double b; //second leg
  double c; //hypotenuse
  Boolean x; //
  System.out.println("Measure the first leg of triangle!");
  a = input.nextDouble();
  System.out.println("Measure the second leg of triangle!");
  b = input.nextDouble();
  System.out.println("Measure the hypotenuse of triangle!");
  c = input.nextDouble();
  System.out.print("Do you wish to use a Pokeball? Y/N");
  x = input.nextString();
  if(x = "Y"){
    System.out.println("Trainer threw a Pokeball!");
    System.out.println("...");
    System.out.println("...");
    System.out.println("...");
      if(a*a+b*b == c*c){
    System.out.println("Congratulations, you have caught a Right Triangle!");
    }else{
      System.out.println("Oh! The Pokemon could not be caught!");
  }}if(x = "N"){
    System.out.println("Too bad! Trainer threw a Pokeball anyway!");
    System.out.println("...");
    System.out.println("...");
    System.out.println("...");
      if(a*a+b*b == c*c){
    System.out.println("Congratulations, you have caught a Right Triangle!");
    }else{
      System.out.println("Oh! The Pokemon could not be caught!");
    }}else{
      System.out.println("FATAL ERROR: INCORRECT INPUT. YOU JUST LOST THE GAME. RESET APPLICATION TO TRY AGAIN.");
      }

 }
}

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0
Exactly and you have to replace

  x = input.nextString();

1 error found:

File: E:\Triangle.java [line: 25]

Error: E:\Triangle.java:25: cannot find symbol

symbol : method nextString()

location: class java.util.Scanner

Link to comment
Share on other sites

  • 0

I fixed a few obvious errors up but when I try to run it the fatal error message appears at the same time the last question gets displayed.

not sure why yet. The code is pretty messy, what is it supposed to do?

edit: oh yea you should probably be taking everything in as strings and then parseint-ing them for real proggys.

edit again:

ok so I have a working version, if you're going to "throw" the ball anyways then there's no point in duplicating the work of calculating whether it is a right triangle. Just do

if throw: show message that it's thrown

else: show message that it's thrown anyways

and then do the calculation, don't put the calculation in the above if/else clauses

also there's no point doing an error like

if true: this

else if false: that

else: death

since it'll never be any other value.

if you want to do error checking then take all values as strings, then cast them into the appropriate type you want, and use try-catch to error-proof those.

Edited by primexx
Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.