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: Password writing file

  1. #1
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Password writing file

    Hey,

    I am writing a program that asks you if you would like to save or load passwords to a file, then it runs the specific routine. Here is the code:

    import java.util.Scanner;
    import java.io.*;
     
    public class Passwords {
     
    	public static void main(String[] arg) throws IOException  {
     
    		String[] passwords = new String[10];
    		int place;
    		String pass;
    		String sl;  //the variable that figures out whether you're going to save or load passwords
     
    		Scanner scan = new Scanner(System.in);
     
    		System.out.print("Would you like to save or load passwords?");
    		sl = scan.nextLine();
     
    		switch (sl.toLowerCase())  {
     
    		case "save":
    			save(passwords);
    		}
    		case "load":
    			load(passwords);
     
     
    	}
    		static void save (String[] passwords) throws IOException  {
     
    		passwords = new String[10];
    		int place = 0;
    		String pass;
     
    		Scanner scan = new Scanner(System.in);
     
    		while (place<10)  {
    			System.out.print("?");
    			pass = scan.nextLine();
    			passwords[place] = pass;
    			place++;
    			if (pass == null)  {
    				break;
    			}
     
    		}
     
    		FileWriter saveFile = new FileWriter("passwords.txt");
     
    		place = 0;
     
    		while (place<10)  {
    			saveFile.write(passwords[place] + "\n");
    			place++;
    		}
     
    		saveFile.close();
     
    	}
     
    	static void load(String[] passwords) throws IOException  {
     
    		int place = 0;
     
    		BufferedReader saveFile = new BufferedReader(new FileReader("passwords.txt"));
     
    		System.out.print("Your passwords are: ");
     
    		while (place<10)  {
    			passwords[place] = saveFile.readLine();
    			System.out.print(passwords[place]);
    			place++;
     
    		}
     
    	}
    }

    My problem is that in the main() routine of the program, in the switch, at the load routine, it has both "load" and load(passwords) are underlined. It tells me that I cannot convert fron string to boolean, and that the expression must return a value. It is obvious that all of the routines are void, so I am not sure what to do.

    Help?

    -Silent


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Password writing file

    What version of java are you running? If < v 7, you cannot use Strings in switch statements.

  3. #3
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Password writing file

    No, I'm using 7.

  4. #4
    Member
    Join Date
    Feb 2012
    Location
    West USA
    Posts
    67
    My Mood
    Inspired
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Password writing file

    Can anyone else help?

  5. #5
    Junior Member Aju_21's Avatar
    Join Date
    Mar 2012
    Location
    Mumbai
    Posts
    4
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Password writing file

    It is because you have written the following case out of switch { }
    case "load":
    load(passwords);

    instead of ending switch i.e. } after save(passwords);
    end } after load(passwords);
    i guess that will solve the problem

  6. The Following User Says Thank You to Aju_21 For This Useful Post:

    SilentNite17 (March 5th, 2012)

Similar Threads

  1. new line in file writing
    By deependeroracle in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: January 30th, 2012, 10:06 AM
  2. file reading & writing
    By macko in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 12th, 2011, 08:54 AM
  3. [SOLVED] Writing " to a File
    By Sai in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 29th, 2010, 05:21 AM
  4. [SOLVED] Writing integers to a file
    By dubois.ford in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 15th, 2010, 06:18 PM
  5. Writing Output To New File
    By Scottj996 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: January 6th, 2010, 07:25 PM