简体   繁体   中英

How do I get my text file to be read in as a command line argument in Eclipse?

I have this code:

import java.util.*;
import java.io.*;

public class LetterPattern{

   // value for letter when there is no match
   public static final char UNRECOGNIZED_LETTER = ' ';

   // size of pattern array
   public static final int PATTERN_SIZE = 10;

   // size of grid array
   public static final int GRID_SIZE = PATTERN_SIZE + 2;

/*

    To hold the pattern, its features, and the letter
    it represents.  If the pattern does not match any
    of the letter specifications, then the letter char
    is a blank.

*/

   int[][] grid;
   int
      massbottom,
      corners,
      tees;
   char
      letter;


   // default constructor
   // grid will be created as a 12x12 array of 0's
   // the integer data members are initialized to 0
   // by default already; letter is set to ' '
   public LetterPattern()
   {
      grid = new int[GRID_SIZE][GRID_SIZE];
      letter = UNRECOGNIZED_LETTER;
   }

   /*
       precondition: assumes sc is not null and has a least one
          more line of text in it.

       postcondition: grid is loaded with 0's and 1's according to
          the discussion in the project description based on what
          remains in the input Scanner sc.

       YOU HAVE TO CODE THIS.
   */
   public void loadPattern(Scanner sc)
   {
       int r = 1; //row number

       while(sc.hasNextLine())
       {
           //read in line
           String gridFill = sc.nextLine();


           for(int i = 0; i < GRID_SIZE; i++)
           {
               if(gridFill.charAt(i) == '*')
               {
                   grid[r][i] = 1;
               }
               else if(gridFill.charAt(i) == '$')
               {
                   //ignore rest of the line, fill it with 0s
                   for(int k = i; k < GRID_SIZE; k++)
                   {
                       grid[r][k] = 0;
                   }
               }
               else
               {
                   grid[r][i] = 0;
               }
           }

           r++;
           if( r == 12)
           {
               break;
           }
       }

       sc.close();
   }

   /*
       precondition: assumes the grid array is not null and is a 12x12
          array with 0's and 1's.

       postcondition: the instance specific data members, massbottom,
          corners, and tees are calculated from the current contents of
          grid according to the project specification.

       YOU HAVE TO CODE THIS.
   */


   public void extractFeatures()
   {   
       //read in row by row, looking for corners and tees, if it is last row, then do massbottom.  
       for(int row = 1; row < PATTERN_SIZE + 1; row++)
       {
           for(int column = 1; column < PATTERN_SIZE +1; column++)
           {           
               int squareValue = grid[row][column];

               if(row == PATTERN_SIZE)
               {
                 //count 1s for the massbottom
                 if(squareValue == 1)
                 {
                     massbottom++;
                 }
               }

               if(squareValue == 1)
               {   //look for corners/tees
                   //check north of vertex
                   int northSquare = grid[row-1][column];

                   //check south of vertex
                   int southSquare = grid[row+1][column];

                   //check east  of vertex
                   int eastSquare = grid[row][column+1];

                   //check west  of vertex
                   int westSquare = grid[row][column-1];

                   int sumSquares = westSquare + eastSquare + southSquare + northSquare;

                   //Counts the squares around the vertex, if there are only 2 "1's", then it is a corner, if three then a tee.
                   //any more or less is invalid.  
                   if(sumSquares == 2)
                   {
                       //checks for valid corner
                       if(westSquare == 1 && northSquare == 1)
                       {
                           corners++;
                           break;
                       }
                       else if(westSquare == 1 && southSquare == 1)
                       {
                           corners++;
                           break;
                       }
                       else if(southSquare == 1 && eastSquare == 1)
                       {
                           corners++;
                           break;
                       }
                       else if(eastSquare ==1 && northSquare == 1)
                       {
                           corners++;
                           break;
                       }

                   }
                   else if(sumSquares == 3)
                   {   
                       //checks for valid tee 
                       if(westSquare == 1 && northSquare == 1 && southSquare == 1)
                       {
                           tees++;
                           break;
                       }
                       else if(westSquare == 1 && southSquare == 1 && eastSquare == 1)
                       {
                           tees++;
                           break;
                       }
                       else if(southSquare == 1 && eastSquare == 1 && northSquare == 1)
                       {
                           tees++;
                           break;
                       }
                       else if(eastSquare ==1 && northSquare == 1 && westSquare == 1)
                       {
                           tees++;
                           break;
                       }
                   }

                }
            }
        }

 }



