Click here to Skip to main content
15,887,844 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a file called information.txt that stores made up people's name, age, address, phone number, username and password.

I am trying to find the username and password of each person from said file, and then display it.

This is what I have so far, and I am just getting the username output only. Any help, suggestions, or pointers would be appreciated!

What I have tried:

Java
public class FindUserPass {

    public static void main(String[] args) {
        Scanner file = null;
        String read;
        
        try
        {
            file = new Scanner(new FileInputStream("/Users/JessR/information.txt"));
            read = file.nextLine();
            
            for(int i = 0 ; i < read.length(); i++)
            {
                int a = read.indexOf("Username:");
                int b = read.lastIndexOf("Password:");

                System.out.println(read.substring(a,b));
            }
        }
        catch(FileNotFoundException e)
        {
            System.out.println(e);
        }
    }
    }
Posted
Updated 11-Dec-16 16:45pm
Comments
[no name] 11-Dec-16 16:04pm    
Well obviously you would need to substring the password out of the file too, which you aren't doing.

1 solution

Are you having problem finding the starting index of the actual password string? Use a combination of lastIndexOf and length, e.g.
String password = "Password:";
int theStartIndexOfPassword = read.lastIndexOf(password) + password.length;

Modify it to take into account any white space.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900