Question (problem) with arrays and user input

T

The DUDE

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, (e-mail address removed)
smith, bob, (e-mail address removed)

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);
}
}
}
 
B

Brad BARCLAY

The said:
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, (e-mail address removed)
smith, bob, (e-mail address removed)

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
 
K

Kabal

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, (e-mail address removed)
smith, bob, (e-mail address removed)

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());
}
}
 
T

The DUDE

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

(e-mail address removed) (Kabal) wrote in message > // Hope this helps.
 
Joined
Nov 29, 2010
Messages
1
Reaction score
0
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..
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top