How To Store A File In Hashmap Or 2D Array

Sep 25, 2014

I have a file called statecapitals.txt that is read in, I want to store it in either a 2d array or hashmap and select a random state then Ask the user for the name of the capital. Then I want to Let them know if they are correct or not and have a choice to play as many times as they like. When they no longer want to play,I want to let them know how many they got correct and how many incorrect. I am not sure which would be better a hash map or 2d array and dont know where to start with each.

here is what the text file looks like:

Alabama - Montgomery
Alaska - Juneau
Arizona - Phoenix
Arkansas - Little Rock
California - Sacramento
Colorado - Denver

[code]....

View Replies


ADVERTISEMENT

How To Store Three Variables In HashMap

Feb 10, 2014

So I just want to store a Key in a HashMap which can related to two values. For example, the Key "ABC" related to "Fire" which in turn relates to "Heat".

How can I code this in a HashMap?

View Replies View Related

Parsing Files From Directories And Store Them In Hashmap

Jul 15, 2014

I have a query regarding parsing a directory, its subdirectories and files of directories. i am using File Object to load absolutepath of main directory and checking file is a directory or file but not geeting exact solution what i want. Suppose directory structure is D:TestPC

PC is a directory and have 2 files test.txt test1.txt and one directory
PC1 directory contains 2 directories PC2 ,PC3 and each PC2 and PC3 have some files.

Now my query is : i want to store each directory in HashMap and its files corresponding to its directory. E.g.:

D:TestPC ---> D:TestPC est1.txt , D:TestPC est2.txt
D:TestPCPC1---> Null
D:TestPCPC1PC2 --->D:TestPCPC1PC2 est1.txt, D:TestPCPC1PC2 est2.txt

like this, where first directory path as key and files are its values.

View Replies View Related

Count Items In Textfile And Store Result In Hashmap

Nov 29, 2014

I have a textfile that contient the name of items separated by blank space

Header 1Header 2Header 3Header 4Header 5javaoraclesqlphpjavasql phpphporaclejava

First i want to read the text file and I want to count the number of occurrence of each item and after that i want store the result in hashmap structure...

Example :

Header 1Header 2java3oracle2sql2PHP3

How can I do that ?

View Replies View Related

Open A File And Store Contents Into A Two Dimensional Array

Feb 11, 2014

So I am trying to open a file and store the contents into a two dimensional array. I seem to have no problem using this same basic code to read the file and output it. But when I try to change it so that all the data is stored into the array, it does not work.

Java Code:

