0

Well, I might have flooded my title a bit but I can't get my java program to find input.txt so I can actually do something with it

This is for homework, I've been trying for the past few hours to get the code to recognize input.txt. I've put "/input.txt" and stuff before just in case, it still didn't work.

0

Output

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

public class DataProcessor {
   public static void main(String args[]) throws FileNotFoundException {
      int[] answers = {0,0,0,0};

         File file=new File("input.txt");
         Scanner scan=new Scanner(file);
         while (scan.hasNextLine()) {
            String letter = scan.nextLine();
            if (letter.equals("a")) {
               answers[0] = answers[0]++;
            } else {
               if (letter.equals("b")) {
                  answers[1] = answers[1]++;
               } else {
                  if (letter.equals("c")) {
                     answers[2] = answers[2]++;
                  } else {
                     if (letter.equals("d")) {
                        answers[3] = answers[3]++;
                     }
                  }
               }
            }      
      }

      System.out.println(answers[0]);
   }
}

I expect it to be able to read the file so that I can run the rest of the code, but none of it's working. What am I doing wrong here? It's just been so frustrating. What's supposed to happen is I make an output.txt file but I can't get to that part until I stop it from throwing errors and not reading the input

No longer throwing errors, but is still not reading the file, or something?

JH159753
  • 3
  • 4

1 Answers1

0

File objects do not have a hasNextLine or nextLine method.

Also, you might want to learn about else if () statements and that == should never be used to compare strings.

 Scanner scan=new Scanner(file);

 while (scan.hasNextLine()) {
     String line = scan.nextLine();

     if (line.equals("a")) {
        answers[0] = answers[0]++;
     } else if (line.equals("b")) {
        answers[1] = answers[1]++;
     } else if (...) {
        // ...
     }      
  }

Suggestion: Using in IDE like Eclipse or IntelliJ will highlight your errors before you try to compile.

And Scanner objects generally aren't preferred over BufferedReader - https://stackoverflow.com/a/5868528/2308683

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Right that was a bug, I meant to put scan.nextLine(); and now it does compile, but I don't think it's reading the correct file – JH159753 Apr 28 '19 at 01:44
  • It should read files that are directly next to any generated `.class` files. – OneCricketeer Apr 28 '19 at 01:45
  • What do you mean directly next to? – JH159753 Apr 28 '19 at 01:47
  • Also, this IDE (jGRASP) was provided by my professor, and I have edited code to use .equals – JH159753 Apr 28 '19 at 01:51
  • When you compile a `.java` file, it creates `.class` files. I don't know where jGrasp would put those files, but most IDEs do not create those files in the same folder as other files (such as a text file) – OneCricketeer Apr 28 '19 at 01:55
  • well my IDE did put the .class file in that same folder. What do I do about that? – JH159753 Apr 28 '19 at 01:59
  • Here you can see how to debug what path java is reading... From there, you can debug the relative path. https://stackoverflow.com/a/54820721/2308683 Or you can just give the absolute path like `new File("C:\\Users\\name\\Desktop\\file.txt")` – OneCricketeer Apr 28 '19 at 02:03
  • I went with the Absolute path, but I'm still getting output 0. – JH159753 Apr 28 '19 at 02:09
  • Then I suggest printing the lines as your scanner is reading them or see how to use the debugger https://www.jgrasp.org/tutorials187/06_Debugger.pdf – OneCricketeer Apr 28 '19 at 02:11
  • The problem is very likely that you need to use `String letter = scan.nextLine().trim()` to remove the trailing newline – OneCricketeer Apr 28 '19 at 02:12
  • Uhh, I think there's an issue with my arrays now. I just put System.out.println(answers[0]); with answers[0] = answers[0]++, and it's repeatedly printing 0 but not incrementing. – JH159753 Apr 28 '19 at 02:13
  • Fixed it, just needed answers[0]++; – JH159753 Apr 28 '19 at 02:24