Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Question (problem) with arrays and user input

Reply
Thread Tools

Question (problem) with arrays and user input

 
 
The DUDE
Guest
Posts: n/a
 
      09-10-2003
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, http://www.velocityreviews.com/forums/(E-Mail Removed)
smith, bob, (E-Mail 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[i]);
}
}
}
 
Reply With Quote
 
 
 
 
Brad BARCLAY
Guest
Posts: n/a
 
      09-10-2003
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, (E-Mail Removed)
> smith, bob, (E-Mail 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

--
=-=-=-=-=-=-=-=-=
From the OS/2 WARP v4.5 Desktop of Brad BARCLAY.
The jSyncManager Project: http://www.jsyncmanager.org


 
Reply With Quote
 
 
 
 
Kabal
Guest
Posts: n/a
 
      09-10-2003
(E-Mail Removed) (The DUDE) wrote in message news:<(E-Mail Removed). com>...
> 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 Removed)
> smith, bob, (E-Mail 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());
}
}
 
Reply With Quote
 
The DUDE
Guest
Posts: n/a
 
      09-10-2003
thanks, that is sort of what I was wanting to do.

(E-Mail Removed) (Kabal) wrote in message > // Hope this helps.
>

 
Reply With Quote
 
acidoerez acidoerez is offline
Junior Member
Join Date: Nov 2010
Location: Manila
Posts: 1
 
      11-29-2010
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..
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
Entering strings as user input but interpreting as Python input (sortof) Chris Carlen Python 1 09-18-2007 01:58 AM
Arrays - Storing User Input woodyee Ruby 6 03-09-2007 03:36 PM
Getting User Input after getting Input from a file dei3cmix@uga.edu C++ 3 03-23-2006 05:01 AM
can I use scanf to get input (some times user enters input sometimes not, just hit keyboard)? santa19992000@yahoo.com C Programming 4 09-09-2005 03:38 AM



Advertisments