What's new

Java P help po. ayaw mag tuloy tuloy ng code

Oden Sama

Forum Veteran
Established
Joined
May 30, 2016
Posts
932
Reaction
92
Points
987
import java.util.*;
import java.nio.file.*;
import java.io.*;
import static java.nio.file.StandardOpenOption.*;

public class TaskPerformance {
Scanner sc = new Scanner(System.in);
String filePath = "C:\\STINOVA\\Desktop\\records.txt";
public TaskPerformance(){
try {
System.out.println( "R - Register account ");
System.out.println( "L - Log in account " );
System.out.print(" \nEnter a Letter: ");
String UserInput = sc.nextLine();

if (UserInput.equals("R")){
registerAccount();
}else if(UserInput.equals("L")){
accountLogin();
}
}catch(Exception e) {

}
}
void accountLogin(){
try{
Path p1 = Paths.get(filePath.toString());

InputStream IS1 = Files.newInputStream(p1);

BufferedReader readeR = new BufferedReader(new InputStreamReader(IS1));
System.out.println("\nLog in to your account\n");
System.out.print ("Enter your Username: ");
String username = sc.nextLine();
System.out.print("Enter your Password: ");
String password = sc.nextLine();
String n1 = "";
String accnt;
String pass;

boolean isAlphanumeric = false;

while((n1 = readeR.readLine()) != null){
String[] records = n1.split (",");
accnt = records[0];
pass = records[1];
if(accnt.equals(username) && pass.equals(password)){

isAlphanumeric = true;
}
}

if(isAlphanumeric == true){
System.out.println("Successfully logged in!");
}else{
System.out.println("Incorrect username or passord, please try again. ");
}

}catch(Exception e){
System.out.print(e.getMessage());
}
}

void registerAccount() {
try{
Path npath = Paths.get (filePath.toString());
OutputStream output = new BufferedOutputStream(Files.newOutputStream(npath, CREATE));
BufferedWriter wrtr = new BufferedWriter (new OutputStreamWriter(output));
System.out.println("\nReg1ster an account\n");
System.out.print("Enter Username: ");
String userName = sc.nextLine();
System.out.print ("Enter Password: ");
String passWord =sc.nextLine();

wrtr.write(userName + "," + passWord);
wrtr.newLine();
System.out.println("\nYour account has been Registered!\n");
wrtr.close();
output.close();

new TaskPerformance();
}catch (Exception e){
System.out.print (e.getMessage( ));
}
}

public static void main(String args[]) {
new TaskPerformance();

}
}
1651375135647.png

1651375135647.png
 

Attachments

I think the problem is you are using an online compiler that didn't have enough privilege to create a file for you. I suggest you use an IDE on your local pc. I also suggest that your code must use CREATE and APPEND during registration so that the existing content shall remain when registering a new account into the file.
 

Similar threads

Back
Top