Question (problem) with arrays and user input

Discussion in 'Java' started by The DUDE, Sep 10, 2003.

  1. The DUDE

    The DUDE Guest

    I am writing a problem to take some user input (firstname, lastname,
    email), store the input into an array, sort the input by lastname and
    then print out all the input in the following format:

    abba, joe,
    smith, bob,

    Here is what I have some far, can anyone help? Any suggestions?

    public class Records {
    public static void main(String[] args) {
    String firstname, lastname, email = null;
    String[] list = new String[100];
    int curPos = 0;

    // quesdo code
    while (still more user input) {
    firstname = input1;
    lastname = input2;
    email = input3;
    list[curPos++] = lastname + ", " + firstname + ", " + email;
    }

    Arrays.sort(list);

    for (int i=0; i<list.length; i++) {
    System.out.println(list);
    }
    }
    }
    The DUDE, Sep 10, 2003
    #1
    1. Advertising

  2. The DUDE

    Brad BARCLAY Guest

    The DUDE wrote:
    > I am writing a problem to take some user input (firstname, lastname,
    > email), store the input into an array, sort the input by lastname and
    > then print out all the input in the following format:
    >
    > abba, joe,
    > smith, bob,
    >
    > Here is what I have some far, can anyone help? Any suggestions?


    This isn't a terribly object-oriented way to do what you're trying to
    achieve.

    Instead of using seperate Strings and doing concatenation of them,
    you'd be better off creating a class that contains the three Strings you
    want to store for each record, and then add a toString() method to it to
    satisfy your display requirements.

    You can then have an array of these objects. You can sort them based
    purely on the lastname field.

    I hope this helps!

    Brad BARCLAY

    --
    =-=-=-=-=-=-=-=-=
    From the OS/2 WARP v4.5 Desktop of Brad BARCLAY.
    The jSyncManager Project: http://www.jsyncmanager.org
    
    Brad BARCLAY, Sep 10, 2003
    #2
    1. Advertising

  3. The DUDE

    Kabal Guest

    (The DUDE) wrote in message news:<>...
    > I am writing a problem to take some user input (firstname, lastname,
    > email), store the input into an array, sort the input by lastname and
    > then print out all the input in the following format:
    >
    > abba, joe,
    > smith, bob,
    >
    > Here is what I have some far, can anyone help? Any suggestions?



    // Hope this helps.


    import java.io.BufferedReader;
    import java.io.InputStreamReader;

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Iterator;
    import java.util.List;
    import java.util.StringTokenizer;

    class TestNames {

    public static void main(String[] args) {
    List list = new ArrayList();
    StringTokenizer st = null;
    String line = null;

    try {
    BufferedReader in =
    new BufferedReader(new InputStreamReader(System.in));

    // Get input from user
    while ((line = in.readLine()) != null) {
    st = new StringTokenizer(line, " ");

    // Add names to list
    list.add(
    new Names(
    String.valueOf(st.nextElement()), // firstname
    String.valueOf(st.nextElement()), // lastname
    String.valueOf(st.nextElement()) // email
    )
    );
    }

    // Print out list of names if there are anys
    if (!list.isEmpty()) {
    Collections.sort(list);

    Iterator it = list.iterator();

    while (it.hasNext()) {
    System.out.println(((Names)it.next()).toString());
    }
    } else {
    System.out.println("No names were found!");
    }
    } catch (Exception ex) {
    System.out.println("Something bad just happened.");
    ex.printStackTrace(System.err);
    }
    }
    }

    public class Names implements Comparable {
    private String firstname, lastname, email;

    public Names() {
    this("", "", "");
    }

    public Names(String firstname, String lastname, String email) {
    this.firstname = firstname;
    this.lastname = lastname;
    this.email = email;
    }

    public void setFirstname(String s) {
    firstname = s;
    }

    public String getFirstname() {
    return firstname;
    }

    public void setLastname(String s) {
    lastname = s;
    }

    public String getLastname() {
    return lastname;
    }

    public void setEmail(String s) {
    email = s;;
    }

    public String getEmail() {
    return email;
    }

    public String toString() {
    return lastname + ", " + firstname + ", " + email;
    }

    public int compareTo(Object o) {
    return toString().compareToIgnoreCase(o.toString());
    }
    }
    Kabal, Sep 10, 2003
    #3
  4. The DUDE

    The DUDE Guest

    thanks, that is sort of what I was wanting to do.

    (Kabal) wrote in message > // Hope this helps.
    >
    The DUDE, Sep 10, 2003
    #4
  5. The DUDE

    acidoerez

    Joined:
    Nov 29, 2010
    Messages:
    1
    Location:
    Manila
    Hello there!
    I am also new to Java.
    This code by "The DUDE" does not execute as intended.
    Using Netbeans, it runs but does not even execute the line where it should ask for user input. And so I had to force STOP the program. It was like an infinite loop (I guess).
    Kindly look into this code because my project should be almost the same..

    Thanks..
    acidoerez, Nov 29, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rir3760
    Replies:
    1
    Views:
    340
    Dave Thompson
    Apr 4, 2005
  2. Philipp
    Replies:
    21
    Views:
    1,126
    Philipp
    Jan 20, 2009
  3. Kev Jackson
    Replies:
    2
    Views:
    110
  4. woodyee

    Arrays - Storing User Input

    woodyee, Mar 9, 2007, in forum: Ruby
    Replies:
    6
    Views:
    135
    John Joyce
    Mar 9, 2007
  5. Cec Tre
    Replies:
    3
    Views:
    193
    Robert Klemme
    Mar 19, 2010
Loading...

Share This Page