Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Can i take input of strings in an array

  1. #1
    Join Date
    Sep 2012
    Location
    Guntur
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Question Can i take input of strings in an array

    Hey guys i got a doubt

    Can i take 5 strings as input from command prompt in an array

    I am doing a little task

    write a Program to Declare 5 set of words and reverse each one and arrange the resulting words in Alphabetical Order


    can i take input in an array??

    usually i did like this

    Scanner in= new Scanner(System.in);
    String a,b,c,d,e;
    a=in.nextLine();
    b=in.nextLine();
    c=in.nextLine();
    d=in.nextLine();
    e=in.nextLine();

    String ar= new StringBuffer(a).reverse().toString();
    String br= new StringBuffer(b).reverse().toString();
    String cr= new StringBuffer(c).reverse().toString();
    String dr= new StringBuffer(d).reverse().toString();
    String er= new StringBuffer(e).reverse().toString();

    i reversed and stored a b c d e strings in ar br cr dr er
    now i have to sort those 5 strings..

    how can i do that???

    can i pass those results to an array???

    If i can pass them to an array we can sort them by using

    CharArray.sort();

    can i pass them to an array??..


    Any Suggestions????????

  2. #2
    Join Date
    Sep 2012
    Beans
    11

    Re: Can i take input of strings in an array

    Quote Originally Posted by Mohan1289 View Post
    Scanner in= new Scanner(System.in);
    String a,b,c,d,e;
    a=in.nextLine();
    b=in.nextLine();
    c=in.nextLine();
    d=in.nextLine();
    e=in.nextLine();

    String ar= new StringBuffer(a).reverse().toString();
    String br= new StringBuffer(b).reverse().toString();
    String cr= new StringBuffer(c).reverse().toString();
    String dr= new StringBuffer(d).reverse().toString();
    String er= new StringBuffer(e).reverse().toString();
    So that's most the work. The rest would be like.

    Code:
    ArrayList<String> words = new ArrayList<String>();
    Scanner in= new Scanner(System.in);
    String a,b,c,d,e;
    a=in.nextLine();
    b=in.nextLine();
    c=in.nextLine();
    d=in.nextLine();
    e=in.nextLine();
    
    words.add(new StringBuffer(a).reverse().toString());
    words.add(new StringBuffer(b).reverse().toString());
    words.add(new StringBuffer(c).reverse().toString());
    words.add(new StringBuffer(d).reverse().toString());
    words.add(new StringBuffer(e).reverse().toString());
    
    Collections.sort(words);
    Or you could do it with a primitive array and Arrays.sort(myArray)

  3. #3
    Join Date
    Sep 2012
    Location
    Guntur
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Can i take input of strings in an array

    Would you please explain to me what you did i am bit confused

  4. #4
    Join Date
    Sep 2012
    Beans
    11

    Re: Can i take input of strings in an array

    Sure.

    I added this line at the top.

    ArrayList<String> words = new ArrayList<String>();

    What it does is create something called an "ArrayList". An ArrayList is like a normal array, just it can do some other stuff since it it's actually an object.


    Then instead of storing your reversed words into variables, I just added them to the ArrayList immediately. I could have just used the variables too. So when I did say this:

    words.add(new StringBuffer(a).reverse().toString());

    It could have been this.

    words.add(ar);

    I just took out the middle step of storing it in a String variable.

    Also,

    words.add(string)

    is the same as

    words[i] = string;

    where i is some integer.

    Finally, I used the method "sort" of the Collections class to sort the ArrayList alphabetically!.

  5. #5
    Join Date
    May 2010
    Location
    SE England
    Beans
    210

    Re: Can i take input of strings in an array

    This looks very much like homework!



    Bouncingwilf

  6. #6
    Join Date
    Sep 2012
    Location
    Guntur
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Can i take input of strings in an array

    Quote Originally Posted by PoxSpax View Post
    Sure.

    I added this line at the top.

    ArrayList<String> words = new ArrayList<String>();

    What it does is create something called an "ArrayList". An ArrayList is like a normal array, just it can do some other stuff since it it's actually an object.


    Then instead of storing your reversed words into variables, I just added them to the ArrayList immediately. I could have just used the variables too. So when I did say this:

    words.add(new StringBuffer(a).reverse().toString());

    It could have been this.

    words.add(ar);

    I just took out the middle step of storing it in a String variable.

    Also,

    words.add(string)

    is the same as

    words[i] = string;

    where i is some integer.

    Finally, I used the method "sort" of the Collections class to sort the ArrayList alphabetically!.

    So What's the use of Collection... Is the Method Sort is pre defined in Collection Class??

  7. #7
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,172
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Can i take input of strings in an array

    Quote Originally Posted by Mohan1289 View Post
    So What's the use of Collection... Is the Method Sort is pre defined in Collection Class??
    Collections (with an S) is a catch-all class for static methods that act on objects implementing the Collection (without S) interface.

  8. #8
    Join Date
    Sep 2012
    Location
    Guntur
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Can i take input of strings in an array

    Little English please... Not completely Technical

    I am little poor at that. What is a "catch-all" class??

  9. #9
    Join Date
    Apr 2007
    Location
    NorCal
    Beans
    1,149
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Can i take input of strings in an array

    Quote Originally Posted by Mohan1289 View Post
    Little English please... Not completely Technical

    I am little poor at that. What is a "catch-all" class??
    He is saying that the Collections class tries to do a lot of different things that aren't necessarily related to each other. "Catch-all" is not a technical term, it is just an English term to describe the way the class was designed.

    Now, for you post. I hope that you have learned about loops; if not, this might not make sense. But try to look around at tutorials and see if you can understand it.

    Code:
    Scanner input = new Scanner(System.in);
    String[] array = new String[5];
    
    for (int i = 0; i < 5 && input.hasNextLine(); i++) {
      String line = input.nextLine();
      String reversed = reverse(line);
      array[i] = reversed;
    }
    This works by using a "for loop" to get another line from the scanner 5 times. Then, for each line it finds, it reverses it and places the reversed version in an array named "array".

    It's pretty easy to extend this so that it will read every line in the input scanner.

    Code:
    Scanner input = new Scanner(System.in);
    ArrayList<String> list = new ArrayList<String>();
    
    while (input.hasNextLine()) {
      String line = input.nextLine();
      String reversed = reverse(line);
      list.add(reversed);
    }
    This is similar to the above example, except instead of being limited to 5 lines, it will read in as many lines as it can.
    Last edited by schauerlich; October 3rd, 2012 at 06:37 AM.
    Posting code? Use the [code] or [php] tags.
    I don't care, I'm still free. You can't take the sky from me.

  10. #10
    Join Date
    Sep 2012
    Location
    Guntur
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Can i take input of strings in an array

    Quote Originally Posted by schauerlich View Post
    He is saying that the Collections class tries to do a lot of different things that aren't necessarily related to each other. "Catch-all" is not a technical term, it is just an English term to describe the way the class was designed.

    Now, for you post. I hope that you have learned about loops; if not, this might not make sense. But try to look around at tutorials and see if you can understand it.

    Code:
    Scanner input = new Scanner(System.in);
    String[] array = new String[5];
    
    for (int i = 0; i < 5 && input.hasNextLine(); i++) {
      String line = input.nextLine();
      String reversed = reverse(line);
      array[i] = reversed;
    }
    This works by using a "for loop" to get another line from the scanner 5 times. Then, for each line it finds, it reverses it and places the reversed version in an array named "array".

    It's pretty easy to extend this so that it will read every line in the input scanner.

    Code:
    Scanner input = new Scanner(System.in);
    ArrayList<String> list = new ArrayList<String>();
    
    while (input.hasNextLine()) {
      String line = input.nextLine();
      String reversed = reverse(line);
      list.add(reversed);
    }
    This is similar to the above example, except instead of being limited to 5 lines, it will read in as many lines as it can.
    How to compare the reversed Strings???
    can i use sort method??

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •