Searching Through Array (List Of Names)
Oct 5, 2014
An array which contain a list of names. I let for a user input and I use that to search through the array. For example lets say the string name "Christian" is stored inside the names array and the user types in Chri, the program looks in the array and finds a name that contains Chri and prints them out. How do I go about doing this?
View Replies
ADVERTISEMENT
Oct 10, 2014
I have an assignment for my intro class that requires me to read from a file that is a list of songs, their artists, and the year they were released. As seen below, a print line statement prompts the user to enter an artist name, and then it uses a buffered reader to gain input, and then it is supposed to match that input.I realize that this is not a complete statement, but I'm mostly concerned with getting the .indexOf statement to work.Currently it only returns the first object in the array.
for(int i = 0; i < song.length; i++) {
System.out.println("Enter an Artist name");
String input1 = kb.readLine();
if (song[i].getArtist().indexOf(input1) > -1) {
/*tried changing -1 to -2. When I do, it returns the
first array entry, regardless of what I input*/
System.out.println(song[i].toString());
}
}
View Replies
View Related
Apr 22, 2015
I have a list of 100,000 + names and I need to append to that list another 100,000 names. Each name must be unique. Currently I iterate through the entire list to be sure the name does not exist. As you can imagine this is very slow. I am new to Java and I am maintaining a 15+ year old product. Is there a better way to check for an existing name?
View Replies
View Related
Apr 30, 2014
Ok here I have a code that generates 1 million random values then converts them to a string then hashcode. I then insert into a linked list and then I want to run through each hash and find it in the linked list timing each run then averaging out the time at the end.
It works great for smaller amounts of numbers it is searching for (fine under 50 thousand searches for the for loop starting at line 24 LinkedListTest.java) but when I try to do the full million searches it gives me "a Exception in thread "main" java.lang.StackOverflowError" at line 158 in List.java. Maybe im getting tired but I cannot figure out why.
// class to represent one node in a list
class ListNode< T >
{
// package access members; List can access these directly
T data; // data for this node
ListNode< T > nextNode; // reference to the next node in the list
[code]....
View Replies
View Related
Nov 1, 2014
I need to display an object, which is in a linked list, given a vin number. It is the display_info method. Here is what i have so far:
import java.util.*;
public class Car implements Comparable<Car>, Cloneable
{
private String model;
private String color;
private int year;
private int vin_number;
private double price;
[code]....
View Replies
View Related
Jan 24, 2015
So I need to be able to find the longest increasing sequence in a 2D grid of numbers. A sequence is simply a series of adjacent squares. Squares may not be used twice. Example here.
97 47 56 36 60 31 57 54 12 55
35 57 41 13 82 80 71 93 31 62
89 36 98 75 91 46 95 53 37 99
25 45 26 17 15 82 80 73 96 17
75 22 63 96 96 36 64 31 99 86
12 80 42 74 54 14 93 17 14 55
14 15 20 71 34 50 22 60 32 41
90 69 44 52 54 73 20 12 55 52
39 33 25 31 76 45 44 84 90 52
94 35 55 24 41 63 87 93 79 24
The longest sequence has length 10, consisting of entries (row, column) as follows;
(5,0) with cost 12
(6,0) with cost 14
(6,1) with cost 15
(6,2) with cost 20
(7,2) with cost 44
(7,3) with cost 52
(7,4) with cost 54
(6,3) with cost 71
(5,3) with cost 74
(4,3) with cost 96
I wouldn't be having a problem if I could use a 2D Array but the assignment requires the use of a List and StringTokenizer.
The way it works, a file is read and each row of the grid is an element of the List of Strings. So in the example above if I were to call myList.get(1) it would return 35 57 41 13 82 80 71 93 31 62 since it's the second row and second index value of the List.
If it was a 2D array I would check values at [x-1][x-1],[x-1][x]... to check all sides and diagonals ect ect. But with the List and Tokenizer there isn't a way that I know of that can search through the String using index values.
I wouldn't even know where to start with that. As of now I read the file fine and make the List of Strings and populate it properly, I'm just a little stuck. I should also mention that which ever method I use to search needs to be recursive.
This is what little I have. It reads in the file, creates the list and assigns the rows to the list.
import java.util.*;
import java.io.*;
public class MaxIncreasingSub {
public static void main(String []args) throws Exception{
File file = new File("Ass1Q2_test1");
List<String> myList = new ArrayList<String>();
[Code] .....
View Replies
View Related
Feb 2, 2014
I'm trying to use the DataFactory class to pull in a list of names, as I need to populate my app in development with randomized test data for testing.
DataFactory
So, I can get a random name like so:-
package domainentities;
import java.util.Random;
import java.util.Collections;
import org.fluttercode.datafactory.AddressDataValues;
[Code] .....
But - it always prints 'Lindsey'. I need a way of completely randomizing but I can't see the class has a method to let me do this? I can use the randomize method but this takes an int argument which will always bring back the same index value (same name).
I wonder if I am over complicating this. I can use a simple String array of names, but I don't want, like 10,000 names in my array and I want a good way of generating good, randomized data. Perhaps a .csv file of names could be read in? Here is a second method I wrote but I don't have the skills to know how to read the array values from my large .csv file:
public String randomSecondName(){
String[] lastNames = {"Smith", "Jones", "Collins","Jackson",
"Dearsley", "Trump", "Carr", "O'Connell", "Dyer", "Furstzwangler" };
Random ran = new Random();
String lastName = lastNames[ran.nextInt(lastNames.length)];
return lastName;
}
View Replies
View Related
Feb 13, 2015
with arrays its binary search which finds a value in O(Logn) time but what about linked lists ? the most effiecient algorithm will be O(n) ? and i know that binary search cannot be implement on a linked list , therefore , the only way to search a linked list is a linear search ?
View Replies
View Related
Oct 16, 2014
One of my assignments was to make a program that would read a sequence of names and then list them all.Just to be clear, it would read them all first, and then it would list them all at the same time.
View Replies
View Related
May 21, 2015
I am trying to write a java program that asks the user for a list of names (one per line) until the user enters a blank line. At that point the program should print out the list of names entered, where each name is listed only once (i.e., uniquely) no matter how many times the user entered the name in the program. I am supposed to use an ArrayList for this problem.
My idea for a solution is to create an ArrayList<String>, read each name, i.e., line, entered and then to see if that name is already in the ArrayList. I created a for loop to check each element in the ArrayList but when I try to assign an element to a string variable I get the error "Type mismatch: cannot convert from Object to String". Not sure why that is happening because the ArrayList is defined as a String list.
Here is the code so far.
/*
* File: UniqueNames.java
*/
import acm.program.*;
import java.util.*;
[Code].....
View Replies
View Related
Sep 29, 2014
a project I am working on. Its a program that creates a singly linked list that stores names and high scores and prints them. For some reason it is printing an entry extra times. Also my remove function is not working properly
GameEntry class:
package project;
public class GameEntry implements Comparable<GameEntry> {
private String name;
private int score;
public GameEntry(String n, int s) {
name = n;
[Code]...
View Replies
View Related
Jul 23, 2014
'm working on a program that is to act as an inventory for a book store. There are two classes (Store and Book), and numerous methods which you will see in my code. The program is supposed to read in an inventory from a file which contains 10 books (each row in the file represents a book with an ISBN number, the price of the book, and the number in stock)and store this file into an array of type Book[]. There is then to be another method to process a purchase interactively. The Store class is also to keep a count of how many books were sold at the time of closing and also how much was made that day.
I'm having some difficulty figuring out how I should set up the purchase processing method though. I've written out how I think it would be set up (not too familiar with the terminology, but I think it would be called pseudo code?) I'll include that now along with a few other notes.
public Book[] purchase(String isbn, double price, int copies) {
int itemsSold;
double totalMade;
Scanner input = new Scanner(System.in);
System.out.println("Please enter the ISBN number of the book you would like to purchase: ");
[Code] ....
So basically here's what needs to be accomplished with this portion of code:
1.Ask the user to enter the ISBN number of the book they'd like to purchase.
2.Search the array for the object that contains that ISBN.
3.If the ISBN isn't found in the array, display a message stating that we don't have that book.
4.If the ISBN is found but the number of copies is 0, display a message saying the book is out of stock.
5.If the ISBN is found and the number of copies is greater than 0, ask the user how many copies they'd like to purchase.
6.If the number they enter is greater than the number of copies of that book in the array, display a message stating that and ask them to enter another quantity.
7.Once the purchase is complete I need to update the array by subtracting the number of copies of that particular book that was purchased.
8.Print the updated array.
9.Display a count of how many books were purchased, and how much money was made from the purchase.
I know that code has a lot of holes in it, but I'm just trying to get everything together and figure out how to do each step.
The part I'm stuck on right now is trying to figure out how to search the array to first see if the ISBN that was entered is in the array, and then to find the number of copies of the book with that ISBN number.
In case it will visualize what this array looks like, here's a print out of the array from a sample run I just did.
ISBN: 1234567, Price: 31.670000, Copies: 0
ISBN: 1234444, Price: 98.500000, Copies: 4
ISBN: 1235555, Price: 27.890000, Copies: 2
ISBN: 1235566, Price: 102.390000, Copies: 6
[Code] ....
View Replies
View Related
Apr 22, 2014
How to pass the methods listed below.
public ArrayList<Person> findPerson(String searchFor)
^ this method is suppose to search an array and find whatever you search (numerical or alphabetical)
I have this
public ArrayList<Person> findPerson(String searchFor) {
Main.handleSearchPerson(keyboard, searchFor);
ArrayList<Person> matches = new ArrayList<>();
for (Person p : personList) {
boolean isAMatch = false;
[Code] .....
I am not sure if how i am searching the method is correct or how to pass that to another class to put it inside of a switch statement.
View Replies
View Related
Oct 5, 2014
An array which contain a list of names. I let for a user input and I use that to search through the array. For example lets say the string name "Christian" is stored inside the names array and the user types in Chri, the program looks in the array and finds a name that contains Chri and prints them out. How do I go about doing this?
View Replies
View Related
Apr 22, 2014
How to pass the methods listed below.
public ArrayList<Person> findPerson(String searchFor)
^ this method is suppose to search an array and find whatever you search (numerical or alphabetical)
i have this
public ArrayList<Person> findPerson(String searchFor) {
Main.handleSearchPerson(keyboard, searchFor);
ArrayList<Person> matches = new ArrayList<>();
for (Person p : personList) {
boolean isAMatch = false;
if (p.getFirstName().equalsIgnoreCase(searchFor)) {
isAMatch = true;
[Code] ....
I am not sure if how i am searching the method is correct or how to pass that to another class to put it inside of a switch statement.
View Replies
View Related
Nov 30, 2014
I am currently working on a application for a car park system. It uses GUI created by myself and holds 15 parking spaces. The user has the option to add, delete or search for a specific car. When the user adds a new car, details of the car are entered, the cars registration number (saved as a string), and the user had to check one raido button if the car is expensive, large or normal.
Once this information is entered a new instance of a vehicle will be created and the bay the car is allocated to turns green and stored in one of three arrays(expensive, normal or large). I also have a fourth array for creating the parking spaces.Each array can only hold 5 vehicles apart from the fourth which can hold all 15. When searching for a vehicle the user enters the registration of the vehicle they want to find, but I have to search through all three arrays to find it, and if it does not exist showing a message saying so.These are my three arrays
public static ParkingBay[] regularBays = new ParkingBay[5];
public static ParkingBay[] largeBays = new ParkingBay[5];
public static ParkingBay[] expensiveBays = new ParkingBay[5];
String Registration; - saves registration entered from add form
String RegistrationNumber; - saves registration number from search form
I need to search to see if RegistrationNumber is in either of the three arrays if not show a message saying otherwise
View Replies
View Related
Nov 19, 2014
I have just started working with linked lists. I have a linked list of Objects and I want to be able to search for a specific object. But currently my code continues to return false. Also how would I go about removing the first index of the linked list.
public static void main(String[] args) {
LinkedList<Cookies> ml = new LinkedList<>();
int choice = 0;
while (choice >= 0) {
choice = menu();
[Code] ....
View Replies
View Related
Feb 10, 2015
I have written an algorithm that compares the chars and returns all the found words in a 20x10 puzzle, but not for a 100x50 puzzle.
The puzzles are loaded in via text file and I have confirmed they're loading correctly.
guesses = String[] of search words
puzzle = char[][] of letters
Why it doesn't work on the large array?
Expected output:
word1, word2, word3
Actual output:
[blank line]
Here's the horizontal algorithm (row by row search)
//temp variable
ArrayList<String> temp = new ArrayList<String>();
try {
//for each guess word
for(String word : guesses) {
int count = 0;
[Code] .....
View Replies
View Related
Apr 19, 2013
I just started in java programming and into Arrays multidimensional. I started a simple 2 dimensional array with 5 names with genders. I 'm having issues because it does not want to print out the names with correct genders Male/ Female. The simple program should print out the 5 names with gender type.
example:
Jack - Male
Sally - Female
Dave - Male
Sue - Female
Brian - Male
Here is what I came up with so far:
public class Multiname
{
public static void main(String[] args)
{
//String[][] multiname;
String [][] multiname =
[Code] .....
This is the result:
multiname : Jack
multiname : sally
multiname : Dave
multiname : Brian
multiname : Sue
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at MultiD.main(MultiD.java:29)
View Replies
View Related
Apr 24, 2014
At the moment I am studying it and the problem is Taking a range of names in an array and outputting them alphabetically, But for some reason I get an outOfBoundsException. And its starting to get to me. How to put the code in
import java.util.Scanner;
import java.util.Arrays;
public class lab241 {
public static void main(String[] args) {
Scanner kb= new Scanner(System.in);
String[ ] names = new String[4];
names[0]= ("Horatio");
names[1]= ("Anatoly");
names[3]= ("Evelyn");
names[4]= ("Anna");
Arrays.sort(names);
for(String N: names){
System.out.println(N);
View Replies
View Related
Oct 16, 2014
How do you format an arraylist?
Mine looks like this:
[<?xml version="1.0" encoding="UTF-8" standalone="no"?> <DefEnv id="Dev">, <Envt id="Test">, , <DB id="DM">,
But I want it to look like: I'd prefer if the '[' , '<>' and ',' were not on them also but I'm not too bothered about that bit.
[<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<DefEnv id="Dev">,
<Envt id="Test">, ,
<DB id="DM">, ]
View Replies
View Related
Apr 5, 2014
I am creating a hangman game and I want to read in a list of words from a text file, but after the user inputs the name of the text file. I get 'Exception in thread "main" java.lang.NullPointerException'.
Here is the code, where I think the problems lie.
public void runModel(){
ArrayList<String> pirateWordsList = new ArrayList<String>();
System.out.println("What is the name of the file you would like to load? (The file included is called piratewords.txt'");
Scanner in=new Scanner(System.in);
String file=in.next();
load(file);
[Code] ....
The full error message is this:
Exception in thread "main" java.lang.NullPointerException
at uk.ac.aber.dcs.pirate_hangman.Model.load(Model.jav a:108)
at uk.ac.aber.dcs.pirate_hangman.Model.runModel(Model .java:45)
at uk.ac.aber.dcs.pirate_hangman.Main.main(Main.java: 6)
View Replies
View Related
Feb 2, 2015
I have 3 classes Pet, Cat and Dog classes. Cat and Dog classes are childs of Pet class.
Each pet has the properties of
private String myName;
private int myX;
private int myY;
private int mySpeed;
private int myAge;
I have an array list which creates 100 dogs. How can I print out the dogs with the two highest age values?
View Replies
View Related
Nov 23, 2014
Im trying to create a program in which I read line by line the contents of a text file, and then report each letter along with its frequency. I was wondering how to read through the lines and process it so that my program knows to increase by a number each time a letter appears in my text file. For example, if A appears in my file 5 times, B 3 times, and C 0 times I want to eventually print out
A -- 5
B-- 3
C-- 0
My first thought was to do this using array lists but is there any way I could do this without using one?
View Replies
View Related
Apr 28, 2015
method called []getLetterGrades but the only hint My professor told me was that I needed to declare another array list for this method and he wouldnt tell me anything else so bummer. But I don't understand why if what we are returning is a char. It would make sense to return an array list of char to get letter grade. Which is what i did but since the function is a char, the array list character wont work as a return.Primarily i would like to know the type that is needed. I just want an explanation for an array list in this method and how it would serve in this method.
import java.io.File;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;
[code]....
View Replies
View Related
Jun 29, 2014
Directions: public static void initialize(ArrayList names, ArrayList sores)
You should write a function that sorts both array lists, based on the values in the scores array list. This is one of the more conceptually challenging parts of the assignment. You want to sort the scores array list, and simultaneously alter the names array list so that the names continue to line up with their respective scores by index. In the example data above, when the score 9900 moves to the first element of the scores array list, the name "Kim" should also be moved to the top of the names array list. The function should have the following signature:
I'm having trouble figuring out how to sort the lists.
import java.util.ArrayList;
import java.util.Scanner;
public class Assignment5
{
/**
*/
public static void main(String[]args) {
intializeArrays();
[Code] ....
View Replies
View Related