   /*
       precondition: assumes the massbottom, corners, and tees 
          data members have been correctly calculated from the 
          current contents of the grid.

       postcondition: letter is assigned either the UNRECOGNIZED_LETTER
         value if the features do not match any of the letter features,
         or the letter whose features are matched.

       YOU HAVE TO CODE THIS.
   */
   public void classifyLetter()
   {
       //massbottom of 1 possible letters are "F', "I", "P", "T", and "Y". 
       if(massbottom == 1)
       {    //check for number of corners and tees to validate a letter

          if(corners == 1 && tees == 1)
          {
             letter = 'F';
          }
          else if(corners == 0 && tees == 0)
         {
             letter = 'I';
         }
          else if(corners == 3 && tees == 1)
          {
              letter = 'P';
          }
         else if(corners == 0 && tees == 1)
         {
             letter = 'T';
         }
         else if(corners == 2 && tees == 1)
         {
             letter = 'Y';
         }

       }
       //massbottom of 2 possible letters are "A", "H", and "M"
       else if(massbottom == 2)
       {  //check for number of corners and tees to validate a letter

           if(corners == 2 && tees == 2)
           {
               letter = 'A';
           }
           else if(corners == 0 && tees == 2)
           {
               letter = 'H';
           }
           else if(corners == 2 && tees == 1)
           {
               letter = 'M';
           }

       }
       //massbottom > 2 possible letters are "B", "C", "E", "G", "L", and "S".  
       else if(massbottom > 2)
       {   //check for number of corners and tees to validate a letter

           if(corners == 4 && tees == 2)
           {
               letter = 'B';
           }
           else if(corners == 2 && tees == 0)
           {
               letter = 'C';
           }
           else if(corners == 2 && tees == 1)
           {
               letter = 'E';
           }
           else if(corners == 3 && tees == 1)
           {
               letter = 'G';
           }
           else if(corners == 1 && tees == 0)
           {
               letter = 'L';
           }
           else if(corners == 4 && tees == 0)
           {
               letter = 'S';
           }
       }
       else
       {
           letter = UNRECOGNIZED_LETTER;
       }       
   }


   // getter functions for the massbottom, tees, corners, and
   // the matching letter
   public int getMassbottom(){ return massbottom;}
   public int getCorners(){ return corners;}
   public int getTees(){ return tees;}
   public char getLetter(){ return letter;}

   /*

       pre: grid is not null

       post: grid is not modified its full contents(all 12 rows of 12 columns)
          has been printed to the screen, line by line

       YOU MUST CODE THIS.

   */
   public void printPatternToScreen()
   {
      //print out the grid
       for (int i = 0; i < GRID_SIZE; i++) 
       {
           for (int j = 0; j < GRID_SIZE; j++) 
           {
               System.out.print(" " + grid[i][j]);
           }

           System.out.println("");
       }
   }

   /*

       pre: grid, massbottom, corners, tees, and letter are all 
          consistently loaded;
          patternNum indicates which number pattern this is from the
          input file

       post: the values of massbottom, corners, tees, and letter are
          reported to standard out labeled with patternNum

   */
   public void reportResultsToStdout(int patternNum){

      System.out.println("\nResults for pattern# " + patternNum + "\n");
      printPatternToScreen();
      System.out.println("\n   Massbottom = " + massbottom
      + "\n   Num of Corners = " + corners +
      "\n   Num of Tees = " + tees);

      System.out.print("\n   These feature values ");
      if (letter == LetterPattern.UNRECOGNIZED_LETTER)
         System.out.println("do not match any letter.");
      else
         System.out.println("match " + letter);
   }

   /*
      This main can be used to obtain your output.

      It sets up a Scanner instance from a command line argument,
      creates a LetterPattern instance, and repeatedly loads the 
      LetterPattern instance from the Scanner and
      analyzes it.  It displays the results to the standard out.

   */
   public static void main(String[] args){

      Scanner src;
      LetterPattern lp = new LetterPattern();
      int
         patternNumber = 1;

      if (args.length > 0){
         try{

            src = new Scanner(new File(args[0]));

            while (src.hasNextLine()){
               lp.loadPattern(src);
               lp.extractFeatures();
               lp.classifyLetter();
               lp.reportResultsToStdout(patternNumber);
               patternNumber++;
            }
         }
         catch(NullPointerException e){
            System.out.println("The file name may have been a null string.\nProgram Terminating." + e);
         }
         catch(FileNotFoundException e){
            System.out.println("No file with the name " + args[0]
            + "\nProgram terminating." + e);
         }
      }
      else // args is empty
         System.out.println("You must supply the input file name on"
         + " the command line.");
   }
}

I am trying to get my "letters.txt" file to be read in from the command line via eclipse. I've done some research and found out how to input the command line argument via Run configurations. Now when I go to run it, all I get is this message

Program terminating.java.io.FileNotFoundException: letters.txt (The system cannot find the file specified)

I have my file located in the same level folder where src and bin is located. ( Letter_Pattern > bin | src |classpath| letters)

Also, I did copy my java file over to my laptop and remade a project, and then just imported the java file in. Will that have affected something?

When running the program with a full path filename "D:\\path\\to\\file\\letters.txt" it's seems to work. Don't forget double quotes in case of space in one of your folder name.

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