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

Thread: One string Text message into array of strings(letters)

  1. #1
    Member
    Join Date
    Apr 2013
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default One string Text message into array of strings(letters)

    I need to convert a text message from the command line into an array of letters.

    args(0) is a string, for example "The sky is beautiful today". And I want it to become theskyisbeautifultoday

    args(o) could be any sentence

    Eclipse keep showing error for:

    String str = args(0);
    char[] chs = str.toCharArray();


    What could be wrong?


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: One string Text message into array of strings(letters)

    args is an array. if you want to access the first element, i.e. your sentence the syntax is
    String str = args[0];

  3. #3
    Member
    Join Date
    Apr 2013
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: One string Text message into array of strings(letters)

    Thanks for your reply
    Actually, args(0) = "The sky is beautiful today"
    So args(0) is one string
    args(0) is set by the user and could be any other text message

  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: One string Text message into array of strings(letters)

    Note:
    [] are used with arrays
    () are used with methods and constructors
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Apr 2013
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: One string Text message into array of strings(letters)

    Yes, sorry.

    So I don't need to convert it to an array.

    I need to change args(0) from "The sky is beautiful today" to theskyisbeautifultoday

    I have tried

    String [] mess = args(0).toLowerCase(args(0));

    but I am not sure it is right and Eclipse keeps returning error.

    Where did I go wrong?

    Thanks

  6. #6
    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: One string Text message into array of strings(letters)

    What is args(0)? See post#4

    Eclipse keeps returning error.
    Please copy the full text of the error messages and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Apr 2013
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: One string Text message into array of strings(letters)

    args(0) = "The sky is beautiful today"

    I will encrypt this message later in a Caesar Box.

    The error message is:

    The method args(int) is undefined for the type CaesarBox

    *ps: CaesarBox is the Class name


    I understand this error could happen if I don't set an input, in this case if I don't set args(0), but I did.

    Thanks.

  8. #8
    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: One string Text message into array of strings(letters)

    You are still not coding arrays correctly:
    () are used with methods
    [] are used with arrays
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Apr 2013
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: One string Text message into array of strings(letters)

    args(0) is the input argument set by the user(text message)

    --- Update ---

    Sorry, args[0]

    So it would be

    String [] mess = args[0].toLowerCase(args[0]);

  10. #10
    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: One string Text message into array of strings(letters)

    args[0] is a String not an array of String
    Try: String mess = args[0]....
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Apr 2013
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: One string Text message into array of strings(letters)

    String [] mess = args[1].toLowerCase(args[1]);

    Error:

    args cannot be resolved to a variable

    --- Update ---

    Sorry just saw your reply now.

    --- Update ---

    String mess = args[0].toLowerCase(args[0]);

    The error continues

  12. #12
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: One string Text message into array of strings(letters)

    If args cannot be resolved then you did not call your parameter in the main method args. Please post your full code and use code tags
    Improving the world one idiot at a time!

  13. #13
    Member
    Join Date
    Apr 2013
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: One string Text message into array of strings(letters)

    public static void main(String[] args) {
    // CaesarsBox <-encrypt|-decrypt>

    if(args[0].equals("-encrypt")){
    System.out.println(encrypt(args[1]));
    }
    else if(args[0].equals("-decrypt")){
    System.out.println(decrypt(args[1]));
    }
    }

    public static String encrypt(String plainText) {


    String mess = args[1].toLowerCase(args[1]);



    }

    public static String decrypt(String cypherText) {

    }

    }




    How to use code tags?

  14. #14
    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: One string Text message into array of strings(letters)

    How to use code tags?
    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Apr 2013
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: One string Text message into array of strings(letters)

    An example is below:

    public class CaesarBox {
     
    	public static void main(String[] args) {
    		// CaesarsBox <-encrypt|-decrypt> 
     
    		if(args[0].equals("-encrypt")){
    			System.out.println(encrypt(args[1]));
    		}
     
    	}
     
    	public static String encrypt(String plainText) {
     
     
    	String s = plainText.toLowerCase();
     
        System.out.println(s);
     
        return s;
     
     
     
     
    	}
     
    }

    The problem now is: why it is not printing s?

  16. #16
    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: One string Text message into array of strings(letters)

    What is the value of args[0] when the program is executed?
    If you don't understand my answer, don't ignore it, ask a question.

  17. The Following User Says Thank You to Norm For This Useful Post:

    dianac (April 11th, 2013)

  18. #17
    Member
    Join Date
    Apr 2013
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: One string Text message into array of strings(letters)

    Hi Norm
    Thank you
    It was - encrypt but should have been -encrypt, so a blank space was the problem.

    Now I need to take out all the blank spaces of s. Any idea of how could I do that?

  19. #18
    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: One string Text message into array of strings(letters)

    What is on the command line when the program is executed? If the user entered:
    java CaesarBox - encrypt
    then args[0] woluld be "-" and args[1] would be "encrypt"

    Does the program want to allow that or should it require that the user enter:
    java CaesarBox -encrypt
    with no spaces?
    If you don't understand my answer, don't ignore it, ask a question.

  20. #19
    Member
    Join Date
    Apr 2013
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: One string Text message into array of strings(letters)

    only with no spaces. Thanks

  21. #20
    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: One string Text message into array of strings(letters)

    If the user's input is not right, tell the user what the valid choices are and exit the program.
    If you don't understand my answer, don't ignore it, ask a question.

  22. #21
    Member
    Join Date
    Apr 2013
    Posts
    69
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: One string Text message into array of strings(letters)

    Now it is right. This was solved. Thanks.

Similar Threads

  1. [SOLVED] Double key Caesar Cipher (USING A STACK AND A QUEUE TO SHIFT LETTERS OF A STRING)
    By Medo Almasry in forum What's Wrong With My Code?
    Replies: 16
    Last Post: November 19th, 2011, 04:18 PM
  2. [SOLVED] Replacing letters in a string NOT WORKING.
    By sp11k3t3ht3rd in forum What's Wrong With My Code?
    Replies: 8
    Last Post: January 14th, 2011, 06:38 PM
  3. Text File into String Array
    By mathanv in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 12th, 2010, 05:30 AM
  4. Reading lines of a text file into a string array
    By fortune2k in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: November 11th, 2010, 11:56 AM
  5. Replies: 1
    Last Post: April 7th, 2010, 03:44 PM