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 4 of 4

Thread: Help reading a file to decode or encode

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Help reading a file to decode or encode

    Hello I've ran into hopefully a small problem. A few weeks ago we were given an assignment to decode or encode a phrase given by a user. This week we have to change our code so it users a file and not user input. This is the original working code. **this is not entire code just the part I need help with**

    public static void main(String[] args) {
    		alpha = new char[26];
    		loadData();
    		input = new Scanner(System.in);
    		while (true) {
    			displayMenu();
    			int choice = input.nextInt();
    			input.nextLine();
    			if (choice == 1) {
    				String phrase = getPhrase("Please enter a phrase to decode");
    				decodeMessage(phrase);
    			} else if (choice == 2) {
    				String phrase = getPhrase("Please enter a phrase to encode");
    				encodeMessage(phrase);
    			} else if (choice == 3) {
    				displayAlphabet();
    			} else if (choice == 4) {
    				System.out.println("Goodbye!");
    				break;
    			}
    		}
    	}
     
    private static String getPhrase(String message) {
    		System.out.println(message);
    		String phrase = input.nextLine();
    		return phrase;
    	}

    I have spent hours trying different senerios without success. I'm not sure where I am making my mystake. This is my latest version.


    private static String getPhrase() {
     
    		Scanner file = new Scanner(new File("C:\\Users\\me\\file.txt"));
    		String[] phrase=new String[5];
    		int i=0;
    		while (file.hasNext())
    		{
    		    phrase[i]=file.nextLine();
    		    i++;
    		  }
    		return phrase;
    	}
    public static void main(String[] args) {
    		alpha = new char[26];
    		loadData();
    		input = new Scanner(System.in);
    		while (true) {
    			displayMenu();
    			int choice = input.nextInt();
    			input.nextLine();
    			if (choice == 1) {
    				decodeMessage(phrase);
    			} else if (choice == 2) {
    				encodeMessage(phrase);
    			} else if (choice == 3) {
    				displayAlphabet();
    			} else if (choice == 4) {
    				System.out.println("Goodbye!");
    				break;
    			}
    		}
    	}
    My newest error is on my return phrase; in getPhrase. It says incompatible types- found java.lang.String[] but expected java.lang.String. I know this is because I'm mixing an array with a String... I just know how to fix it...


  2. #2
    Member clydefrog's Avatar
    Join Date
    Feb 2012
    Posts
    67
    Thanks
    15
    Thanked 2 Times in 2 Posts

    Default Re: Help reading a file to decode or encode

    Just as a thought, maybe you can invoke the Array toString() method?

    like:

    String str= Arrays.toString(phrase)

    and then instead of returning phrase, just return str

  3. The Following User Says Thank You to clydefrog For This Useful Post:

    mickey2012 (February 29th, 2012)

  4. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help reading a file to decode or encode

    That worked! thanks!

  5. #4
    Member clydefrog's Avatar
    Join Date
    Feb 2012
    Posts
    67
    Thanks
    15
    Thanked 2 Times in 2 Posts

    Default Re: Help reading a file to decode or encode

    no problem

Similar Threads

  1. encode a png image in ascii format..
    By entwicklerin in forum File I/O & Other I/O Streams
    Replies: 9
    Last Post: January 16th, 2012, 10:17 AM
  2. Replies: 3
    Last Post: December 2nd, 2011, 11:53 AM
  3. Encode and decode - Caesar cipher
    By siabanie in forum Java Theory & Questions
    Replies: 1
    Last Post: October 7th, 2011, 07:25 PM
  4. Replies: 0
    Last Post: June 19th, 2011, 03:16 AM
  5. Need help with reading from .dat file
    By cyoung in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: April 26th, 2011, 08:34 AM