I need to display multiple list arrays in one line through the for loop - so basically I need to count them as well.
Example:
Here is my code:
List<String> name = new ArrayList<String>();
List<String> number = new ArrayList<String>();
name.add("Tommy"); // Test name
name.add("Tom"); // Test name 2
number.add(new String("123-456-7890")); // Test phone number
number.add(new String("098-765-4321")); // Test phone number 2
for (String d:name) {
System.out.print(d);
}
for (String b:number) {
System.out.println(b);
}
And here is my output with my code:
Sorry if this question was duplicated, if it is, I will delete my question right away but for now I haven't fount anything like this.
Thank you.
I think in this case you are better off using a Map, which allows you more flexibility and better control over the entries:
import java.util.HashMap;
import java.util.Map;
/**
* Created by dlcol_000 on 3/19/2016.
*/
public class MapExample {
public void execute(){
Map<String,String> phoneDirectory = new HashMap<>();
phoneDirectory.put("Tommy","123-456-7890");
phoneDirectory.put("Tom", "098-765-4321");
//Now you can do things like, look up the phone number for a person:
String tomsPhone = phoneDirectory.get("Tom");
System.out.println("Toms' phone number is: " + tomsPhone);
//Or you can print the contents of the phone directory
System.out.println("The phone directory contains: " + phoneDirectory);
//Or you can iterate over all people in the directory printing their numbers
for(String person: phoneDirectory.keySet()){
System.out.println(person + ": " + phoneDirectory.get(person));
}
}
public static void main(String... args){
new MapExample().execute();
}
}
Which gives the following output:
Toms' phone number is: 098-765-4321
The phone directory contains: {Tom=098-765-4321, Tommy=123-456-7890}
Tom: 098-765-4321
Tommy: 123-456-7890
You are printing complete the name list and then the number list, you should instead intercalate/alternate and print one from each list for every iteration
final List<String> name = new ArrayList<String>();
final List<String> number = new ArrayList<String>();
name.add("Tommy"); // Test name
name.add("Tom"); // Test name 2
number.add(new String("123-456-7890")); // Test phone number
number.add(new String("098-765-4321")); // Test phone number 2
for (int i = 0; i < name.size(); i++) {
System.out.println(name.get(i) + "\t\t" + number.get(i));
}
at the end I will suggest you as comment above to define a Class "ContactPerson" and override the toString method.
With that approach you will get it faster and safer
It really depends on what you want to use your objects for.
One important aspect is that one should separate concerns. In your case:
there is a concern about "persons"; where each person probably has one name; and one ... or maybe several (!) phone numbers
there is a concern on how certain information is modeled internally. Of course, you can think of a phone number as a simple string with numbers. But you could also design a whole class that holds phone numbers.
there is a concern on how certain information is displayed.
Based on that, you could the following for starters:
create a class "Person" that has two fields; a String for the name; and a String for the phone number
override the toString() method that contains both name and phone number
And all of a sudden, when iterate your list of Person objects and call System.out.println(person.toString()) for each person; you have nicely formatted output.
My key message to you: understand that there are plenty of choices; and shouldn't stop on the first "working" version; but play around ...
Related
My task is:
The program should read items from the user. When all the items from the user have been read, the program prints the information of each item.
For each item, its identifier and name should be read. If the identifier or name is empty, the program stops asking for input, and prints all the item information. Modify the program so that after entering the items, each item is printed at most once. Two items should be considered the same if their identifiers are the same (there can be variation in their names in different countries, for instance).
If the user enters the same item multiple times, the print uses the item that was added first.
I have done the code below, but it will still add items with the same identifier.
Why? How can I fix it?
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<String> itemsName = new ArrayList();
ArrayList<String> itemsIdentifier = new ArrayList();
while(true){
System.out.println("Identifier? (emppty will stop)");
String identifier = scanner.nextLine();
if( identifier.isEmpty()){
break;
}
System.out.println("Name? (emppty will stop");
String name = scanner.nextLine();
if(name.isEmpty()){
break;
}
if(!(itemsIdentifier.contains(identifier))){
itemsIdentifier.add(identifier);
}
if(!(itemsName.contains(name))){
itemsName.add(name);
}
}
System.out.println("==Items==");
int j = 0;
for(String i: itemsIdentifier){
System.out.println(i + ": "+ itemsName.get(j));
j++;
}
}
}
I think the problem with your code is that you are adding name into itemsName list even when you are not adding identifier into itemsIdentifier list in the following lines
if(!(itemsIdentifier.contains(identifier))){
itemsIdentifier.add(identifier);
}
if(!(itemsName.contains(name))){
itemsName.add(name);
}
Ideally shouldn't you either add both name and identifier or don't add any?
You have a while(true) loop which will keep going indefinitely, you are breaking the loop only if the user input is empty, but you are not doing anything when the lists already contain an identifier more than once. (You are adding to the list if you have a unique identifier).
EDIT
I have other misgivings on the code above (I am assuming that this is an assignment), but since I do not know if you built this on top of what the lecturer gave you, I can only speculate. Some thoughts on the code above:
You could create your own Record object.
As per the instructions, you would need to override the equals method.
Instead of having two lists, you would have only 1 list of type Record.
Try something like this:
if(!(itemsIdentifier.contains(identifier))) {
itemsIdentifier.add(identifier);
itemsName.add(name);
}
In your code, if the id is already in the list, the name could still be added...
You only need to check whether the identifier is similar or not. The name similarity condition is not required. As long as the identifiers are different( though they have the same name), you still add them to the itemsIdentifier and itemsName. On the other hand, if the identifiers are identical, there is no need to run a check on the similarity of the name.
This is a java program that works with a class file that I did not list here. My problem is:
It prints the list from the file just fine, but I need to now make the program ask the user for a last name. The program then has to check from the file to see
if that last name was included and
if it was to then print out the information profile of that last name such as the first name and the quiz average. Also
if it is not a last name used in the program to then state no last name found
Here is my code:
import java.util.Scanner;
import java.io.*;
import java.util.ArrayList;
import java.io.File;//you need this to read the Employee file
import java.io.FileNotFoundException;
public class QuizAverages8 {
public static void main(String[] args) throws FileNotFoundException {
String sFindStu ="";
ArrayList<Student> StudentList = new ArrayList<Student>();
generateEmpList(StudentList);
System.out.println("STUDENT ROSTER AND QUIZ AVERAGES");
System.out.println("");
System.out.println("Students in class: " + StudentList.size());
System.out.println("");
for (int nIndex = 0; nIndex < StudentList.size(); nIndex++){
printEmployee(StudentList.get(nIndex));
}
// This is where I want to test to check the program if the last name is used
System.out.print("Please enter the last name of a specific student: " );
sFindStu = input.next();
if (sFindStu.equals(StudentList.contains(args))){
System.out.println(StudentList.get(nIndex));
}
else{
System.out.println("No name with " + sFindStu + "Found");
}
// It ends here
}
public static void printEmployee(Student myStudent){
System.out.println("Employee Name: " + myStudent.getFirstName() + " " + myStudent.getLastName().toUpperCase());
System.out.println("Quiz average: " + myStudent.calculateQuizAverage());
System.out.println(" ");
}
public static void generateEmpList(ArrayList<Student> list) throws
FileNotFoundException{
String sFileName = "Students.txt";
String sInputLine = " ";
File fileToOpen = new File(sFileName);
Scanner inputFile = new Scanner(fileToOpen);
String[] saTokens = null;
ArrayList<Student> empList = new ArrayList<Student>();
while(inputFile.hasNext()){
sInputLine = inputFile.nextLine();
saTokens = sInputLine.split("-");
Student emp = new Student();//create empty object
emp.setFirstName(saTokens[0]);
emp.setLastName(saTokens[1]);
emp.setQuiz1(Integer.parseInt(saTokens[2]));
emp.setQuiz2(Integer.parseInt(saTokens[3]));
emp.setQuiz3(Integer.parseInt(saTokens[4]));
list.add(emp);
}
}
}
So, boils down to:
if (sFindStu.equals(StudentList.contains(args))){
being wrong on so many levels. You see, the point of being a "coder" is about understanding the abstractions you are using. How do you understand them? By reading their documentation. By using a search engine that leads you to tutorials others have written.
Meaning: ArrayList is a Java Collection, and yes, therefore it provides a contains() method. As the javadoc suggests:
Returns true if this list contains the specified element.
But of course, Lists are generic, so when you have a List<String> then you can only pass a single string to it, like:
if (someStringList.contains(someString))
The compiler won't let you pass an array, because you declared a List of String. You can't ask a list of eggs whether it contains a sampler of eggs.
So, as written: instead of asking the list if contains an array of strings (which by the way: the args you are passing there has nothing to do with your other code, does it!), ask the list if it contains, like the player name:
if (StudentList.contains(sFindStu)) {
Done.
And of course: the real answer is that you should change the way you approach problems. You seem to pull together concepts without spending much time understanding them. That is a pretty inefficient strategy. You have to understand each and any character that you write down in your source code. Instead of throwing together things of which you assume they maybe luckily do what you need them to do.
Beyond: read about Java naming conventions.
I'm working on a program that will allow a user to use a console menu to input various things into an array.
Input instances of the class student into the array which I have done and works.
Create instances of course details which I have also done.
And search the array for a particular students details. If a student with this name exists, it will print all of their details that are stored in the array and if not will throw up a message saying something like "Student not on Course".
I'm just assuming student names are unique as I only need to store 20 of them. I have created this method so far which doesn't seem to be working. I think I need to use a for loop instead of binarySearch but I'm not quite sure how to do that as no videos or posts seem to show how to use this loop with a String and a Scanner.
This is the method so far:
public static void Search()
{
String studentName;
#SuppressWarnings("resource")
Scanner searchScanner = new Scanner(System.in);
System.out.println ("Type Student Name to Search");
studentName = searchScanner.nextLine();
int FoundName;
Arrays.sort(Students);
FoundName = Arrays.binarySearch(Students, studentName);
if (FoundName > -1)
{
System.out.println("FoundName is" + Students[FoundName]);
} else
{
System.out.println("Student not found");
}
}
Any help with this would be greatly appreciated, you would be helping a lowly noob out very much :)
If you prefer to do a sequential search, you can use a for loop like this:
private void Search(){
// Create a scanner for input, and get the name for search
Scanner inputScanner = new Scanner(System.in);
System.out.println("Type student name for search:");
String studentName = inputScanner.nextLine();
// use for loop to search array.
for(int i = 0; i < studentArray.length; i++){
if(studentArray[i].getName().equals(studentName)){
// If student was found, print his details and return from this function.
System.out.println(studentArray[i]);
return;
}
}
// If we reach this point, it means the student was never found in the for loop.
System.out.println("Student not found.");
}
A couple things to note:
In your question, you're comparing a student object with a string. The difference in types alone is enough to make the binary search return false, so you will never get a match.
In my loop, I am assuming the array holds Student objects, so I call getName() on the student object to compare the two strings, so I will get a match if one exists.
Printing the student object itself will not just print values, you need to override toString() if you haven't yet.
First of all don't do the SuppressWarnings. It's a bad practice and not good when you are beginning to learn to start with bad practices.
I would use instead of the binarySearch for your case, just use the contains method in a list, like this:
List <String> list = new ArrayList();
list.add("one");
list.add("two");
if (list.contains("two")) {
System.out.println("true");
}
In addition no need to sort the array : ).
Im currently making a shopping store application, I have 6 classes. 1 for products where it defines the fields for products in the store, another for the shopping basket, one for the GUI and the rest for listeners.
I need to be able to run a method that runs through an array list and running the to.String method on it and returning it as String. Here is what I have at the moment,
private ArrayList<OrderItem> basket = new ArrayList<OrderItem>();
public String createArrayText () {
for (int i = 0; i < OrderItem.size(); i++){
if (i == OrderItem.size() - 1){
return ordersText ;
}
}
}
ordersText is a variable I made at the top of my shopping cart class.
This was my first start at it however I'm getting a error on the .size and obviously missing some key components.
One thing Extra is that each item created is added to the array list, each item has a unique order number.
Arrays.toString(basket);
Is that what you're looking for? If not, you need to explain a little better.
You generally speaking loop over a List like this (Java 7, it's called enhanced for loop):
for (TYPE TEMP_NAME : COLLECTION) {
}
That's the overall syntax. TYPE is the type of item in the list, yours are Object's in the given code. TEMP_NAME is the temporary name you want each entry to be referred as. COLLECTION is the list/array/stack/queue or other Collection.
Concretely:
for (Object o : basket) {
// if basket contains 10 entries the line will run 10 times. each new run contains a different object with name o
}
Normally when building strings it's preferred to use StringBuilder. We can skip that as it's "only" performance that you gain from it. We'll do it with a regular String. So:
Create an empty string that will get longer and longer
Loop the collection/array/list
Append each object's .toString() to the string from 1)
e.g.
String myReturnString = "";
for (Object o : basket) {
myReturnString = myReturnString + " // " + o.toString();
}
return myReturnString;
Notes:
Your loop with an index is fine. You can do it that way too, if you want to.
The line of code that appends the string has a " // " separator between each entry. You can pick whatever you like. You can also simplify it to be myReturnString += " // " + o.toString();
I used the following coding to display user accounts in my domain.But in that coding it display only first 100 records.But in my domain nearly 500 users account.I don't know what problem in this coding
import java.net.URL;
import java.util.List;
import com.google.gdata.client.appsforyourdomain.UserService;
import com.google.gdata.data.appsforyourdomain.provisioning.UserEntry;
import com.google.gdata.data.appsforyourdomain.provisioning.UserFeed;
public class Readuser {
public int i3;
public String rn[]=new String[100];
public void read(){
try
{
// Create a new Apps Provisioning service
UserService myService = new UserService("My Application");
myService.setUserCredentials(admin,password);
// Get a list of all entries
URL metafeedUrl = new URL("https://www.google.com/a/feeds/"+domain+"/user/2.0/");
System.out.println("Getting user entries...\n");
UserFeed resultFeed = myService.getFeed(metafeedUrl, UserFeed.class);
List<UserEntry> entries = resultFeed.getEntries();
for(i3=0; i3<entries.size(); i3++) {
UserEntry entry = entries.get(i3);
rn[i3]= entry.getTitle().getPlainText();
System.out.println(rn[i3]);
}
System.out.println("\nTotal Entries: "+entries.size());
}
catch(Exception e) { System.out.print(e);}
}
public static void main(String args[])
{
Readuser ru=new Readuser();
ru.read();
}
}
You only allocate 100 entries.
public String rn[]=new String[100];
Hint from your code : public String rn[]=new String[100];
Do you really need to have i3 and rn as class members ? Do you really need rn ? A List seems more comfortable as an Object than a String[].
There is no need for the string array (String[]).
Arrays are fixed size; and in this case you have allocated 100 "slots" for Strings, and when You try to assign a string to position 100 ( you know, the 101:th string) it fails.
You catch an exception in the end. Print the stack trace to find out whats going on
catch(Exception e) {
e.printStackTrace();
}
Learn to read it an find out what is says... However you should not catch the exception in this method. It is better to abort whatever the program was doing. Catch it in your main method - just printing or logging it is fine, so that you can correct the programming error.
Anyway; The result you get is a List of user entries. Lists are part of the (java.util)collections framework. Collections have a lot of features; in this case you want to iterate over all entries in the list. You can do this by using the iterator() method -read the javadoc...OR you can use for-loop syntactic sugar for doing this:
for( UserEntry user : entries ) {
// user is the current UserEntry
System.out.println(user.getTitle().getPlainText());
}
The variables i3 and rn are no good... They shouldn't be class variables, and if you need "temporary" variables, define them close to where you are going to use them.
As for naming of variables, a name like "entry" is less useful than "user". Actually a class called UserEntry should probably be called just User, but I don't know about this API, so...