public void convertFile()
{
String filePath = ("C:UsersBradDownloadsFB1.csv");
String [][] rowCol = new String [429][6];
try
{
BufferedReader br = new BufferedReader(new FileReader(filePath));
StringTokenizer st = null;
System.out.println("Your file is being converted to an array. This may take several minutes.");

[code]....

View Replies View Related

Store Data From A File Into Array Of Type Product?

Mar 17, 2015

public class InputFileData {
/**
* @param inputFile a file giving the data for an electronic
* equipment supplier’s product range
* @return an array of product details
* @throws IOException
*/
public static Product [] readProductDataFile(File inputFile)
throws IOException{
// YOUR CODE HERE
}

This code is meant to be used to read a text file and store the data in an array of type Product[]. I know how to read in a text file and have it sort it into an array, but I've never seen code laid out in this fashion before (specifically "public static Product[]", and I'm unsure how to work with "(File inputfile)". I've looked all over the place but can't find any examples of anything like this.

Also, the code given cannot be changed, as it's that code I have to work with.

I still don't understand how to use it as a whole. For example, do I read the file in the main and have this method read that in and output to the Product class? Do I read the file in this method? I can't work out how to make this work when I have to use this method.

View Replies View Related

Get Data From Config File And Store In Array Without Using Split Method

May 8, 2014

How i can get the data from a file, for a key that have multiple values without using split method and i want to store that data in an array

file name:"new.txt"[contains below data]
Name=google.com, yahoo.com,facebook.com
Attachment=abc1,abc2,abc3

I am successfully able to do this using following code

Properties props = new Properties();
String configFilePath = (Cjava_confignew.txt);
props.load(new FileReader(configFilePath));
String TestName = props.getProperty(Name);
String test[] = TestName.split(,);

Now i just want to store the values for key "Name" in array without using TestName.split(,); but i am not able to do this.

I have also used .yml file to avoid this but did not succeed, some way to do this

file name test.yml

Name
-google.com
-yahoo.com
-facebook.com

Attachment
-abc1
-abc2
-abc3

i want to store multiple values for single key(Name) within the array

View Replies View Related

Read Characters From Txt File / Store Them Into 2D Array And Print Information

Oct 10, 2014

The point of this program is to read characters from a txt file and store them into a 2D array. After this has been accomplished, the information is to be printed in the same manner it is read from in the txt file.

Here is the code I have so far:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main
{
public static void main(String[] args) throws FileNotFoundException

[Code] ....

And this is the error I am receiving when trying to accomplish the goal of the project:

Exception in thread "main" java.lang.NumberFormatException: For input string: "############"
at java.lang.NumberFormatException.forInputString(Unk nown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at Maze.<init>(Maze.java:15)
at Main.main(Main.java:20)

What's going on here and how I can correct this?? The information I am trying to store in a 2D array and then print is this:

############
#.#........#
#.#.######.#
#.#....#...#
#.###.*#.#.#
#...####.#.#
#.#.#..#.#.#
#.#.#.##.#.#
#o#......#.#

View Replies View Related

Program Should Read File Contents And Store Information In Array Of Golfers

Dec 3, 2014

The main method will drive your program by doing the following:

-Create an array to hold all the individual golfers and par scores (type is Golfer[ ]).
-Prompt the user for a data file containing the Par scores and the player names and score. The format of the input file should look like

Par, 3, 4, 4, 3, 4, 3, 5, 3, 4
George, 3, 3, 4, 3, 4, 2, 3, 3, 4
Paul, 4, 5, 4, 3, 6, 3, 4, 3, 5
Ringo, 3, 3, 4, 3, 4, 2, 4, 3, 4
John, 4, 4, 4, 3, 4, 2, 5, 3, 4

Your program should read the file contents and store the information in your array of Golfers.

View Replies View Related

Generate 10 Random Integers / Store In Array And Then Calling A Method To Display Array

Nov 8, 2014

So I need to generate 10 random integers in the range 1-20 but i have to store them in an array called numbers. Then I have to call a method called displayArray which displays the contents of the array and for the assignment i have to use a for loop to traverse the array. The method header for the displayArray method is:

public static void displayArray(int[] array)

This is what I have done
 
public class RandomIntegers {
 static int numbers = 0;
public static void displayArray(int[] array) {
System.out.println(numbers + "Numbers Generated");

[Code] .....

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

Putting Values From 2D Array To HashMap

Apr 9, 2014

I have a 2D array and the elements are listed as follows:

outlook temperature humidity windy gooutside
sunny hot high false n
overcast hot high false y
....

I need to put these values into a HashMap, where the elements of the first row are the keys and the elements from row 1 to n-1 are the values. What would be the best way to make sure the key and values are matched correctly?

Here is what I have:

String[][] array = new String[numberOfRows][numberOfCols];
HashMap<String, String> map = new HashMap<String, String>();
for(int rows = 0; rows < (numberOfRows * numberOfCols); rows++) {
for(int cols = 0; cols < array[i].length; cols++} {
map.put(array[0][cols], array[rows*cols][col];
}
}

I keep getting the out of bounds error.

View Replies View Related

Creating A System That Will Ask User To Create A File That Will Store To Text File

Mar 9, 2015

I am new to java and I am creating a system that will ask the user to create a file that will store to a text file, Once the user created the file I have a class that will let the user input the subject name that has been created, However, I keep on getting this java.util.nosuchelementexception.Here's my code:

public void display_by_name()
{
String id, name,total;
String key[]=new String[30];
String value[]=new String[30];
int i=0;

[code]....

View Replies View Related

Read File Into Hashmap ArrayIndexOutOfBounds Exception

Jan 8, 2014

I'm trying to get a file read into a HashMap and I keep getting ArrayIndexOutOfBounds Exception. This is a part of the file I'm reading in:

a あ
e え
i い
o お
u う

Java is unicode based. I'm new to using HashMaps and I/O HashMap<String, String> hm = new HashMap <String, String>();

try {
in = new BufferedReader(new FileReader("hirigana.txt"));
}
catch(Exception e) {
System.out.println("failed to find hirigana");

[code]....

View Replies View Related

How To Read From Text File Using HashMap For Category And Descriptors

Oct 28, 2014

I have a TxT file with the next content:

[Cars]
BMW=3
Lamborghini=10
[Computers]
Mouse=5
Keyboard=12

How can i read the file and output in

System.out.println("Category " + descriptor + " Value: " + )

The output console should look like :

Cars : Lamborghini 10
BMW 3
Computers: Mouse 5
Keyboard 12

And the Value MUST be readed from thext file for each descriptor.

package test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.Map;
public class Descriptor {
private final String descriptor;

[Code] ....

View Replies View Related

How To Store File Properties In A File For Notepad

Dec 4, 2014

I'm not too familiar with serialization.It actually I found at this site (Java - Serialization) says that it will write and read properties of an object. I'm not quite sure how this would work as I would have thought that writing it to a text file would just do a toString() when you wrote it, but the page seems to think otherwise, and also names it .ser or something instead of .txt.

I thought, maybe this would work with my idea for a notepad that could store a Font, font color, and other settings, for the file. It would be beyond normal notepads. However, could you do this for a file as it would store text plus other things.

The upshot of this is that the same file, I was hoping to be able to be read by regular text programs like Notepad. However, could Notepad read such a modified file? Also, if notepad wrote to it, would it destroy my font properties and stuff that I'd add to it?

Also, what does this mean:

Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeans™ has been added to the java.beans package. Please see XMLEncoder.

Originally, I was going to use FileAttribute or something like that but it looked hard to do and likely not what I wanted. However, this looks more like what I want, but I'd like to know. This could be a huge breakthrough for a lot of things I was hoping to do but never could think of an efficient way to do.

Update:This serializable thing works great for my text editor but it won't work too hot if another program like Notepad reads it as it will appear like gibberish, including the text.

I had thought of having it make two files, one regular, and one that was serialized, to deal with this, but this doesn't work if I try opening a file that another text editor created that might not have this other serialized version of the file.

Also, I haven't yet tried extending File itself yet. However, I still don't see how it would work the way I'd hoped.

In a way, I want it to be like a RTF with some things used to set te font of the text area when loaded but be able to deal with opening a file made by another text editor and also have another text editor be able to read it even if my text editor created it.

View Replies View Related

File Handling Not Store Bytes Into File

Apr 14, 2015

when open file "rahul.txt"so it shows characters which i stored not byte code i want to stored in file byte code. Now i have a problem for file handling it is not store bytes into myfile. it stores character not bytes.

package com.fileh;
import java .io.*
public class Writedata {
public static void main(String args[])
{
try{
FileOutputStream fout= new FileOutputStream("rahul.txt");
String s="my name is rahul";
byte[] b =s.getBytes();
fout.write(b);
fout.close();

[code]....

and second problem is that when i use FileInputStream to read file rahul.txt so it did not read .

View Replies View Related

How To Store 2 Related Elements Using 2x2 Array

Jul 16, 2014

I have the simple table below:

currency amount
€ 2.0
$ 4.0
£ 5.0

How could I store the currency and amount in an array? A 2x2 array would do this but how to store them and retrieve them is the challenge. For example, I have a method that asks the user for two inputs, the currency and the amount and using the array as a chat table where I could map the currency to the the currency input entered by the user, I could do some calculations with the amount entered by the user. how I could represent the 2x2 array?

View Replies View Related

Program That Use Array To Store 10 Numbers

Oct 16, 2014

i'm trying to write a program that uses an array to store 10 numbers. The numbers should be randomly generated ( Math.random() ), and they should be between 1 and 100 ( 1 and 100 inclusive ). The program should produce an output like the one below:

Element 1 = 23 ( Odd )
Element 2 = 15 ( Odd )
Element 3 = 32 ( Even )
Element 4 - 10 ( Even )
Element 5 - 99 ( Odd )
Element 6 - 1 ( Odd )

[Code]...

I have written code for this but its only showing me 0's after first number can you check whats wrong with my code. my code is.

class even/odd{
public static void main(String[]args){
int y=0;
int z=0;
int[] array= new int[11];
for(int x=1; x <array.length ; x++){
array[x]= (int) (Math.random()* 100);
 
[Code]...

View Replies View Related

Store Numbers In Array Then Sort

Feb 19, 2014

Code below. I am not sure if my logic is correct. I want to prompt a user to enter registration numbers from 100 to 1000, store the numbers in an array then sort them.

import java.util.Scanner;
class regnumber
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("registration number ", 100,1000);

[Code] ....

View Replies View Related

Read ASCII Art Map And Store Information In 2D Array

Feb 12, 2014

I've written some code to do this, but it's not working as I expect. This is what the ASCII map looks like:

###################
#.................#
#......G........E.#
#.................#
#..E..............#
#..........G......#
#.................#
#.................#
###################

Here's how I've tried to store the information in a 2D array:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
 public class Map {
 public void importMap() throws IOException{
BufferedReader br = new BufferedReader(new FileReader("map.txt"));
String line;

[Code] ....

But it's not printing out what I'm expecting. It's printing out:

[[#, #, #, #, #, #, #, #, #], [#, #, #, #, #, #, #, #, #], [#, #, #, #, #, #, #, #, #], [#, #, #, #, #, #, #, #, #], [#, #, #, #, #, #, #, #, #], [#, #, #, #, #, #, #, #, #], [#, #, #, #, #, #, #, #, #], [#, #, #, #, #, #, #, #, #], [

What is printed is much larger than what I've copy and pasted but I can't post it all here.

The error I get is:

Exception in thread "main" java.io.IOException: Stream closed
at java.io.BufferedReader.ensureOpen(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at Map.importMap(Map.java:26)
at Map.main(Map.java:44)

View Replies View Related

How To Get Pixel Values Of Image And Store It Into Array

Nov 13, 2014

So i wanted to try something new like find an image within an image. So for my "find" method I would like to take an image and use it to scan and compare sum of absolute differences with the bigger image. So that the smallest SAD would be the exact image that I am using to scan. What I am thinking is to put each pixel value of both images into two separate arrays and compare them via Math.abs(image1[i][j]-image2[i][j]); . My only problem is that I do not know how to put each pixel value into an array.

Also, If I only want to compare just the green in the picture. I saw that the Pixel class has a getGreen(); method. If I want to find the SAD of the green, would Math.abs(image1.getGreen()-image2.getGreen()); work? I was planning to have 2 nested loops running through each column and row for each image and just find the SAD of the green value.

View Replies View Related

How To Copy 5x5 Array Into 6x6 And Store New Value To Take Up Sixth Position

May 5, 2015

I want to take my 5x5 array that reads data from a file, add the rows and columns, then print out a new 6x6 array with those values. Here is my code that shows I understand everything except this.

import java.io.*;
import java.util.*;
public class prog470cAddingRandC
{
public static void main (String [] args)
{
//read the file
Scanner inFile=null;
 
[Code] ....

I was told in a previous question to make the starting array 6x6 but that is not working. How do I take sum1 and sum2 and store that along with the values of the old array and print it?

View Replies View Related

Create Country Object And Store In Array

Oct 12, 2014

Write a program that prompts the user for information about some countries, creates an object for each country, and then stores the objects in an array. After the user has entered information about all the countries, your program should print out which countries in the list have the smallest and largest area and population density. Assume the user will enter information about at least one country but that the program will not have to store more than ten countries in the array. The user will indicate that they are done entering countries by typing "DONE" for a country name. Here is an example of what your program must look like when it is executed (user input is shown bold)

Please enter the name of a country: United States
Enter the area in square km and population of United States: 9827000 310000000
Please enter the name of a country: Mexico
Enter the area in square km and population of Mexico: 1973000 122300000
Please enter the name of a country: Canada
Enter the area in square km and population of Canada: 9985000 35160000
Please enter the name of a country: Liberia
Enter the area in square km and population of Liberia: 111370 4294000
Please enter the name of a country: DONE

Liberia has the smallest area at 111370 square km.
Canada has the largest area at 9985000 square km.
Canada has the smallest population density at 3.521282 people per square km.
Mexico has the largest population density at 61.986822 people per square km.

I don't know, I'm really confused with this Java program. I don't know how to do the main part of the program where the user inputs, array, and the output.

// Access the Scanner and ArrayList class by importing the java.util package.
import java.util.Scanner;

/** Project - A Country Object
* The purpose of this program is for the user to enter some information at most 10 countries. After the user has entered information about all the countries, the program should print out which countries in the list have the smallest and largest area and population density.
*/

public class Country
{
private String countryName;
private int theArea;
private int thePopulation;
public Country(String countryName, int thePopulation, int theArea)

[Code] ....

View Replies View Related

Fibonacci Application - Store Its Sequence In Array

Mar 12, 2014

Modify the Improved Fibonacci application to store its sequence in an array. Do this by creating a new class to hold both the value and a boolean value that says whether the value is even, and then having an array of object references to objects of that class.

Did I just need to declaring the variable in other class (for boolean value and the value itself) or else ?

Here is the code for ImprovedFibonacci.java

Java Code:

class ImprovedFibonacci {
static final int MAX_INDEX = 9;
/**
* Print out the first few Fibonacci numbers,
* marking evens with a '*'
*/
public static void main(String[] args) {
int lo = 1;
int hi = 1;
String mark;

[Code] ....

View Replies View Related

All Log In App2 Always Store In App 1 Log File

Nov 7, 2014

i have two applications running in Glassfish 2, both are using log4j and they have separated log.propeties. The problem is all the log in App2 always store in App1's log file.Here is how i init to load log.properties file.

try {
Properties p = new Properties();
p.load(new FileInputStream("C:log.properties1"));
org.apache.log4j.PropertyConfigurator.configure(p) ;
} catch(Exception e) {
e.printStackTrace();
}

What does PropertyConfigurator do?

View Replies View Related







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