Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3798384
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:33:50+00:00 2026-05-19T13:33:50+00:00

I have a text file where i have names and passwords separated by :.

  • 0

I have a text file where i have names and passwords separated by :.

user1:pwd1
user2:pwd2

In the login page if the user gives the correct username and password it will lead you to the welcome page. But I am not getting this properly. The output which i get is

user1
pwd1
inside try
user1
pwd1
true
welcome user1
user2
pwd2
false
not equal

My code is below.

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.regex.*; 
import com.sun.org.apache.xalan.internal.xsltc.compiler.Pattern;


public class TextFile {

    /**
     * @param args
     */

    public void getNamePwd(String name, String pwd) {
        // TODO Auto-generated method stub
        System.out.println(name);
        System.out.println(pwd);
        String[] splitVals=null;
        try{
            System.out.println("inside try");
            String strLine;
            BufferedReader br = new BufferedReader(new FileReader("D:\\test\\text.txt"));
            while((strLine=br.readLine())!=null){
            splitVals=strLine.split(":");
            for(int i=0;i<splitVals.length;i=i+2){
            System.out.println(splitVals[i].toString());
            System.out.println(splitVals[i].toString());
            String nameUser=splitVals[i].toString();
            String passWord=splitVals[i+1].toString();
            System.out.println(name.equals(nameUser));
            if((name.equals(nameUser))&&(pwd.equals(passWord))){
                System.out.println("welcome"+name);
                }
            else{
                System.out.println("not equal");
            }
            }
            }
        }catch(Exception e){

        }
    }

}

please help me..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T13:33:50+00:00Added an answer on May 19, 2026 at 1:33 pm

    I suspect that you want to stop looking for username/password matches after you’ve found one… To do this you have to break the loop upon a match. To do this you do the following:

    readLoop:
    while((strLine=br.readLine())!=null){
    
        // ...
        String[] splitVals = strLine.split(":");
    
        if((name.equals(nameUser))&&(pwd.equals(passWord))){
            System.out.println("welcome"+name);
            break readLoop;
        }
    
        // ...
    }
    

    Besides, I don’t know why you need this loop:

    for(int i=0;i<splitVals.length;i=i+2)
    

    Recall that you read the file line by line. That is, the splitted array will contain the username and password of the current line.

    To print the username / password you could do something like this:

    System.out.printf("Username: %s, Password: %s%n", splitVals[0], splitVals[1]);
    

    I would probably solve it using a Scanner. Something like this:

    import java.io.*;
    import java.util.Scanner;
    
    
    public class TextFile {
    
        public static void main(String[] args) throws FileNotFoundException {
    
            if (userPassOk("hello", "world"))
                System.out.println("Welcome");
            else
                System.out.println("Get out!");
        }
    
        private static boolean userPassOk(String user, String pass)
                throws FileNotFoundException {
    
            Scanner s = new Scanner(new File("test.txt"));
            while (s.hasNextLine()) {
                String[] userPass = s.nextLine().split(":");
                if (userPass[0].equals(user) && userPass[1].equals(pass))
                    return true;
            }
            return false;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text file with a set of names formatted in the following
I have Text file that contains data separated with a comma , . How
I have jsp file: <%@ page language=java contentType=text/html; charset=UTF-8 pageEncoding=UTF-8%> <%@ taglib prefix=c uri=http://java.sun.com/jsp/jstl/core%>
I have a text file named num.txt who's only contents is the line 123
I have a text file which contains something like this: Hello, my name is
I have an .erb file where I'd like to return the following text: Name
I have a text file that contains a list of filenames, minus the extension,
I have text file with some stuff that i would like to put into
I have text file with something like first line line nr 2 line three
I have text file, like FILED AS OF DATE: 20090209 DATE AS OF CHANGE:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.