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


ADVERTISEMENT

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

Error In Junit ArrayIndexOutOfBounds - Cannot Seem To Find Source

May 12, 2015

I get an error "ArrayIndexOutOfBounds" in my Junit test on the following method:

public String[] getMACs(String ssid) {
int first = bs.searchFirst(arrayWlanMac, new GetSet(null, ssid, 0, 0),
new ComparatorSSID());
int last = bs.searchLast(arrayWlanSsid, new GetSet(null, ssid, 0, 0),

[Code] ....

The error occurs in the first while loop right in the if condition (if (!arrayWlanSsid[i + 1].getMac().equalsIgnoreCase(arrayWlanSsid[i].getMac())) )

View Replies View Related

How To Read Files In Eclipse - Getting FileNotFound Exception

Jan 7, 2015

I have a Dynamic Web Project , and in one of the Class method I am reading an xml file. The problem is until now I am giving the absolute path of file on my system

File quizFile=new File("C:UsersMahtabQuizzesquiz1.xml");

I understand its bad way , so I want to put these xml files in my eclipse project itself.

So I created a new folder called quizzes under src and put all those xml files in quizzes folder.

I am trying to read the file , I tried

File quizFile=new File("quizzesquiz1.xml");
File quizFile=new File("quizzesquiz1.xml");

But getting FileNotFondException , how can I refer to these files.What path should I point to.

View Replies View Related

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 View Related

Read Text File Into Object Array And Creating Random Access File

Dec 8, 2014

I am working on a project that requires me to build a database with random access file, representing products, the base product contains a name (about 30 characters), a price (double), and a quantity (integer). I have worked on this project for probably 15+ hours and have tried so many things and feel like I've barley made any progress...

The part i am really struggling with is taking the data from the text file and creating an object array with it using the product class. Once ive accomplished that, i have to use that data to create a random access file with the data.

Here is the base Product class that must be used to create the objects for the array.

public class Product
{
public String pName;
public String stringName;
public double price;
public int quanity;

[Code] .....

And then here is the data from the text file that i must extract to use to create product objects.

Dill Seed,938,34
Mustard Seed,100,64
Coriander Powder,924,18
Turmeric,836,80
Cinnamon (Ground Korintje),951,10
Cinnamon (Ground) Xtra Hi Oil (2x),614,31
Cinnamon (Ground) High Oil (1X),682,19

These continue for about 40-50 entries, they are not separated by a blank line though i had to add those so it would display correctly, each entry is on its own line with name separated with spaces, then price after a comma, then quantity after the second comma.....

View Replies View Related

Read Text File Into Array Ask User To Save File And Print Data

Jul 14, 2014

New to programming. Am supposed to create a program that reads a text file (of integers) and computes a series of computations on these integers. I don't have the code for the integers in my code yet, (i know how to do those), but am struggling getting the array to simply print in the print writer. I have the user select a text file, read the file with a scanner, and then save the computations done from my code into another file. specifically, the problem is as follows: Write a program that uses a file chooser dialog to select a file containing some integers. The file contains an integer N followed by N integers. The program then uses a file chooser dialog to let the user specify the name and location of an output file to write results to.The data written to the output file will be as follows

(1) The original list of N numbers from the input file,
(2) The original list of N numbers printed in reverse order of how they appear
in the input file.
(3) The sum and average of these numbers,
(4) The minimum of all the numbers,
(5) The maximum of all the numbers.

[import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;

[Code]....

View Replies View Related

Read Input File And Create Output With Anagram For Words In File

Sep 24, 2014

Well my code is supposed to ask for an input file and then (ex: input.txt), read the input file and create an output.txt file with the anagram for the words in the file. Also it should be displayed on the screen. However my code doesn't display the anagram on screen or the output file!

Heres is the code:

import java.io.*;
import java.lang.*;
import java.util.*;

/* This program will read a file given by the user, read the words within the file and determine anagrams of the given words. If the file that the user inputs is empty, then the program will output "The input file is empty."
* The program will read the file line by line, counting the total number of words read. If there are more than 50 words, "There are more than 50 words."
* will be printed, and the program will terminate. After each line is read, the words in the line will be separated,punctuation characters will be removed, and upper case characters will be switched to lower case.
* If any word is larger than 12 characters, that word will not be considered in the total amount of words in the file and it will not be sorted.
* After each word is read, the letters will be sorted and stored into an array containing each
* word's 'signature'. After all the words have been read, words will be printed to the output file on the same line based upon their signature.
*/

public class Anagram {
//Creating constants for maximum words in file and maximum chars in word
public static final int MAX_CHARS = 12;
public static final int MAX_WORDS = 50;

[Code] ....

View Replies View Related

JSP :: How To Browse And Read A File In Page To Perform Encryption For File

Nov 12, 2014

I am trying to create a file browse option in my jsp and later after browsing, all i want to do is to read that file in my jsp without saving it into that database so that i can perform encryption for it and save that encrypted file later into my database. please provide reference link so that i can refer to some applications and and move ahead with my work.

View Replies View Related

JSP :: Read Log File And Trigger Email With Same File Attached

Apr 6, 2015

I want to read the contents of a log file present on the server, and trigger email along with the log file attached using JSP.

View Replies View Related

Read A File And Then Output Back To Same File On New Line

Jan 24, 2015

I am trying to write to a file but everything I have tried doesn't seem to work. What I want to do is read a file and then output back to the same file on a new line. The text file contains the sentence "Java is a programming language".

import java.util.Scanner;
import java.io.*;
class HW1B {
public static void main(String[] args) {
Scanner fileIn = null;
try {
fileIn = new Scanner (new FileInputStream("text.txt"));

[Code] ....

These are the errors :

HW1B.java:19: error: no suitable constructor found for Scanner(FileOutputStream)
fileOut = new Scanner (new FileOutputStream("text.txt"));
^
constructor Scanner.Scanner(Readable) is not applicable
(argument mismatch; FileOutputStream cannot be converted to Readable)
constructor Scanner.Scanner(InputStream) is not applicable

[Code] ....

Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
2 errors

View Replies View Related

Read File And Input Each Line Of File Into Array

Mar 21, 2015

I am trying to read a file and input each line of the file into an array. I have not set the array size as I was hoping to fill the array using a while loop. Unfortunately the scope of the array does to work inside the while loop so I am being told that the array 'students' has not been initialised even though it has just outside of the while loop. Is it possible to do what I am trying to without having the array initialised in the while loop as surely the array will be reset every time if it was in the while loop? Here is my code:

public static void students(String file) throws FileNotFoundException {
try {
File studentInfo = new File(file);
Scanner input = new Scanner(studentInfo);
String[] students;

[Code] ....

View Replies View Related

Can't Read From A File

Apr 12, 2015

I am writing a program that needs to read from a file, but I can't even get it to open. I have tried saving the .txt file 2 different ways. First, I saved the file to my src/package folder. Then I tried just the src folder. Then I tried going through Import/general/filesystem/ and selecting the root directory. However, my program is still unable to open the file.

private void readFile(String filename){
Scanner file;
try {
file = new Scanner(new File(filename));
System.out.println(file);
file.close();
} catch (Exception e){
System.out.println("Could not find file");
}
}

What am I doing wrong? How/where should I put the file?

View Replies View Related

Read From Txt File - For Loop

Jul 13, 2014

I am trying to read from a file and storing them back in the file but its not working, I think the way I am reading is wrong or something. Is the for loop correct way ?

try {
in = new BufferedReader(new FileReader("Events_data.txt"));
for (int x = 0; x <= Events.length; x++) {
tid = in.read();
te = in.readLine();

[Code] .....

View Replies View Related

How To Read A File Correctly

Feb 13, 2014

So i am creating a File object which has a text file passed to it. I then try to do logic with it using a BufferedReader. However, I get a FileNotFoundException on the using my code below. The Error is on the BufferedReader line. I

Java Code: System.out.println("--Reading text file--");
File file = getFile(c,fileName) // Returns a File object.
System.out.println(file); // Shows me the file is looking correct. Displays contents to console.
BufferedReader br = new BufferedReader(new FileReader(file));
System.out.println("BUFFERED");
while((line = br.readLine()) != null) {
try {
// Do Logic
}
catch(Exception ex){
ex.printStackTrace();
}
br.close(); mh_sh_highlight_all('java');

View Replies View Related

Read From File Input

Oct 25, 2014

The program is reading from file input. how can I replace hate with love the file contains only one line.I hate programing!

Java Code:

[import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class Program3 {
public static void main(String[] args) {
Scanner fileIn = null;

[code]...

View Replies View Related

Unable To Read File

Nov 27, 2014

You are given a text. Write a program which outputs its lines according to the following rules:

If line length is ≤ 55 characters, print it without any changes.
If the line length is > 55 characters, change it as follows:
Trim the line to 40 characters.
If there are spaces in the resulting string, trim it once again to the last space (the space should be trimmed too).
Add a string... <Read More> to the end of the resulting string and print it.

I felt as if I did do what they were asking, but for some reason, I'm getting "Sorry, Unable to read file!"."Instead of using file, I did use the location of the file name".Also, I've attached a image of the input looks like and the output.

My CODE:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
public class Main {
public static void main (String[]args)
{
try{

[code]....

View Replies View Related

Null Pointer Exception When Trying To Write To File

Aug 14, 2014

I've been closely following a tutorial series, and as far as I can tell my code is exactly the same, but not functioning.

The app throws a nullpointer exception here on "controller.saveToFile(fileChooser.getSelectedFile ());"

// Export data item
exportDataItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (fileChooser.showSaveDialog(MainFrame.this) == JFileChooser.APPROVE_OPTION) {
try {
controller.saveToFile(fileChooser.getSelectedFile());

[Code] ....

There are no errors popping up in the code, and I dont understand why there is a null pointer exception when Im saving this file.

View Replies View Related

Null Pointer Exception With File I / O And Scanner

Jul 26, 2014

I get a null pointer error on line 17 of the following fragment. I suspect it has to do with the way I am reading the file, but I'm unsure. I can provide more information or code if necessary.

JFileChooser jfc = new JFileChooser();
jfc.setFileSelectionMode( JFileChooser.FILES_AND_DIRECTORIES );
int userInput = jfc.showOpenDialog( null );
if( userInput == JFileChooser.CANCEL_OPTION ){

[Code] .....

View Replies View Related

Read Excel File Into JTable

Feb 4, 2014

I need to get excel file into jtable . I'm searching for this problem for 1 weak and no results . I downloaded hssf packages and didn't make a change in reading...

View Replies View Related

Use TextIO Methods To Read File

Dec 2, 2014

For the unit 3 assignment, you must develop a program. that reads the file icecream.dat. This file is supplied in the Files for Unit 3 Assignment directory in Unit 3. You must use TextIO methods to read the file. As you read the file, count the total number of ice cream cones that were sold. (This is the same as the number of lines that you read from the file.) Also count the total number of "Strawberry" cones that were sold.

At the end of the program, print out the total number of cones, the number of Strawberry cones, and the percentage of cones that were Strawberry.To do this program, you have to know when to stop reading from the file. TextIO has a function named TextIO.eof() to check whether the entire file has been read. The value of this function is true if the entire file has been read. The value is false if there is more data in the file. You want to continue reading from the file as long as TextIO.eof() is false.

Note: Suppose that flavor is a variable of type String and you want to test whether its value is "Strawberry". To do this, test whether flavor.equals("Strawberry"). (Do not use == to test for equality of Strings.)

Each line of the file icecream.dat is an ice cream flavor such as "Vanilla" or "Strawberry." A line represents the sale of one ice cream cone of the given flavor.

You must complete your program, test, debug, and execute it. You must submit your java code file. The output of your program must be captured by either copying the content in the output window and pasting it into a text document that you submit along with your java code file, or by capturing the image of the screen which contains the output of your java program. In windows you can capture a screen shot with the Ctrl Alt and Print Screen key sequence. This image can then be pasted into a Word, WordPad, or OpenOffice document which can be submitted with your assignment.

For students using the programr service online. Make sure that you create both the TextIO.java and the icecream.dat files as java project files. I found it was easier to rename icecream.dat to icecream.txt for some reason. For students using Netbeans, use the same set of rules that were defined in Unit 2 to setup your project and incude the TextIO.java file as we did in unit 2. Also make sure that you copy the icecream.dat file into your project directory so that it can be found by your java program.

View Replies View Related

I/O / Streams :: Read € Symbol In A File

Aug 25, 2014

I have a problem with € symbol ... an application write a String content on the file system with following code:

out = new FileOutputStream(strPath + "import.xml");
out.write(trp.getTrpXMLModel().getBytes("ISO-8859-1"));

It's correctly wrote on the file system:

TT;Pierre-H€enri;;Orange Grove;zefezfezfez, Directeur Juridique;TT;;azdaz;01 53 33 56 87;ezfezfez;01 53 33 51 59;.....&ée&ée;;;;;;132;CORDIAL;aaa

But when the application try to read the previous file with following code:

BufferedReader in = new BufferedReader(new InputStreamReader(inStream,"ISO-8859-1"));
String line = in.readLine();

The application isn't able to properly read the symbol ... and return following line, without "€":

TT;Pierre-Henri;;Orange Grove;zefezfezfez, Directeur Juridique;TT;;azdaz;01 53 33 56 87;ezfezfez;01 53 33 51 59;.....&ée&ée;;;;;;132;CORDIAL;aaa

View Replies View Related

How To Read Audio File In Java

Mar 20, 2015

How to read an audio file in java?

View Replies View Related

How To Read Particular Column From Text File

Mar 3, 2014

personid gender height class
1 female 1.6 short
2 male 2.0 tall
3 male 1.85 medium
4 female 1.9 medium
5 male 1.7 short
6 female 1.8 medium
7 female 1.95 medium

for eg i have to find minimum and maximum height from column height.

View Replies View Related

Read And Rewrite On XML File - Serialization?

May 19, 2015

I'm building GUI and whole app is going to read and rewirte on xml file..

First I make function for read and write and they are working, but now I decide to make one class where I'm going to have get and set.. This is my class wehre I need to read everything from xm class CurrentData {

private String USERNAME = "username";
private String PASSWORD = "password";
private String STATUS = "status";
public String getusername() {
return username;

[Code] ....

Now my idea is when I start app that everything from xml is going to read from xml and trough get is going to show on my interface, when I edit I'm going to save and with function create I'm going to make same xml file with different parameters... how to use serialization...

View Replies View Related

Read Information From File - How To Get Highest Value

Nov 12, 2014

The requirement asked me to write a Java program to read information from the file, display the original information along with the player's average score of the season (one line for each player), and announce who is the highest average-scoring player of the team. But I always only get the highest which show the last player.

Such like the output showed:

Smith 13.0 19.0 8.0 12.0 Player's average: 13.00
Badgley 5.0 Player's average: 5.00
Burch 15.0 18.0 16.0 Player's average: 16.33
Watson Player's average: 16.33
Knox 10.0 12.0 8.0 6.0 Player's average: 9.00
Casler 15.0 7.0 Player's average: 11.00
Winorburg 13.0 14.0 14.0 23.0 20.0 Player's average: 16.80
Morton 4.0 11.0 13.0 Player's average: 9.33
Seeling 16.0 8.0 9.0 11.0 Player's average: 11.00
Tanshi 5.0 13.0 10.0 Player's average: 9.33

The highest average-scoring player is: Tanshi

import java.util.Scanner;
import java.io.*;
public class PA7{
public static void main(String[] args) throws IOException{
String name = null;

[Code] .......

View Replies View Related







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