0

We have a group project to get data from a text file that will be input by the user. We have created a method to get the input and get the data saved to a output file but I'm stuck as to how we will get the file to be read for the methods we create that will then read through the file. Currently it just prints to the console.

public static void main(String[] args) throws FileNotFoundException {
    Scanner in = new Scanner(new File("dna.txt"));  //create scanner object for dna.txt
    processFile(in);//call the method using scanner as parameter
    File outputFile = new File("DataOutput.txt");
    PrintStream fileOutput = new PrintStream(outputFile);
    fileOutput.close();
    }                               

public static void processFile(Scanner in) throws FileNotFoundException{
    Scanner console = new Scanner(System.in);//open Scanner in the console         
    System.out.println("File to be read: ");//prompt user to enter the txt file
    String inputFile = console.next();//turns file into string and read in console
    while (in.hasNextLine()){//while stuff is in file, keep looping
        String s = in.nextLine();//contents in file = string s. we can now process s with arrays
        System.out.println(s);//print s
    }   


    }
}
Unheilig
  • 16,196
  • 193
  • 68
  • 98
  • this is confusing to me. it looks like you are ok through String inputFile, but the while loop is operating on the in parameter, which is not the inputFile variable I assume you think is the filename of the output file?. – hubson bropa Jun 01 '15 at 17:23
  • Ok, yeah, that's what I thought I was doing wrong but couldn't tell. I am just having a hard time because without the while loop it just prints my txt file to the console but I need it to be read by methods I will call later. – Chloe Wake Jun 02 '15 at 01:18
  • To read the file, http://stackoverflow.com/questions/2788080/reading-a-text-file-in-java – ggbranch Jun 04 '15 at 02:04
  • Thanks guys, we figured it all out! – Chloe Wake Jun 04 '15 at 06:54

0 Answers0