Results 1 to 4 of 4
  1. #1
    thomas6 is offline Member
    Join Date
    Mar 2010
    Posts
    3
    Rep Power
    0

    Default Input from Command Line rather than from a text file

    Hi. I have the following code which takes input (a username and password) from a text file, reads it in and prints it out in a Binary Search Tree. I have to modify it so it doesn't read from a file, instead the usernames and passwords are enterered at the command line. Nothing I do seems to work. Any help would be appreciated. Thanks.

    Java Code:
     import java.util.Scanner;
    import java.io.*;
    
    public class Run
     {
    
            public static void run()
            {
                    BSTree input = new BSTree();        
                    
                    int lineno = 0;
                    
                    try {
            FileReader fr = new FileReader("file.txt");
            BufferedReader reader = new BufferedReader(fr);
            String line = reader.readLine();
            Scanner scan = null;
            while (line != null) {
            scan = new Scanner(line);
            String u = "";
            String p = "";
            u = scan.next();
            p = scan.next();
            
            if (lineno == 0)
            {
                    input.username = u;
                    input.password = p;
                    lineno++;
            }
            
            else
            {
                    input = addtoBSTree(u, p, input);
            }
            
            line = reader.readLine();
            
            }
                    printBST(input);
                    
            reader.close();
            
            }
    
    
            catch (FileNotFoundException e) {
                    System.out.println("Could not find the file");
            }
            catch (IOException e) {
                    System.out.println("Had a problem reading from file");
            }
    
            }
                    
            
            
            public static void main(String args[])
        {
                    run();
    
        }
            
            public static BSTree addtoBSTree(String u, String p, BSTree t) {
    
            if (t == null) return (new BSTree(u, p));
            if (sort (u, t.username)) return (new BSTree(t.username, t.password,
                            addtoBSTree(u, p, t.left), t.right));
    
            else return (new BSTree(t.username, t.password, t.left, addtoBSTree(u, p, 
                            t.right)));
            }
    
            public static boolean sort (String s1, String s2){
            if (s1.length() == 0) return true;
            if (s2.length() == 0) return false;
            if (s1.charAt(0) < s2.charAt(0)) return true;
            if (s1.charAt(0) == s2.charAt(0)) return
            (sort(s1.substring(1),s2.substring(1)));
            return false;
                }
    
            public static void printBST(BSTree t){
                    if (t == null) return;
                    else {printBST(t.left);
                    System.out.println("Username: " + t.username);
                    System.out.println("Password: " + t.password);
                    System.out.println("");
                    printBST(t.right);
                            }
            }
    
    
    public class BSTree {
    
                          String username;
                    String password;
                    BSTree left;
                    BSTree right;
    
                   public BSTree (String u, String p, BSTree l, BSTree r) {
                            
                                   username = u;
                            password = p;
                            left = l; 
                            right = r;
    
                    }
    
                    public BSTree (String u, String p) {
    
                            username = u;
                            password = p; 
                            left = null; 
                            right = null;
    
                    }
                    
                    public BSTree () {
    
                        username = null;
                        password = null; 
                        left = null; 
                        right = null;
    
                }
                    
                    }

  2. #2
    ksemeks is offline Member
    Join Date
    Jan 2010
    Posts
    6
    Rep Power
    0

    Default

    You can use
    Java Code:
    System.out.print("Input username: "); 
    BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
    try
    {
            String user = userInput.readLine();
    }
    catch (IOException e)
    {
    ..
    }
    for the input from a console.
    You should however delete the part of code which asks for a file input.

  3. #3
    thomas6 is offline Member
    Join Date
    Mar 2010
    Posts
    3
    Rep Power
    0

    Default

    Thanks ksemeks, I've added that and it appears to take in the username but it doesn't seem to add it to the binary search tree. Also, how would I go about entering a password as well? Thanks

    EDIT, here is what I currently have;

    Java Code:
    import java.util.Scanner;
    import java.io.*;
    
    public class Run
     {
    
            public static void run()
            {
           
            	BSTree input = new BSTree();        
            	
            	System.out.print("Input username: "); 
            	BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
            	try
            	{
            	        String user = userInput.readLine();
            	        String u = "";
            	        String p = "";
            	        input.username = u;
            	        input.password = p;
                        input = addtoBSTree(u, p, input);
                        printBST(input);
    
    
            	}
            	catch (IOException e)
            	{
            		System.out.println("IO error trying to read your name!");
                    System.exit(1);
            	}
            	
            
            }
                    
            
            
            public static void main(String args[]) throws Exception 
        {
                    run();
    
        }
            
            public static BSTree addtoBSTree(String u, String p, BSTree t) {
    
            if (t == null) return (new BSTree(u, p));
            if (sort (u, t.username)) return (new BSTree(t.username, t.password,
                            addtoBSTree(u, p, t.left), t.right));
    
            else return (new BSTree(t.username, t.password, t.left, addtoBSTree(u, p, 
                            t.right)));
            }
    
            public static boolean sort (String s1, String s2){
            if (s1.length() == 0) return true;
            if (s2.length() == 0) return false;
            if (s1.charAt(0) < s2.charAt(0)) return true;
            if (s1.charAt(0) == s2.charAt(0)) return
            (sort(s1.substring(1),s2.substring(1)));
            return false;
                }
    
            public static void printBST(BSTree t){
                    if (t == null) return;
                    else {printBST(t.left);
                    System.out.println("Username: " + t.username);
                    System.out.println("Password: " + t.password);
                    System.out.println("");
                    printBST(t.right);
                            }
            }
     }
    Last edited by thomas6; 04-26-2010 at 09:59 PM.

  4. #4
    ksemeks is offline Member
    Join Date
    Jan 2010
    Posts
    6
    Rep Power
    0

    Default

    thomas6, well, at least learn some fundamental java programming.

    So, replace this lines
    Java Code:
            FileReader fr = new FileReader("file.txt");
            BufferedReader reader = new BufferedReader(fr);
            String line = reader.readLine();
            Scanner scan = null;
            while (line != null) {
            scan = new Scanner(line);
            String u = "";
            String p = "";
            u = scan.next();
            p = scan.next();
    with this
    Java Code:
            System.out.print("Input username: "); 
            BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
            	try
            	{
            		String u = userInput.readLine();
            	}
            	catch (IOException e)
            	{
            		e.printStackTrace();
            	}
            	
            System.out.print("Input password: "); 
            BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
            	try
            	{
            		String p = userInput.readLine();
            	}
            	catch (IOException e)
            	{
            		e.printStackTrace();
            	}
    And put away the reader and line objects, get rid of yours 2 exceptions(FileNotFound and IOException), and you can put away the java.util.Scanner since you won't be needing it.

Similar Threads

  1. count character in text file as input file
    By aNNuur in forum New To Java
    Replies: 7
    Last Post: 03-25-2010, 05:01 PM
  2. read a specific line in an input file
    By sara12345 in forum Advanced Java
    Replies: 7
    Last Post: 01-03-2010, 11:40 PM
  3. Saving To A New Line Using A Text File
    By jadaleus in forum Advanced Java
    Replies: 10
    Last Post: 10-24-2008, 08:21 PM
  4. Saving To A New Line Using A Text File
    By jadaleus in forum Advanced Java
    Replies: 1
    Last Post: 10-24-2008, 01:31 AM
  5. Unable to execute command line command in java
    By LordSM in forum New To Java
    Replies: 1
    Last Post: 08-08-2007, 01:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •