Results 1 to 4 of 4
- 04-26-2010, 06:24 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 3
- Rep Power
- 0
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; } }
- 04-26-2010, 06:31 PM #2
Member
- Join Date
- Jan 2010
- Posts
- 6
- Rep Power
- 0
You can use
for the input from a console.Java Code:System.out.print("Input username: "); BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in)); try { String user = userInput.readLine(); } catch (IOException e) { .. }
You should however delete the part of code which asks for a file input.
- 04-26-2010, 08:39 PM #3
Member
- Join Date
- Mar 2010
- Posts
- 3
- Rep Power
- 0
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 08:59 PM.
- 04-26-2010, 09:00 PM #4
Member
- Join Date
- Jan 2010
- Posts
- 6
- Rep Power
- 0
thomas6, well, at least learn some fundamental java programming.
So, replace this lines
with thisJava 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();
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.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(); }
Similar Threads
-
count character in text file as input file
By aNNuur in forum New To JavaReplies: 7Last Post: 03-25-2010, 04:01 PM -
read a specific line in an input file
By sara12345 in forum Advanced JavaReplies: 7Last Post: 01-03-2010, 10:40 PM -
Saving To A New Line Using A Text File
By jadaleus in forum Advanced JavaReplies: 10Last Post: 10-24-2008, 07:21 PM -
Saving To A New Line Using A Text File
By jadaleus in forum Advanced JavaReplies: 1Last Post: 10-24-2008, 12:31 AM -
Unable to execute command line command in java
By LordSM in forum New To JavaReplies: 1Last Post: 08-08-2007, 12:23 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks