0

I am trying to get Java to identify triangles from input contained in a text file. I have the code for identifying triangles when a user inputs integers into the console but I want it to read the integers from a separate text file now. For some reason I am getting errors doing this like cannot read from the file. I added a file exception but all it did was println all three if statements. Any help would be appreciated. I just do not think the normal threads on opening a file have helped me.

import java.util.Scanner;

public class Triangle {
    public static void main(String[] args) {
        Scanner user = new Scanner(System.in);
        double a, b, c;

        System.out.println("Enter longest side: ");
        a = user.nextDouble();
        System.out.println("Enter second side: ");
        b = user.nextDouble();
        System.out.println("Enter third side: ");
        c = user.nextDouble();

        if (a == b && b == c) {
            System.out.println("Equilateral Triangle");
        } else {
            if ((a == b && b !=c) || (a == c && a != b)) {
                System.out.println("Isoceles Triangle");
            }
            if (a != b && b != c && a != c) { 
                System.out.println("Scalene Triangle");
            }
            if (((a + b) > (c)) && ((a + c) > b) && ((b + c) > a)) {
                System.out.println("Not a Triangle");
            }
        }        
    }
}
Ray2814
  • 1
  • 2
  • You should look at all the good examples at the linked duplicate bug and then apply it to your code. You'll learn more that way. – clearlight Feb 05 '17 at 04:56
  • Keeping your code neat/conforming to a respected code styling guide helps w/debugging and makes it readable to others. Consistency of styling and methodical approach helps immensely in debugging & increasing maintainability and supporting peer review. That includes putting a space around operators. Cramming everything together looks really tacky the longer you code and spend time looking at pro code and non-pro code. Also, that clever trick of sometimes using braces and sometimes not with if/else isn't always a benefit. Sometimes it's just better to go with all braces in a complex if stmt – clearlight Feb 05 '17 at 04:59

1 Answers1

0

Pass a text file to the scanner object. Add this to your code

File input = new File("Whatever.txt");
Scanner scan = new Scanner(input);

Make sure to have the file in the same path as the project directory, otherwise you have to specify the path manually.

MacStation
  • 411
  • 4
  • 20
  • Thanks for trying to help. I added what you said and what Eclipse said and it does not work for me. public class Triangle { public static void main(String[] args){ File input = new File("input.txt"); Scanner scan = new Scanner(input); //Variables double a, b, c; System.out.println("Enter longest side: "); a = ((Scanner)input).nextDouble(); System.out.println("Enter second side: "); b = ((Scanner)input).nextDouble(); System.out.println("Enter third side: "); c = ((Scanner)input).nextDouble(); – Ray2814 Feb 05 '17 at 05:36
  • This is what I am currently doing and getting [link]https://postimg.org/image/tuabqun93/ – Ray2814 Feb 05 '17 at 05:47
  • It's complaining about casting because that's what you are doing. (Type)Object will try to cost Object to Type. To refer to scanner how I think it want to is in.nextDouble() – MacStation Feb 06 '17 at 13:17