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: Passing Strings in methods

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Passing Strings in methods

    I am trying to call stringToFile in the main method but it throws error saying "incompatible types scanner cannot be converted to string."

    Thanks in advance!

     
    public static void main(String[] args) {
        Scanner keyboard = new Scanner (System.in);
     
                    mask(keyboard);
     
                    printingString();
     
                    fileName(keyboard);
     
       String  completeFileName = stringToFile(keyboard);
      }
     
     
    public static String stringToFile(String completeFileName)throws FileNotFoundException {
     
            File myFile = new File (completeFileName);
            Scanner fileReader = new Scanner (myFile);
     
            PrintWriter fileWriter = new PrintWriter(myFile);
     
            System.out.println("Saving Original random character string...");
            printingString();
            fileWriter.close();
     
    return null;
     
        }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Calling method error

    Very little of what you posted looks kosher to me, but the one error you've mentioned is obvious and well described by the error message. The method stringToFile() requires a String object as a parameter, and you're passing it a Scanner object instead.

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Calling method error

    How do I satisfy the string requirements need for stringToFile(); and readingStringFromFile();?

    I can't figure out how to pass it through.



    import java.io.*;
    import java.util.*;
    public class main {
     
        /**
         *
         * @param args
         */
        public static void main(String[] args)  {
        Scanner keyboard = new Scanner (System.in);//allow for use of keyboard input
     
                    mask(keyboard);
     
                    printingString();
     
                    fileName(keyboard);
     
                    stringToFile();
     
                    readingStringFromFile();
     
     
     
        }
        public static int mask(Scanner keyboard){
            int holder;//creats a temp int
            System.out.print("Enter the encryption mask: ");//asks fro encrytipon
            holder = keyboard.nextInt();//userinput to holder
            keyboard.nextLine();//consumption 
            return holder;//returns encryption mask
     
        }
     
        public static void fileName(Scanner keyboard){
            String fileName ="a";
            System.out.print("\nEnter a file name without extensions: ");
            fileName = keyboard.next();//userinput to fileName
            String completeFileName = fileName + ".txt";
        }
     
        public static void printingString(){
            System.out.println("Original random character string:");
            for (int i = 0; i < 50; i++)//loop to obtain 50 random characters
            {
              char randomChar = (char) ((Math.random()*255)+32);
              System.out.print((randomChar));
            }
    }
     
        public static void stringToFile(String completeFileName, String printingString)
                throws FileNotFoundException {
           System.out.println("Saving Original random character string..."); 
           File myFile = new File (completeFileName);
            Scanner fileReader = new Scanner (myFile);
     
            PrintWriter fileWriter = new PrintWriter (myFile);
            fileWriter.println(printingString);
            fileWriter.close();
        }
     
        public static void readingStringFromFile(String completeFileName)
                throws FileNotFoundException {
            System.out.println("Original random character string from the file");
            File myFile = new File (completeFileName);
            Scanner fileReader = new Scanner (myFile);
     
            String lineFromFile = fileReader.nextLine();
            System.out.println(lineFromFile);
        }

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Passing Strings in methods

    Ignoring the code you've most recently posted and reverting to your original, consider the stringToFile() method's signature:

    public static String stringToFile(String completeFileName)

    The appropriately named (I hope) parameter variable suggests that the name of a file is required as the parameter. That and the code previous suggests the code might obtain the name of the file from the user, something like:

    // Ask user for name of file:
    System.out.print( "Input the name of the file: " );
    String completeFileName = keyboard.nextLine();

    // pass file name to stringToFile()
    stringToFile( completeFileName );

    I'm not sure what happens next or what you're ultimately trying to do, but if you need further help, please explain.

    Keep coding!

  5. #5
    Junior Member
    Join Date
    Mar 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Passing Strings in methods

    Here is the requirement for the lab :

    Each there needs to be a method for
    - obtaining the file name from the keyboard
    - obtaining the mask
    - writing a string to a file
    - reading a string from a file
    - encrypting a string
    - decrypting a string
    - printing a string


    create and save a file (randomCharacters.txt) that is a one line string made up of at least 50 characters: blanks, numbers (1 – 9), upper and lower case letters, and special characters. Your program is then read to the file, print the string, and modify the file input string so that each letter is encrypted. The encryption method that you are to use is to exclusively-or ( ^ ) each of the characters from the original string that was read from file randomCharacters.txt with an integer value (mask) that your program reads. You are then to write the newly encrypted string to a file (encrypted.txt), close the file, reopen and read in the encrypted string. Your program will decrypt the encrypted string by again exclusively-or ( ^ ) each of the encrypted characters with the mask that was read in earlier. You are to print both the encrypted and decrypted version of the string that came off of file encrypted.txt. Your program should be modularized with methods having at least one for:

    --- Update ---

    I figured it out thanks for the help!

Similar Threads

  1. Methods and passing variables
    By bamxmejia in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 5th, 2013, 10:09 PM
  2. About passing value from JFrame to other
    By kvv230892 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 27th, 2012, 02:55 PM
  3. Why and where abstract methods & classes and static methods are used?
    By ajaysharma in forum Object Oriented Programming
    Replies: 7
    Last Post: July 14th, 2012, 01:16 AM
  4. Passing variables with void methods
    By knightmetal in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 21st, 2012, 08:46 PM
  5. Passing Arrays Between Methods
    By kigroy in forum What's Wrong With My Code?
    Replies: 14
    Last Post: September 10th, 2011, 10:10 PM

Tags for this Thread