How To Print Out All Keys Currently Stored In A HashMap

Jul 4, 2014

I want to write a method to print the all the names of a phone book. phoneBook is a HashMap<String, String> , that has names as keys and phone numbers as values.

Reading the documentation for both HashMap and Set, I have more or less an idea of how it could be done but I cant put it on code..
 
public String getName()
{
  String[] keys = phoneBook.keySet().toArray();
return keys[0];
  }

This is wrong. It doesnt compile saying incompatible types. I was thinking first to manage to succeed to get a key from my phone book and then I change it to print all the keys.

View Replies


ADVERTISEMENT

HashMap / How To Get Size Of Value And NOT The Keys

Apr 5, 2014

I am asked in my assignment to make a program that accepts a text file as an example a novel and i have to sort each word as a PERSON or ORGANIZATION or LOCATION or O as in Other , example :

Microsoft/ORGANIZATION ,/O Nelly/PERSON !/O '/O

Now we notice that microsoft is and organitzation and "," is Other and Nelly is a person's name and so on ..

Now I am asked to return the numbers of tags in the text which is 4 in our case because we have (ORGANIZATION,PERSON,LOCATION,OTHER)

My question here is my logic true ? And since i made a map of String,String ; Is there any way that i can get the size of the values in our case the values are the organization etc.. ?

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;

[code].....

View Replies View Related

Getting Keys From Values In Hashmap

Feb 9, 2014

I have one doubt.In HashMap if keys contains 1,2,3,4 and values are a,b,c,d we can get values using get(key) method like 1 will A,2 will return B and so on. Can we get the keys from values like A will get 1 and also if in key if there is a String like 1,2,3,Z and value is A,B,C,7 Z should get me 7. Here I am not using any generics.

View Replies View Related

HashMap Keys To String Array

Dec 21, 2014

I have the following hashMap declared:

private HashMap<String, Car> cars = new HashMap<String, Car>();

How can i get an array of String filled with hashMap keys?

View Replies View Related

HashMap - Return Multiple Keys -OR- Values

Dec 13, 2014

Here is my HashMap and a method for listing all the keys in it

HashMap<String, String> exampleOne = new HashMap<String, String>();
public void allKeys() {
int i;
i =0;
for (String name: exampleOne.keySet())

[Code]....

Now I want to return all values that associated with one key. How do I do this? Or is it possible to other way round? I mean return All keys associated with a value?

View Replies View Related

Compiler Warnings When Using SortedSet To Process HashMap Keys In Order

Feb 16, 2015

My objective here is to process a HashMap's key's in order. I found SortedSet as a way to do it.

The HashMap is like this:

nobelPrizeWinners = new HashMap<String, PrizeWinner[]>();
// 2009:
nobelPrizeWinners.put(new String ("2009 Physics"), new PrizeWinner[] {new PrizeWinner("Charles K.", "Kao"), new PrizeWinner("Willard S.", "Boyle"), new PrizeWinner("George S.", "Smith")});

[Code] ....

This is the method I am trying to write

public void displayAllYearsAndWinners_2()
{
// Creation of the SortedSet
SortedSet sortedSet = new TreeSet();

[Code] ....

However, the compiler gives me a warning of NobelPrizeWinners.java uses unchecked or unsafe operations. Recompile with -Xlint:unchecked for details.

As I said, my objective here is to process them in order. If this compiler warning cannot be resolved, I am open to other methods of accomplishing my objective.

View Replies View Related

How To Print Stored Content When Using ArrayList

Oct 5, 2014

I have a main class AddressBook where I use ArrayList<Contact> addressBook = new ArrayList<>(); to add information such as name and phone numbers in my Contact class. How do I go about accessing all the information (names, and phone numbers) that is stored in my Contact class? What happens when I print it out is it only prints out the last input of name and phone number and not the entire collection of names and phone numbers. How do I print out the contents of ArrayList?

View Replies View Related

Invoke Some Methods In For Loop In Order To Print Some Info Stored In A List

Apr 28, 2014

I am trying to invoke some methods in a for loop in order to print some info stored in a List. But for some reason, compiler pops a message saying "cannot find symbol - method getEmpID(). You are using a symbol here (a name for a variable, method or class) that has not been declared in any visible scope." But I am pretty sure that method getEmpID (as also getName(), getAfm(), and payment() ) have been declared as public.

Note: My List contains objects of different type (SalariedEmployee, HourlyEmployee). I hope this is not the factor causing this problem.

Java Code:

import java.util.*;
abstract class Employee{
private String name = "";
private String afm = "";
private long EmpID;
static long count=0;

[code]....

View Replies View Related

Going Through All The Keys On Keyboard One At A Time

Jul 14, 2014

I am writing a code that tries to figure out the users password by going through every possible key (brute force). Although I think its going to work, it looks EXTREMELY inefficient to me, since its just a huge switch statement for each character -- 94 total, including the shift values. Is there a built in method in the JAVA API that goes through every key or something?

Here is my code for that segment:

public char[] HackingPassword(){
char[] passChars = this.password.toCharArray();//convert the password to char[]
char[] hacking = new char[passChars.length];//make the hacking variable same size as users password
int nextCharValue = 0;//this is used to cycle through the keyboard
//check each letter of hacking to match with password
for(int i = 0; i < passChars.length; i++){

[Code] .....

View Replies View Related

JPA Primary Key From Two Foreign Keys

Oct 22, 2010

I am new with java, eclipse, jpa(eclipselink), postgresql, and trying to make a web application. I have two tables:

bids: id, quantity, price

trades: bidid, askid, quantity, price

bidid and askid columns are foreign keys from bids table (id), and they are the primary key for the trades table.

I created the Entities from the Tables (Bid and Trade class) with eclipse and it generated a TradePK class for the primary key.

Trade.java:

@Entity
@Table(name="trades")
public class Trade implements Serializable {
@EmbeddedId
private TradePK id;

[Code] ....

I understand that this is necessary because the primary key is from two column, but as soon I want to persist a Trade back to the database Eclipselink call the column names twice:

INSERT INTO trades (price, quantity, datetime, BIDID, ASKID, bidid, askid) VALUES (?, ?, ?, ?, ?, ?, ?)

So postgres give me back an exception: ERROR: column "bidid" specified more than once

And I don't know why. According to the JPA docs I didn't found any mistake:

Introduction to EclipseLink JPA (ELUG) - Eclipsepedia

Or I just can't see it... or is there a better doc which explains it better?

View Replies View Related

JSF :: SQL Database - CRUD With Foreign Keys

Aug 12, 2014

I am trying to work with a SQL database and some JSF pages that were created with/for CRUD. The data comes up just fine but all my foreign keys show the primary key for the key not the data. So instead of having the actual Zone I get the number (my primary key) that matches the Zone.

example:
4717A Cool ReceptionTitle: Gatherer of Cool Companycom.vancowboy.lotor_db.LotroZones[ id=11 ]

The last item should read "All" not "com.vancowboy.lotor_db.LotroZones[ id=11 ]"

This was straight from a tutorial so I can learn this but the FK has got me baffled...

View Replies View Related

Hashmaps - Selecting Keys / Values

Apr 22, 2014

I'm learning Java using BlueJ, I have made a class that has a HashMap of (String, String) that contains an the make of a car and the model.

I want a method to return a collection of all the keys that satisfy a condition, like if someone wants to find what make a certain model is. I know it requires a loop, just not too sure how to write the loop to satisfy the condition.

View Replies View Related

How To Identify Up Down Left And Right Keys With Keylistener

Apr 9, 2014

I Want to identify up, down, left and right keys with the keylistener() WHATE NAME DO I IDENTIFY IT WITH

View Replies View Related

To Decrypt Data - File Containing Keys Is Not Being Read

Aug 27, 2014

I am developing a java code using netbeans for encryption decryption by RSA algorithm. Swings and file handling play a major role in these code. My encryption code is working nicely but the code for decryption is not since keys file is not being read. That is why variable mod and pri are getting null values and the following error stack is coming. I know where the problem is somewhere in void readkeys() function but cannot solve it.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.math.BigInteger.modPow(BigInteger.java:1579)
at rsakeydecryption1.RsaKeyDecryption.decryption(RsaKeyDecryption1.java:167)
at rsakeydecryption1.RsaKeyDecryption.read_output(RsaKeyDecryption1.java:294)
at rsakeydecryption1.RsaKeyDecryption.actionPerformed(RsaKeyDecryption1.java:330)

[Code] .....

My code for decryption is

package rsakeydecryption1;
import java.math.BigInteger;
import java.util.Random;
import java.io.*;
import javax.swing.*;

[Code] ....

View Replies View Related

Binary Search Trees Theory - Sequence Of Keys?

Mar 15, 2014

Suppose that a certain BST has keys that are integers between 1 and 10, and we search for 5. Which sequence below cannot be the sequence of keys examined?

(a) 10,9,8,7,6,5
(b) 4,10, 8, 6, 5
(c)1,10,2,9,3,8,4,7,6,5
(d) 2,7,3,8,4,5
(e)1,2,10,4,8,5

I've noticed many differences, such as there is only one with an odd number of keys, and one has all integers from 1-10 inside of it, but I can't find any real reason that you wouldn't be able to search it?

View Replies View Related

How To Make Moving Character By Alternating Arrow Keys

Aug 3, 2014

I've started creating a simple game in Java, so now I'm stuck. I wanted to make moving character by fast alternating arrow keys, but I don't know how. I can make a boolean variable, which disables moving by pressing the same key again, but the player can just hold the key and character is still moving. I thought about changing position of character when the key is pressed, instead of increasing speed, but then the movement wouldn't be smooth. I think this may sound incomprehensible, so I will add a link to game like an example of what I mean.

Play Mini Sports Challenge game online - Y8.COM

View Replies View Related

Deep Clone Of Entities - Set Primary Keys To Null

Nov 21, 2014

I need to get a clone of a fairly deep object tree. All objects are entities and I need to set the primary keys to null. The root object:

public class Grundentscheidung implements Cloneable {
@OneToMany(cascade = CascadeType.ALL)
@JoinColumns({
@JoinColumn(name = "gesamtentscheidung_dstNr"),
@JoinColumn(name = "gesamtentscheidung_id")

[Code] .....

When I call

Grundentscheidung grundentscheidungClone = grundentscheidung.clone();

grundentscheidungClone contains the whole object tree with all dependencies, but the Tatbestand objects have their primary keys. When I use the debugger I see that Tatbestand.clone() is never called.

Is my code faulty? I would like to avoid to write a large method which sets all primary keys on the object tree to null.

View Replies View Related

Swing/AWT/SWT :: Snakes Stopped Moving When Tried Hitting Keys

Mar 28, 2014

I've visited this great site been following this java tutorial on making the snake game, and as I followed along at added the second player. Right up to the point I got into the resetting if I hit borders or myself or the other player, the snakes stopped moving when i tried hitting keys...problem started up to the point where I added snake2 in the GenerateDefaultSnake method

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.LinkedList;
import java.util.Random;

[code]....

View Replies View Related

Java 2D Game - Overcoming Max Of Three Keys At A Time Limitation Of Keyboard

Jan 7, 2014

I'm working on a Java 2D game which requires a max of six keys be held down at the same time.

The game is for two players on the same keyboard, playing simultaneously.

However, all three computers I ran the program on only allow a max of three keys held at a time. They all have trouble with reacting to more than three keys being held.

It seems that pressing a new key after three are already held, either cancels some other key-holding or is ignored.

I've been told that this is a hardware issue. Most keyboards can't handle more than three keys held at a time. But a lot of games do require this, and they do not require special gaming-keyboards to run on my computer without problems.

So there has to be a solution that will make the game playable on any standard keyboard.

(I use Key Bindings).

The game's controls:

Player 1

- Rotate sprite and set angle of movement: LEFT arrow

- Rotate sprite and set angle of movement: RIGHT arrow

- Move forward: UP arrow

- Shoot missile: ENTER key

Player 2

- Rotate sprite and set angle of movement: 'A' key

- Rotate sprite and set angle of movement: 'D' key

- Move forward: 'W' key

- Shoot missile: 'T' key

Relevant code:

The Key Bindings part:

Java Code:

// An action for every key-press.
// Each action sets a flag indicating the key is pressed.
leftAction = new AbstractAction(){
public void actionPerformed(ActionEvent e){
keysPressed1[0] = true;

[Code] .....

This is mostly how reacting to key-presses and key-releases works in my program. When a key is pressed or released, a flag is set. The Board class reades the flags every game-loop cycle and reacts accordingly.

As I said, the program doesn't react correctly to more than 3 keys held at a time, probably because of the keyboard. Is there a way to code a solution?

View Replies View Related

Values In Array Not Stored?

Oct 6, 2014

I just forgot to increment n while trying to store the humidity... I do this every time and I suddenly realize what I did wrong ...

My problem is that after printing humidity[n] in the "Humidity(%)" row, it seems that humidity[n] becomes 0. I checked like this:

System.out.println(humidity[5] + " " + humidity[6]);

In the "Humidity(%)" row, they come out fine, but when I do this, they come out as 0, which I think would explain why my heat indices are consistently lower than the temperature when the temperature is over 80.

My code:

import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class HeatIndex {
public static void main(String[] args) throws IOException {
System.out.printf("%70s", "Heat Index: Key West, Florida");

[Code] ......

View Replies View Related

How To Make Print Employee Button To Print Info Of Other Class

Apr 12, 2014

Basically the class is supposed to have a static variable that keeps track of the number of companies (numberOfCompanies) that have been created and a static method that returns the value of this variable. I need to have a constructor and a method to addEmployee, a method to printCompany, and a toString method. The addEmployee method will check that there is only 1 salesperson, 2 designers, and 4 manufacturing persons at a time and will check that there are no more than 7 employees of the company.

The addEmployee method will return a String indicating what the error is (i.e. "There is already a sales person in this company") or a null if the employee was added. If attempting to name a third company, an error message will be shown. what i have so far but my print company mmethod returns null company class :in which i am obliged to use inputdialog because it says that it cannot return void when i use the showmeassage diaolog.

company class:

import java.util.ArrayList;
import javax.swing.JOptionPane;
public class Company {
private ArrayList<Employees> list ;
private static int numberOfCompanies;

[Code] ....

error message when print button is clicked:

Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: JOptionPane: parentComponent does not have a valid parent
at javax.swing.JOptionPane.createInternalFrame(Unknown Source)
at javax.swing.JOptionPane.showInternalOptionDialog(Unknown Source)
at javax.swing.JOptionPane.showInternalMessageDialog(Unknown Source)

[Code] ......

View Replies View Related

Get The Value Of A Variable Stored In Another Class Of Another Package?

Nov 12, 2014

Source packages

1.First package : CarDealer.java (JFrame form)

2. Second package : Vehicle.java (JFrame form)

- The user runs CarDealer JFrame in which appears a JTexField in which he types in some characters (pretending he types some data about selling licence).
- The user clicks on the JButton "Save" that stores the characters typed in inside a String variable called strLicence.
- The user clicks on the JButton "Open 'Vehicle Form'" that opens the Vehicle JFrame.
- Finally, in the Vehicle JFrame I want to write a code that can see/use the value of the String variable called strLicence.

In other words, for example, I want to store the value of strLicence in a new variable declared in the Vehicle class.

(Actually I'm working with a long and elaborate project for the dissertation of my bachelor's degree, so, for cutting to the chase, I simplified it by creating an essential JFrame project.)

I used netbeans

1. CarDealer.java

package Firstpackage;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFrame;

[code]....

I also attach the netbeans project in a zip file ....

View Replies View Related

JSP :: How To Call Stored Procedure Through Code

May 29, 2014

I have a .csv file ,and I have written a jsp code which uploads csv file to a local folder . Now I want to upload that csv file to database using stored procedure. Below I have included the code which uploads the csv to database.

<%@page import="org.apache.commons.fileupload.*,java.util.*,java.io.*"%>
<%@page import="java.sql.*"%>
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<img border="1" src="img/tss.JPG" alt="portal" />
<br><br><br><br><br><br>
<%
try{

[Code]....

View Replies View Related

Java Object Stored In Array?

Apr 8, 2014

I have been having trouble with a recent hw assignment requiring us to sort user defined data types in an array. so far, i have implemented the sorting algorithms correctly but i cant seem to store a string and an integer together. I have an employee class

[public class employee {
private String Name;
private int IdNumber;
public employee(int IdNumber,String Name){
this.Name=Name;
this.IdNumber=IdNumber;}

[Code] .....

And in my app, i create a employee object-----> employee john=new employee(1020, "John");

How can I prompt my insert method to take in employee id and string name together?

View Replies View Related

Where Are All Keywords Like Final And This Stored In Java

Jan 23, 2014

Which package or class i can find all the predefined keywords in java like "this" etc...

actually i want to know if this operator is itself final in nature or not from its syntax?

View Replies View Related

Servlets :: Why Objects Stored In Session Should Be Serializable

Feb 5, 2015

Why is it that objects stored in session object should be serializable?

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved