0

I'm working on a project where 3 text files stored into a folder. I'm trying to avoid hardcoding the 3 paths into the source file.

That being said, I would like to run the program by typing at the prompt:java sourceCode folderName.

Please see below what I've achieved so far

:

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

    public class Indexes {

        //main method
        public static void main (String[] args)throws IOException {

            for (String MyFolder: args) {
                Scanner textFile =new Scanner(inputFile);

                //read the inputFile
                inputFile=textFile.nextLine();

                //two other objects
                File input =new File(inputFile);
                Scanner scan=new Scanner(input);

                int thesize=0;  
                int page=0;

                while (scan.hasNext()){

                    String readprt=scan.next();
                    //char[] c = s.toCharArray(); //conversion into character

                    char [] charArray=readprt.toCharArray();  //conversion of word of type string into type character
                    int size=charArray.length ; //length of word

                    thesize +=size;

                    if (thesize <=100){

                    }
                }
            }
        }
    }





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


public class Indexes {


    /*

    //creation of constructor
    public ReadFile (String inf){


    }

    public WriteFile (String outf){


    } */


    //main method
    public static void main (String[] args)throws IOException {

        for (String MyFolder: args) {

            MyFolder="C:/Users/youpi/Desktop/DataScience/DS730/Lesson2/MyFolder/huckfinn.txt";
            MyFolder="C:/Users/youpi/Desktop/DataScience/DS730/Lesson2/MyFolder/wap.txt";
            MyFolder="C:/Users/youpi/Desktop/DataScience/DS730/Lesson2/MyFolder/moby.txt";
            //System.out.println(MyFolder);



        //create objects for ("huckfinn.txt")

        //String inputFile="huckfinn.txt" ; //, outputFile="huckfinn_output.txt" ;
        String inputFile=MyFolder ;
        Scanner textFile =new Scanner(inputFile);

        //read the inputFile
        inputFile=textFile.nextLine();

        //write into  the outputFiles by defining an object for each of the files

        PrintWriter printer1 =new PrintWriter("C:/Users/youpi/Desktop/DataScience/DS730/Lesson2/MyFolder/huckfinn_output.txt");
        PrintWriter printer2 =new PrintWriter("C:/Users/youpi/Desktop/DataScience/DS730/Lesson2/MyFolder/wap_output.txt");
        PrintWriter printer3 =new PrintWriter("C:/Users/youpi/Desktop/DataScience/DS730/Lesson2/MyFolder/moby_output.txt"); 

        //two other objects
        File input =new File(inputFile);
        Scanner scan=new Scanner(input);

        int thesize=0;  
        int page=0;



        while (scan.hasNext()){

            String readprt=scan.next();

            //char[] c = s.toCharArray(); //conversion into character


            char [] charArray=readprt.toCharArray();  //conversion of word of type string into type character
            int size=charArray.length ; //length of word




                        /*printer1.println(readprt+":"+" "+size); */
                        //printer1.println("Size of"+" "+readprt+":"+ " "+counter);


                        //printer2.println(readprt+":"+" "+size);
                        //printer2.println("Size of"+" "+readprt+":"+ " "+counter);


                        //printer3.println(readprt+":"+" "+size);
                        //printer3.println("Size of"+" "+readprt+":"+ " "+counter);


                                                 //read next word on the next line 
                    /* }
                }
            }  */

            //printer1.println(readprt);
            //printer2.println(readprt);
            //printer3.println(readprt); 


                                    // the government of the people by the people

                                    thesize +=size;

                                    if (thesize <=100){

                                        printer1.println(readprt+":"+" "+size);
                                        printer2.println(readprt+":"+" "+size);
                                        printer3.println(readprt+":"+" "+size);
                                        continue ;
                                    } else {

                                        page ++;

                                        printer1.println("character count <=100:"+ " "+(thesize-size));
                                        printer1.println("                              Page"+" "+"-"+page+"-"+"                              ");

                                        printer2.println("character count <=100:"+ " "+(thesize-size));
                                        printer2.println("                              Page"+" "+"-"+page+"-"+"                              ");

                                        printer3.println("character count <=100:"+ " "+(thesize-size));
                                        printer3.println("                              Page"+" "+"-"+page+"-"+"                              ");

                                        thesize=0;

                                    }



            }
        //close objects 
        printer1.close();
        printer2.close();
        printer3.close(); 




        //object to read in methods
        //Index ReadObj = new Index();



        }
    }
}
setod003
  • 1
  • 2
  • 1
    Your question is unclear. Do you want to input the file names only through command line arguments or do you want to prompt the user after your program launches? And how many pathnames should your command line argument accept? – user3437460 Feb 02 '17 at 18:06
  • sounds like you are going to pass a folder location and read all the files located in the given folder/directory? – RAZ_Muh_Taz Feb 02 '17 at 18:09
  • Yes I want to pass a folder name and read all the files located in the given folder. Now I don't want to insert the path of the folder into the code. This to allow anybody who will have the code read files within the given folder name(let's say folderName). – setod003 Feb 02 '17 at 18:32
  • Look at the answer for http://stackoverflow.com/questions/5694385/getting-the-filenames-of-all-files-in-a-folder – Dakoda Feb 02 '17 at 18:38
  • Actually I'm not looking to embed in the source code the path of the folder that contains all the files to be read. Let's say if I give you the code I'm looking to build and you create on your computer a folder named folderName containing let's say 3 files or more or less, you should be able to read in all the files and create an output for each of files. – setod003 Feb 02 '17 at 18:44
  • Pass in the value via command line. http://stackoverflow.com/questions/890966/what-is-string-args-parameter-in-main-method-java – Christopher Schneider Feb 02 '17 at 18:56
  • Possible duplicate of [What is "String args\[\]"? parameter in main method Java](http://stackoverflow.com/questions/890966/what-is-string-args-parameter-in-main-method-java) – Christopher Schneider Feb 02 '17 at 18:56
  • I don't wanna prompt the use to pass in value at the prompt. Once the user compiles and run the program, the only value he or she will enter at the prompt will be java sourceCodeName folderName. – setod003 Feb 02 '17 at 19:04
  • I have no idea what you're asking. This: `java sourceCode folderName` indicates you want to pass in args via command line. Your comments and question say completely different things. Revise your question with exactly the behavior you expect, step-by-step, and what these commands are. And I'm unclear why you say the 'Once the user compiles'. That's a given, unless that's somehow relevant to your question. – Christopher Schneider Feb 03 '17 at 14:47
  • Christopher, I agree that the argument folderName will be passed at the command line, but the source code itself must not have any path. Correct me if I'm wrong because this is only my second week of learning java. Is it not possible to avoid hardcoding in the source code? Please see in the code field what I've actually written (a new update), but my the instructor said hardcoding is not acceptable. – setod003 Feb 03 '17 at 17:31

1 Answers1

0

I am also new to JAVA, but I guess the question is very basic.

If you already know what is the path then you should create a .properties file for your project and read the path from this configuration file.

If you can know the path only on run time, the you should read it from args inserted in command line (or other input resource available in run time).

if the the prefix of the path is known but the suffix isn't then you should use the combination of the two approaches above and do the following:

String fullPath = prefix + suffix;
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
elirandav
  • 1,913
  • 19
  • 27