简体   繁体   中英

Reading numbers from a .txt/input file and printing a triangle in Java Eclipse

package classicTriangle;

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

    File file = new File("c:/eclipse/numbers");
    Scanner scan = null;
    String str[];
    int s1=0,s2=0,s3=0;
    try 
    { 
        Scanner file = new Scanner( new File("c:/eclipse/numbers"));
        String line;
        while((line=file.nextLine())!=null)
        {
            str = line.split(" ");

            if(str.length==3)
            {
                try
                {
                    s1 = Integer.parseInt(str[0]);
                    s2 = Integer.parseInt(str[1]);
                    s3 = Integer.parseInt(str[2]);

                    if(s1+s2>=s3 && s2+s3>=s1 && s1+s3>=s2)
                    {
                        if(s1==s2 && s2==s3) System.out.println("Equilateral");
                        else if(s1==s2 || s2==s3 || s3==s1) System.out.println("Isosceles");
                        else System.out.println("Scalene");
                    }
                    else
                    {
                        System.out.println("Not a Triangle");
                    }
                }
                catch(Exception e)
                {
                    System.out.println("Not a Triangle");
                }

            }
            else
            {
                System.out.println("Not a Triangle");
            }
        }
    }
    catch(Exception e)
    {

    }

    }

    }

I am getting a duplicate local variable at the line

" Scanner file = new Scanner( new File("c:/eclipse/numbers"));"

I am a noob to this, and honestly cannot figure it out. Any advice/input would be great.

You defined File file earlier, so that name is taken when you define Scanner file . Change one of them and everywhere you reference it.

Note that a try block doesn't have its own local variables; it shares those of the parent section.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM