HashMap - Searching Library Program By Book Index
Feb 9, 2014
I'm trying to search a library program by book index and running into some trouble with my HashMap. Here's the code in question:
Java Code:
public String searchByBookIndex(int input) {
NumberFormat fmt = NumberFormat.getCurrencyInstance();
Map<Integer, Book> bookMap = new HashMap<>();
for (int i = 0; i < author.booksByAuthor.size(); i++) {
bookMap.put(author.booksByAuthor.get(i).getBookIndex(), book);
[Code] ....
The search itself works fine; however, the problem is that the values of everything in the HashMap are overwritten by the last book entry. For example, let's say there are four books (index in parenthesis):
The Book (1), A Wonderful Book (2), A Great Book (3), A Bad Book (4).
Once the code above iterates, the HashMap out is this (via println):
1 = A Bad Book, 2 = A Bad Book, 3 = A Bad Book, 4 = A Bad Book.
What's causing this?
View Replies
ADVERTISEMENT
Jul 16, 2014
My perfect idea is to have a Hashmap that would increase size each Key & value added, it's almost like a self-increasing array. I mean I could just create a very large Hashmap for me to add objects to anytime I want with a key to be able to find the object. Is this the most efficient way, because I'm trying to make my Object Library compatible with any amount of Objects, however, I know there's a limit to how many values you can have in an array, but it's larger than I'll ever need.
Let's say I'm making a number storage program, and I may need from 3 to 8 numbers to be stored with a key to find them easily using a Hashmap, rarely I may need below 3 or above 8. So is it efficient for me to create a Hashmap that has 10 placeholders? Or can I make this more efficient. Note, my Library is static.
View Replies
View Related
Apr 6, 2014
I am working on the Kevin Bacon - 6 degrees of bacon problem. I want to take my Array List and use the index and Values that I have saved in it as the Key and Value of a Hashmap. Below is my code.
HashMap<Integer, ArrayList<String>> actors =
new HashMap<Integer, ArrayList<String>>();
ArrayList array = new ArrayList();
try (BufferedReader br = new BufferedReader(
new FileReader("actors.txt"));)
[code]...
saving each one in it's own spot. I want to use the index of each one as the key to find it in the Hashmap....
View Replies
View Related
Sep 30, 2014
I am creating an apointment application. This programm will have the ability to book apointments for a number of different services, such as doctors, hairdressers, teachers etc for a number of cities.
These information will be placed on a combo box and ideally i would like to provide only the options available acording to the choices made so far.
So, for example we have
A Pediatric in London
A Vet in London
A Sergeon in Berlin
A Barber in Berlin
A Heardresser in Rome
A Programmer teacher in London
An English teacher in Rome
My thinking is that the user choses the city, for example London, then there will be a type of service combo box. In this case (of London) it will hold Doctors and Teachers. If Doctors is chosen, the next field will hold the available Medical specialty, i.e. Pediatrics and Vets and according to that choice the apropriate doctor will pop up.
My problem is not how to programm that. i havent got there yet.
My problem is how to design it! I am thinking that each individual will be an object. each having the same fields, city, type of service etc. But in such a way i am not sure how i can manage to get all the appropriate information in order to poppulate the combo boxes.
I would have to search all the objects for each combo box? From a rough testing i did, i think i could easily get all the individuals that resign in London, but that would give me two records for the doctors(for example).
View Replies
View Related
Jan 29, 2014
I have a custom library I made to make things easier for myself. I used it in a small program in NetBeans and it works fine. When I try to clean and build, it says it can't find the methods from my Library class. How do I get the library packaged into the jar?
View Replies
View Related
Mar 30, 2015
I am reading the excellent book Algorithms. The author of this book is using his own libraries.
I have downloaded the libraries (it is a file called stdlib.jar) and I've store it in a directory called ~/Downloads(I am using a macbook pro).
Then I have created a project with IntelliJ Idea 14.1 using the default package (as it is the only way for a program to 'see' these libraries).
The program is running inside the ide, but because of the nature of this book, all the included code must be run from the command line.
The problem is that I cannot run for example the Average class from the command line because the system asks for the external library.
Specifically the error is :
I enter : java -cp . Average
and the Error I get is :
Exception in thread "main" java.lang.NoClassDefFoundError: StdIn
at Average.main(Average.java:11)
Caused by: java.lang.ClassNotFoundException: StdIn
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
[Code] ....
The Average class is the following :
public class Average {
public static void main(String[] args) {
//Average the numbers on StdIn
double sum = 0.0;
int cnt = 0;
[Code] .....
View Replies
View Related
Jun 7, 2014
i am making a program which uses porter stemmer library for stemming words. I have added/included a jar file "porter-stemmer-1.4-sources.jar" to my project, I am facing an error that "package org.tartarus.martin does not exists" , i am using net beans IDE.
View Replies
View Related
Dec 26, 2014
While making my holiday program and trying to use the Jansi library to add color to the console text,
I got this output:
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.RuntimeException: Uncompilable source code - package org.fusesource.jansi does not exist
at Main.<clinit>
View Replies
View Related
Sep 5, 2014
My Computer Programming teacher has given the class a problem that requires the use of var.charAt(), var.length() and while/for. Basically, the problem is that we have to create program that'll show a mirrored or reverse version of the entered word. Like for example, if your input is the word "Hello" (the quotation marks aren't included), the output will be "olleH".
Here's the code:
import java.io.*;
public class StringMirror
{public static void main (String [] args) throws IOException
{BufferedReader scan = new BufferedReader (new InputStreamReader(System.in));
String enteredWord = "";
int lengthOfTheWord = 0;
int lengthOfTheWordMinusOneVersion = 0;
[code]....
It is working, but the problem is that after the output, there's this error that says String index out of range: -1.
the program is working but I kind of wanted it to have no errors in the end.
View Replies
View Related
Aug 25, 2014
I need to write a java program to find the index of the element whose value is the sum of the remaining elements. Recently I have been asked this question in an Interview which I couldnt solve properly.
View Replies
View Related
Apr 14, 2015
I have this program where I'm supposed to fill an array with 1000 indices with 1000 randomly generated numbers between 1 and 1000. The program is supposed to check if any of the numbers match an index that is the same value (so for example, the number 4 is in index 4). How to check for that condition, especially using a binary search (I'm also told to use a binary search).
Right now the variable index isn't initialized because I don't know what to initialize it to exactly. How do I check to see if any numbers match the value of the same index?
import java.util.*;
public class Identity {
public static void main(String[] args) {
int [] integers = new int [1000];
// Fill array with randomly generated numbers
int [] display = GenerateRandom(integers);
[Code] ....
View Replies
View Related
Feb 2, 2015
I am trying to build an address book, and I started from what i know how to do.
public class Persona {
String nome;
String indirizzo;
int cellulare;
public Persona(String nom, String ind, int cel) {
nome = nom;
indirizzo = ind;
cellulare = cel;
[code].....
Now I would like the program to take a string I insert on command line (for example java Cerca"Robert Baratheon") and compare it to the nome field of every Persona in the address book.
View Replies
View Related
Jan 12, 2014
I am designing an address book. I have come up with the following design:
Address Book:
Attributes : Entry
Methods: add(), delete(), search()
Entry:
Attributes : Name, Address, Phone Number
Methods: addName(),editName(),deleteName(),addPhone(),editPhone(),deletePhone(),addAddress(),editAddress(),deleteAddress()
Name:
Attributes: firstName, lastName
PhoneNumber:
Attributes:areaCode, phoneNumber
Address:
Attributes:streetNumber,streetName,city,zip.
View Replies
View Related
Aug 22, 2014
I found out what my next java class project is, and i am trying to get a head start on it. It is a simple address book. it needs to have 4 choices that get cycled through until quit is chosen:
1. add business contact
2. add personal contact
3. display contacts
4. quit
I have made an abstract contact class with a personal subclass and business subclass(all 3 are required). they have all been made, with set/get methods(the wonders of encapsulation and polymorphism have finally hit home!). The main program needs to add a new subclass object when chosen, and it needs to accept and store contacts by type
So, i am figuring 2 object arraylists for business/personal.
My question is: how do you create an object without having a name for it already programmed? Does it even need to have a unique name, since it would be stored in a seperate index and/or arraylist?
Contact contact = new Contact();
this.firstname = "Be";
this.secondname ="Ginner";
this.phonenumber = "921-456-0102";
personalArray.add (contact);
Then cycle through, changing the properties, and add again to personalArray?
View Replies
View Related
Jul 5, 2014
I have a question about arraylist objects.... I want to create an address book that will contain a persons information (name, phone, address, email..etc..). The first two snippets of code are from the same class ContactArrayList and the last one is from the class Contact. Do I need to create a separate arraylist for each data type? Is it possible to use a single arraylist to contain all the object (person) information and then be searched for using a GUI? I would also like to be able to access this data to be manipulated using a JFrame (add, edit, and search).
public class ContactArrayList<E> implements List<E> {
public int size, capacity;
E contact;
private E[] contacts;
public ContactArrayList() {
size = 0;
capacity = 20;
[Code] ....
View Replies
View Related
Jun 22, 2014
I have a program for a phone directory. It needs to add, delete, append, and edit telephone records. I've gotten it to write and search the files. Just need to get it to delete and edit them.
Here's what I have so far:
import java.nio.file.*;
import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.ByteBuffer;
import static java.nio.file.StandardOpenOption.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
[code]....
View Replies
View Related
Nov 25, 2014
Why my borrowBook, returnBook, getBookIndex, remove book and printAuthorBook methods wont work. They all have a parameter that is a string and Im guessing its a common problem in all the methods. I think all my code for Book class is right. Iv attached the two classes. I also attached the test library which tests the two classes.
public class Book
{
// The fields:
private String author;
private String title;
public int pages;
public String refNumber;
public int numBorrowings;
boolean available;
// add more fields here
[code]....
View Replies
View Related
Oct 19, 2014
So for the last few weeks I have been working on a Java project for school. I see the light at the end of the tunnel, but I have run into some speed bumps with my main class. It was working fine, but I was pulling an all night and I deleted some of my code by mistake.Now I am not seeing what is exactly wrong. Anyways, the project was to create an address book complete with a menu system.
Here are all of my classes:
import java.util.ArrayList;
/**
* A model of an address book which keeps track of contacts.
* An address book object should keep track of the concept of a current contact (think index).
* You must use an ArrayList for your implementation.
*/
public class AddressBook {
Contact contact = new Contact();
[code]....
View Replies
View Related
Aug 9, 2014
I'm working on a problem where I have to use the FileInputStream to opens a file that contains the name of a user's favorite book and then displays it.
If the file doesn't exist, then I prompt for the book title and write it to the file using FileOutputStream. This is what I have so far but I'm lost because if I put a file that exists in line 8 then it closes, but if I don't and the user puts their favorite book nothing happens. Or, as far as I can tell nothing does.
import java.io.*;
class DisplayBook {
public static void main(String[] args)
[code]....
View Replies
View Related
Feb 22, 2014
Sample XML:
XML Code:
<Books>
<Book id="1" Name="C#">
<URL>http://localhost/download/M1.xml</URL>
</Book>
<Book id="2" Name="Oracle">
<URL>http://localhost/download/M2.xml</URL>
</Book>
<Book id="3" Name="Archius">
<URL>http://localhost/download/M3.xml</URL>
<URL>http://localhost/download/M4.xml</URL>
</Book>
</Books> mh_sh_highlight_all('xml');
I have to fetch only the book name attribute and use it for display .However say for example ,I have got the list of Books ie(C#,Oracle,Archius) ,passing this value to a method should return me an array of URL nodes value within it.
The idea is to display the book names in a list and then on user selection , corresponding URL needs to be fetched.
Was able to get the loop for traversing the nodes
[java=code]
NodeList nodeList = document.getDocumentElement().getElementsByTagName ("Book");
for (int i = 0; i < nodeList.getLength(); i++) {...
[java=code]
So
a) Display the BookNames
b) Get the corresponding URL's for the particular book if I pass the Bookname
View Replies
View Related
Nov 30, 2014
Applications: Netbeans (8.0.1), Apache Tomcat
My Aim: Creation of a telephone book
How should it work:
I have a Database with Data of people (name, adress, phone number).
If a user enters a phone number (CLIENT) -> the correct person with the name and adress should appear.
I build up the infrastructure and now I'm at the point that I have to create a Webmethod for it. I just worked on projects like addition of two integers, hello "name", ... I think this example now is harder, BUT i can learn a lot!
View Replies
View Related
Jan 29, 2014
If you were given a problem to search for an error in a section of code but it is incredibly long, what are some ways you can go through the lines of code quickly?
View Replies
View Related
Mar 23, 2014
Okay, I will be as succinct as possible. I am writing a rudimentary book store program for homework. The program consists of two class files and a main. The main issue I am having is on getting the search portion of my program to work. I will post as little as I can and still make sense. This is the block of code belongs to my public Book getBook() method, which is of the BookStore class.
View Replies
View Related
Jan 2, 2015
I have a code that is meant to read a file and organize all the names from least to greatest salaries. It also allows the user to enter a name to find from the file, and the program finds the name and displays it. I have two errors, and I will show the error in my code
View Replies
View Related
Nov 3, 2014
I'm doing LinkedList at the moment and I'm having a bit of trouble with my assignment. The part I'm struggling with is remove an employee from a training course (as specified by their employee number),
what I'm confused about is iterating through the linked list to find the employee we're looking for. What I would do in this situation if I was using an array list is
for(Employee emp : myList) {
if(emp.getEmployeeNumber().equals(searchedNumber)) {
remove from training course..
break;
}
}
"Can only iterate over an array or an instance of java.lang.Iterable" is what it is telling me, and I can't figure out why/how its done differently for linked lists.
View Replies
View Related
Mar 25, 2015
I'm very new to Java, and I'm writing a code to search a string to see how many times the word "dog" is found in it. I'm not sure if this is error-free or the most efficient, but I'd like to keep it simple.
public void run()
{
String input = new String("The Dogman was no ordinary dog, nor man, but rather a peculiar dog-like man who barked like a dog, and panted like a dog, he even ate like a dog. He owned a dog named Doglips, and interestingly enough, his favorite food was hotdogs.");
println(input);
int index = -1;
int count = 0;
print("Counting dogs:");
inputarray = input.split(" ");
[Code] .....
View Replies
View Related