Im new to java and I need a little help.

I store the usernema, password and name in user.dat file. The user enters his usernema and password , if its a match it prints his name on the screen. If its not a match the user is asked to register-add username , password and name to the file.

File content is:

user1 pass1 Tom
user2 pass2 Bob
user3 pass3 Ben

I have a code that kind of works.I cant figure out the part when ther isnt a match. Im sure im doing the file scan the best way.

If I use else the addUser() will run 2 times if is there a match and 3 times if there isnt one.

Any help will be appreciated!

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


public class test {

    public static void main(String[] args) throws FileNotFoundException  {


        Scanner console = new Scanner(System.in);

        System.out.print("Enter username:");

        String uname = console.next();

        System.out.print("Enter password:");

        String pw = console.next();

          try {

               FileInputStream fstream = new FileInputStream("C:/user.dat");

               DataInputStream in = new DataInputStream(fstream);

               BufferedReader br = new BufferedReader(new InputStreamReader(in));

               String temp;


               while ((temp = br.readLine()) != null) {



                  String[] splitted = temp.split(" ");

            if (uname.equals(splitted[0])&&pw.equals(splitted[1])){


           System.out.println("Welcome "+splitted[2]+" !");

                 break;

             }


               }

               //no match run addUser()

               in.close();



             } catch (Exception e) {

               System.err.println(e);
             }



           }
 }

And for adding new users :

static void addUser(){

          String new_uname=null;
          String new_pw=null;
          String new_name=null;

        String choise=null;
        Scanner console = new Scanner(System.in);

        System.out.print("Do you want to register? y/n:");
        choise = console.next();    

    while (!(choise.equalsIgnoreCase("y")||choise.equalsIgnoreCase("n"))){

        System.out.print("Try again! y/n:");
         choise = console.next();    

    }

    if(choise.equals("y")){

        System.out.print("Enter username:");

         new_uname = console.next();

        System.out.print("Enter password:");

         new_pw = console.next();

        System.out.print("Enter name:");

         new_name = console.next();


         try {

         BufferedWriter out = new BufferedWriter(new FileWriter("C:/user.dat", true));


            out.write(new_uname+" "+new_pw+" "+new_name);
            System.out.print("Thanks for registering!");


           out.close();



             } catch (Exception e) {

               System.err.println(e);
             }


          }




    }

One way is to have a boolean matchFound = false When you find match in the file you set that to true. Then at line 49 you can test the boolean to see if you need to add the user.

Thanks it worked like charm :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.