🔒 Closed (JAVA) PATULONG PANO MAG MATCH NG TXT FILE!!!

Status
Not open for further replies.

Nero Swallowtail

Honorary Poster
so ganto kase dapat, ata, ewan ko kung possible to sa java, gumagawa po kase kami ng log in at sign up interface, bale pag nag sign up ka, ma i store ung i iinput mo sa isang file using IO Class, ang problema ko is kapag dalawang accounnt yung ginawa di tapos tinype ko ung naunang account na naka store sa txtfile di nababasa ni java, ung latest account lang ang na sa save, di ko kase kabisado tong IOClasses so di ko alam ibabato kong condition para mabasa nya ung file, pa help po please, kanina pa po kase deadline nakiusap nalang ako sa sir namin hehe
eto po ung code:
Java:
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.text.DecimalFormat;

import static java.nio.file.StandardOpenOption.*;

public class TaskPerf6 {
    
    
    
    

    public static void main(String[] args) {
        
        Scanner scan = new Scanner(System.in);
        
        Path filePath = Paths.get("C:\\Users\\Asta\\Desktop\\Record.txt");
        File newFile = new File("C:\\Users\\Asta\\Desktop\\Record.txt");
        int n = 0;
        
        String Info, RegUserName = null, RegPassWord = null, LogUserName, LogPassWord, Select, Option; 
        
        do{
        System.out.print("Do you want to Log in or Sign up? \nEnter \"L\" to Login in, Enter \"S\"  to Sign up \nEnter your Response here: " );
        Select = scan.nextLine();
        
        
        try {
        OutputStream output = new BufferedOutputStream(Files.newOutputStream(filePath, CREATE));
        BufferedWriter writer = new BufferedWriter (new OutputStreamWriter(output));
        FileReader read = new FileReader(newFile);
        BufferedReader BR = new BufferedReader(read);
        
        
        
        if(Select.equalsIgnoreCase("S")) {
            System.out.print("Enter your desired Username: ");
            RegUserName = scan.nextLine();
            
            System.out.print("Enter your password: ");
            
            RegPassWord = scan.nextLine();
            Info = RegUserName +"\n" + RegPassWord;
            writer.write(Info);
            writer.flush();
            writer.close();
            
            System.out.println("Succesfully Signed up");
            
         }
        else if(Select.equalsIgnoreCase("L")) {
            
            System.out.print("Enter your Username: ");
            LogUserName = scan.nextLine();
            System.out.print("Enter your Password: ");
            LogPassWord = scan.nextLine();
            if(LogUserName.equals(RegUserName) && LogPassWord.equals(RegPassWord)) {
                System.out.println("Succesfully Logged in\n");
            }
            else {
                System.out.println("Incorrect Username or Password");
            }
            System.out.print("Enter \"X\" to Exit or \"S\" to Sign up for another account:");
            Option = scan.nextLine();
            if(Option.equalsIgnoreCase("X")){
                n++;
            }
            
            else if(!Option.equalsIgnoreCase("S") || !Option.equalsIgnoreCase("X")) {
                throw new InvalidLetterException();
            }
            else {
                continue;
            }
            
        }
        else {
            if(!Select.equalsIgnoreCase("S") || !Select.equalsIgnoreCase("L")) {
                throw new InvalidLetterException();
            }
            
        }

        }
        
        catch(IOException e) {
            System.out.println("Warning: " + e.getMessage());
        }        
    
        catch(InvalidLetterException e) {
            System.out.println("\nWarning: " + e.getMessage());
                
        }
        
    }while(n == 0);
        
   }
    
}
 
ay mag e error pala yan sainyo, eto ung code para dun sa InvalidLetterException

Java:
public class InvalidLetterException extends Exception{
    public InvalidLetterException() {
          super("Invalid  Letter Detected");
    }
}
 
Check nyo po subukan nyo print yung get result ng login nyo bka di lng na detect yung sunod na strings.

First to do.
Get the strings from txt file.

Try to print values to console to check if line is detected.

create a String model to store arrays of strings.
match the stored string arrays using for loop.
 
File file = new File("abc.txt");
FileInputStream fin = new FileInputStream(file);
BufferedReader reader = new BufferedReader(fin);

List<String> list = new ArrayList<String>();
while((String str = reader.readLine())!=null){
list.add(str);
}

//convert the list to String array
String[] strArr = Arrays.toArray(list);


(Match Everything from created arrays)
private String[] toppings = {"Cheese", "Pepperoni", "Black Olives"};

// our constructor; print out the String array here
public JavaStringArrayTests1()
{
// old for loop
int size = toppings.length;
for (int i=0; i<size; i++)
{
System.out.println(toppings);
}
}
 
Status
Not open for further replies.

About this Thread

  • 8
    Replies
  • 486
    Views
  • 2
    Participants
Last reply from:
Arjienx

Trending Topics

Online now

Members online
597
Guests online
2,285
Total visitors
2,882

Forum statistics

Threads
2,332,323
Posts
29,264,059
Members
1,146,210
Latest member
MakeitHappen6152
Back
Top