Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: something strange

  1. #1
    Member
    Join Date
    Jan 2011
    Posts
    88
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default something strange

    I cant seem to figure this out, its probably something really easy that I'm over looking but its annoying me now.

    When someone types in "commands" they have to push the enter key twice before it will out print what its supposed to.

    Heres a snippet of my code:

    if (cmd.equals("commands")) {
    			for (int i = 0; i < commands.length; i++)
    				System.out.println("Current commands: " + commands[0][i] + commands[1][i]);
    		}

    Heres the whole class:
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
     
     
    public class Main {
     
    	private BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
    	private String[][] commands = {{"newpassword", "listpasswords"}, 
    			{" - Saves the password you type", " - Shows a list of saved passwords."}};
     
    	public static void main(String[] args) throws IOException {
    		Main m = new Main();
    		while (true) {
    			System.out.println("Please type 'commands' to view a list of available commands, " +
    					"\nor just type in a command.");
    			m.getInput(m.input.readLine());
    		}
    	}
     
    	public void getInput(String cmd) throws IOException {
    		String password = input.readLine();
    		if (cmd.equals("commands")) {
    			for (int i = 0; i < commands.length; i++)
    				System.out.println("Current commands: " + commands[0][i] + commands[1][i]);
    		}
    		if (cmd.equals("newpassword")) {
    			writeToFile(password);
    			System.out.println("You typed" + password);
    		}
    	}
     
    	public void writeToFile(String toWrite) throws IOException {
    		FileWriter fstream = new FileWriter("password/file.txt");
    		BufferedWriter out = new BufferedWriter(fstream);
    		out.write(toWrite);
    		out.close();
    		System.out.println("Wrote " + toWrite +" to file.");
    	}
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: something strange

    before it will out print what its supposed to.
    What is it supposed to print?

  3. #3
    Member
    Join Date
    Jan 2011
    Posts
    88
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: something strange

    the outprint of

    System.out.println("Current commands: " + commands[0][i] + commands[1][i]);

    would be

    Current commands: newpassword - Saves the password you type
    Current commands: listpasswords - Shows a list of saved passwords.

    so basically its just out printing array 'commands'

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: something strange

    How many places in your code do you read input from the user? What happens to the input that is read?
    Add a println after EACH place you read something in from the user to print out what you read.
    Add some ID String to the output so you know what println printed it:
    println("ID1 input=" + <THEINPUTHERE> + "<");

  5. #5
    Member
    Join Date
    Jan 2011
    Posts
    88
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: something strange

    I fixed it thanks

Similar Threads

  1. Strange errors with java.nio.file
    By jnewb in forum What's Wrong With My Code?
    Replies: 9
    Last Post: January 4th, 2012, 02:37 PM
  2. Beginner programming- strange problem
    By jkkruse in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 23rd, 2011, 05:39 AM
  3. Strange error message
    By javapenguin in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 11th, 2011, 02:03 PM
  4. Strange problem with drawing string
    By Asido in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 26th, 2010, 03:38 PM
  5. Strange Compiling Error
    By crism85 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 13th, 2009, 12:59 AM