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

Thread: Read in arguments as a String

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

    Default Read in arguments as a String

    Hi I am a beginner in Java and need to read the arguments from command line into a String ignoring case.

    I tried
    String s = Arrays.toString(args);
        s = s.toLowerCase(); 
        System.out.print(s);

    And for arguments abcd Abcd

    It is printing out

    [acbd, abcd]

    Shouldn't it be printing out abcdabcd? I don't understand why it comes with square brackets. Can anyone explain it to me? Tks.


  2. #2
    Junior Member
    Join Date
    Nov 2011
    Location
    Aarhus, Denmark
    Posts
    28
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Arrays.toString is a method for visualising arrays, so the square brackets are there to show that it is an array.

    Try and explain what it is exactly you want your code to do.

    Rolf

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

    Default Re: Read in arguments as a String

    I want to change the arguments from the command line to a single string with no upper case. For example, if args[0]= Hello and args[1]= World it would become a string= "hello world"

  4. #4
    Junior Member
    Join Date
    Nov 2011
    Location
    Aarhus, Denmark
    Posts
    28
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Try having a look at the javadoc for the String class and search for Java string concatenation on Google.

    Rolf

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

    Default Re: Read in arguments as a String

    Thanks, I wrote:
     String str = args.toString();
     
        String s = str.toLowerCase();
     
        System.out.print(s);

    But now it is printing the address of s. I need to use method parseToString?

  6. #6
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    646
    My Mood
    Sleepy
    Thanks
    10
    Thanked 95 Times in 88 Posts

    Default Re: Read in arguments as a String

    No, you have to iterate over args and concatenate them to one String.

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

    Default Re: Read in arguments as a String

    I thought about it, but to concatenate I would need to know how many arguments the user set right? The idea would be a generalized code so arguments would become a single string regardless of how many arguments the user would set. Is there a way of concatenate as many arguments as the user set? I can't limit the number of arguments. Tks.

  8. #8
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    646
    My Mood
    Sleepy
    Thanks
    10
    Thanked 95 Times in 88 Posts

    Default Re: Read in arguments as a String

    Have you ever read through the Basic Java tutorial? You can easily find out how many elements an array has.

  9. The Following User Says Thank You to PhHein For This Useful Post:

    dianac (April 25th, 2013)

Similar Threads

  1. How to read a string into an array?
    By javaiscool in forum Java Theory & Questions
    Replies: 5
    Last Post: March 26th, 2013, 09:36 PM
  2. Replies: 3
    Last Post: March 23rd, 2013, 08:20 PM
  3. How To Read jar arguments in javaswing JtextField
    By orasysco in forum AWT / Java Swing
    Replies: 1
    Last Post: September 8th, 2012, 08:55 AM
  4. Replies: 1
    Last Post: April 19th, 2012, 03:46 AM
  5. [SOLVED] Read double from console without having to read a string and converting it to double.
    By Lord Voldemort in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: June 26th, 2011, 09:08 AM