0

I've made a program a checks whether the username and & password are correct or not, but I want to open a file with a defined path if they are correct. How do I do that? This is what I've tried:

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

public class Login {

    public static void main(String[] args) {
        int pass;
        String un;
        String file_name = "C:/Users/Username/Desktop";

        System.out.println("Welcome, please enter the username and password");
        Scanner scan = new Scanner(System.in);
        System.out.println("Username?:");
        un = scan.nextLine();
        System.out.println("Password?:");
        pass = scan.nextInt();
        if (un.equals("Admin") && pass == 123) {
            System.out.println("That's correct");
            Readfile file = new Readfile(file_name);
            String[] aryLines = file.OpenFile();

        } else {
            System.out.println("You ain't the Admin");
        }
    }
}

The login checker works, but I can't open the file. It says among other that Readfile cannot be resolved to a type

almightyGOSU
  • 3,698
  • 6
  • 27
  • 39
  • 1
    First hit on google when searching for "java read text file": an [so article](http://stackoverflow.com/questions/4716503/best-way-to-read-a-text-file). – Patrik Jul 06 '15 at 12:16
  • But that's without the login – user4476151 Jul 06 '15 at 12:19
  • If you want me to do your (home-)work, you need to pay me. A bit of your own programming-effort is required. – Patrik Jul 06 '15 at 12:20
  • It's not my homework.... I'm just interested in making such a program. – user4476151 Jul 06 '15 at 12:23
  • Is Readfile a class you wrote? Is it in the same package as your Login class? Do you have a JAR file or just class files? Do you get the error at compile time or at runtime? Please provide the commands you use to compile and run your program. – Puce Jul 06 '15 at 12:35
  • Have solved the problem – user4476151 Jul 06 '15 at 13:25

2 Answers2

0

This is because ReadFile is not resolved to type as there is no such a class.

You can use BufferedReader along with FileReader to read text file. as below:

    BufferedReader br = null;

    try {
        String line; 
        br = new BufferedReader(new FileReader("C:\\testing.txt"));
        while ((line= br.readLine()) != null) {
            System.out.println(line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } 
Abhijeet Dhumal
  • 1,704
  • 11
  • 23
  • I guess Readfile is a custom class and it's a classpath issue. But we need more information to be sure. – Puce Jul 06 '15 at 12:35
-1
String file_name = "C:/Users/Username/Desktop";

is not a file name, it is a folder name. You need to add something like that:

String file_name = "C:/Users/Username/Desktop/filename.txt";

Also, after you do that, you should check your code, benefiting from examples on internet. I didn't try but it looks like there are going to be some errors; the last one is my advice to you.

stuck
  • 424
  • 1
  • 4
  • 15
  • This might be true but does not answer the OPs question (Readfile cannot be resolved to a type). – Puce Jul 06 '15 at 13:14
  • I think it's because of classpath, and this is only a suggestion. If try to do that it maybe help. – stuck Jul 06 '15 at 13:18