Cannot Read Into Array List

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


ADVERTISEMENT

Searching Array - Read From A File That Is List Of Songs

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

How To Create Unmodified / Read Only List

Jun 26, 2014

How to create a unmodifiable/read-only list in Java?

View Replies View Related

How To Create Unmodifiable / Read Only List In Java

Jun 26, 2014

How to create a unmodifiable/read-only list in Java?

View Replies View Related

How To Create Unmodifiable / Read-only List In Java

Jun 26, 2014

I am a java fresher, How to create a unmodifiable/read-only list in Java?

View Replies View Related

Read String To List (Integer) And Back

Feb 14, 2014

I am looking for a good and reliable library to read a string to construct a list of Integers, Doubles, Booleans, etc. This library should be robust enough to handle faulty input from an inexperienced user.

Example:

input: "1, 2, 3, 4"
output List<Integer> [1, 2, 3, 4]

input: "1, 2, 3.6, 4"
output List<Double> [1.0, 2.0, 3.6, 4.0]

input: "true, true, false"
output List<Boolean> [true, true, false]

input: "[1, 2, 3]"
output List<Integer> [1, 2, 3]

input: "(1, 2, 3)"
output List<Integer> [1, 2, 3]

It would be really nice if such a library would already exist.

View Replies View Related

Name / Zip Array Read And Print

Sep 22, 2014

Designed to store a first name (string), last name (string), and zip (int). Assume each line will contain two strings followed by integer each separated by tab. Then print.

I have my two class files (one for the individual and one for the array) and then my driver below. I think my only problem is my driver. I think I'm reading the data file wrong but not sure how exactly.

test in .txt file:

JohnSmith12345
JohnDoe12346
SueSmith09877
VeronicaVarguez67890
MuhammadMaliki54321

public class Person
{
private String firstName;
private String lastName;
private int zipCode;
public Person(String fName, String lName, int zCode)

[Code] ....

View Replies View Related

How To Read A File Than Display It Into Array

Jul 13, 2014

I'm new to java and I'm trying to figure out how to read a file like this:

Jan26081.5910211.79
Feb23301.5918221.79
Mar24261.5922101.79
Apr23751.6921431.79
May24001.6915381.79

And output the file and print it into an array with total under it.

View Replies View Related

How To Read Text File Into Array

Nov 17, 2014

I have to write a program for sorting an array of random numbers by the users choice. These random numbers are stored in a text file, which the user inputs, and is then stored into an array for sorting. I won't have a problem with the sorting algorithms, but I've never had to read a text file and store it into an array before

The text file has the numbers stored like so :
148
626
817
4
312
652
643
etc....

I gather that I'll probably have to user the Scanner for the user to input the text file name, but how do I store it to an array? So far I only have the bones of the program done, like so

import java.util.Scanner;
public class SortingAlgorithms {
public static void main(String[] args) {

[Code].....

View Replies View Related

Read Text File And Put It In 2D Array

Oct 25, 2014

I am trying to read in lines of text from a file then prints out the text. I only need one 2D array and can't copy from one array to another.This how the file looks like:

4 4
FILE
WITH
SOME
INFO

I have been able to read the file but when I am trying to run my program I have blank spacing instead of an array.

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

[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

Read Numeric Values From A Text File Into Array

Oct 28, 2014

I am trying to read numeric values from a text file into an array. This is what i have so far but when i compile it, it doesn't run and give me a result.

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class question2_17110538 {

[Code] ....

View Replies View Related

How To Read Text File In Array And Show It To Textfield

May 30, 2014

I have a problem about read text file in array and show it to textfield :

Content the file:

I want read line 1 to textfield 1, line 2 to textfield 2,....

View Replies View Related

Read In Series Of Passwords Into Array - Valid / Invalid

Apr 21, 2014

Write a program reads in a series of passwords into an array. Store 10 passwords. After creating the password array, have the reader enter a password and see if it is in the array. Fi it is there, print "Valid password" and if it is not there, print "Invalid password."

This is what i have so far

Java Code:
public class passwordMatch
{
public static void main (String [] args){
String [] myArray = new String[10];
myArray[0] = "jasmine22";
myArray[1] = "jackson77";

[Code] .....

It doesn't compile. It gives me error saying .class expected next to String.myArray[]

View Replies View Related

How To Read Data From Text File And Place Into Array

Nov 15, 2014

I'm trying to figure out the necessary code for reading integers from a text file, then placing them withing the two-dimensional array I have created, and then finally printing the array using a nested for-loop. I have attached the file I would like to read from. I'm having trouble figuring out how to reference the text file with the integers, and also how to properly create a dynamic preface for each row of the array. I want the preface to display as "Week #: ", with # being the row number. I tried to create an integer to be used that way, but NetBeans tells me whatever integer I declare has already been defined in method main. I know I need to use scanner to read the file. I would like the output to display like this:

Quote
Temperature Data

Week 1: 73 71 68 69 75 77 78
Week 2: 76 73 72 72 75 79 76
Week 3: 79 82 84 84 81 78 78
Week 4: 75 72 68 69 65 63 65

View Replies View Related

Read Float Numbers From A File / Put Them In Array And Sort

Jun 30, 2014

I'm supposed to write a program, which reads float numbers from a file, puts them in an array and sorts the array. After that I'm suppose to add the numbers so that when I add the 1 and 2 number of the array, I'm suppose to save the sum on the position of number 1, then I add number 3 and 4 and save the sum on position 2 etc. Also if my array has an uneven number of floats it's suppose to add the last 3 and not 2 numbers in the last iteration. The problem is the method throws an ArrayOutOfBounds Exception but I can't seem to find my mistake.

That's the second method. The first one just stores the sum in another variable and then returns it. Also is there a way in that I can Scanner/File/array etc. and initialize the array only once so I don't write the same code two times like it is now.

package sumOfFloats;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;
public class SumOfFloats {
public static float sumFloats() throws FileNotFoundException{

[Code[ ....

View Replies View Related

Formatting Array List?

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

Highest Value In Array List?

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

Alternatives To Array List?

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

Why Array List Is Needed

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

Sort Both Array List

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

Array List And Method

Mar 28, 2014

I had to write a program that prompts the cashier to enter all prices and names, adds them to two arrays lists, calls the method that I implemented, and displays the result and use 0 as the sentinel value. I am having difficulty coming up with a for loop in the method, I believe I have the condition right but I am having trouble with the statements. I now know that String does not have the get property, but I have only done examples with integers and I am not very good with for loops and wouldn't know how to fix it.

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
ArrayList<Double> sales = new ArrayList<Double>();
ArrayList<String> names = new ArrayList<String>();
System.out.print("Enter Number of Customers");
double salesAmount;
System.out.print("Enter Sales for First Customers");
salesAmount = in.nextDouble();
while(salesAmount != 0)

[code]....

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

Removing Duplicates In Array List

Sep 18, 2014

I am stuck on this exercise and I don't know what exactly is wrong. I think it's something with the .remove and the for each loop, but I am not sure.

public class seven {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
list.add("aaa");
list.add("brr");
list.add("unni");

[Code] ....

This is what i get

Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
at java.util.ArrayList$Itr.next(Unknown Source)
at seven.removeDuplicates(seven.java:24)
at seven.main(seven.java:18)

View Replies View Related

Enum Type And Array List

Feb 10, 2015

Here I have an enum class

public enum Money{

ONE_PENNY(1),
TWO_PENCE(2),
FIVE_PENCE(5),
TEN_PENCE(10),
TWENTY_PENCE(20),
FIFTY_PENCE(50),
ONE_POUND(100),
TWO_POUNDS(200);

private int coin;
Money(int c) {
coin = c;
}
int showCoin() {
return coin;
}

and for a test class, I need an array list with a couple of coins in it (i.e. ONE_POUND, TWO_POUNDS) and a loop that adds together the values of the coins in the list and prints the result. How can I do this?

View Replies View Related

Input From Console To Array List?

Dec 10, 2014

I have this very simple application just to test console input:

import java.util.ArrayList;
import java.util.Scanner;
public class WriteTester {

[Code]....

When I let it run, only every third entry is put into the array list and I have to hit "enter" three times for the "break" in line 21 to trigger. I cannot find out why.

View Replies View Related







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