Read Txt File And Add Words To ArrayList As Strings - Index Out Of Bounds
Apr 15, 2014
I have been trying to get this method to work for a few hours now, for some reason I get an IndexOutOfBounds exception every time. Basically I am trying to read a txt file and add the words to an ArrayList as strings separated by a space .
private ArrayList<String> readLinesFromFile(BufferedReader inputFile) throws IOException
{
String value = null;
ArrayList<String> result = new ArrayList<String>();
while((value = inputFile.readLine()) != null){
String[] values = value.split(" ");
for (int i = 0; i < values.length; i++){
result.add(values[i]);
}
}
return result;
}
View Replies
ADVERTISEMENT
Oct 21, 2014
I need to read from a text file given to us that has a list of books with authors names and book titles separated by an @ symbol for our delimiter. Here is the code I have right now, but it throws an ArrayIndexOutOfBoundsException at line 7...and I am unsure why?
import java.io.*;
import java.util.*;
public class Driver {
public static void main(String[] args) {
new Driver(args[0]);
[Code] ....
I realize that it must have something to do with my command line argument...but I am unsure what. Am I not entering the file name correctly?
View Replies
View Related
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
Oct 17, 2014
Code is supposed to count number of Words in a String between A-Z and a-z, i am aware there are many methods to do this more efficiently, but i would prefer to do it using the methods shown.
class
// Purpose : 1) Write a program which will input a string from the
//keyboard, and output the number of separate words,
//where a word is one or more characters separated by
//spaces. Your program should only count as words groups
//of characters in the ranges A..Z and a..z
//
{
public static void main(String args[])
[code]....
View Replies
View Related
Oct 21, 2014
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;
import java.awt.event.*;
[Code] ......
Just cant see where the array is out og bounds the array ButtonList has 9 buttons....
View Replies
View Related
Feb 6, 2014
Ok, so I'm just trying to write a basic little program that reverses the letters in someone's name. I thought I had it down, but I guess not. Here's the code, and the error I'm getting is:
java.lang.StringIndexOutOfBoundsException:
String index out of range: 11 (in java.lang.String)
(11 is the length of the name I'm inputting)
import java.io.*;
import java.util.*;
[code]....
View Replies
View Related
Jul 13, 2014
I can't even run the program, because it gives me index out of bounds exception but what exactly went wrong.
I feel that even after it's able to run it will be totally wrong, but one thing at a time /*Write a method called longestSortedSequence that accepts an array of integers as a parameter and returns the length of the longest sorted (nondecreasing) sequence of integers in the array.
For example, in the array {3, 8, 10, 1, 9, 14, -3, 0, 14, 207, 56, 98, 12}, the longest sorted sequence in the array has four values in it (the sequence -3, 0, 14, 207), so your method would return 4 if passed this array. Sorted means non-decreasing, so a sequence could contain duplicates. Your method should return 0 if passed an empty array.
*/ public class ten {
public static void main(String[] args) {
int[] arr = {3, 8, 10, 1,9, 14, -3, 0, 14, 207, 56, 98, 12};
longestSortedSequence(arr);
System.out.println(longestSortedSequence(arr));
[code]....
View Replies
View Related
Feb 18, 2009
I am getting Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4 at lsb2.main(lsb2.java:44)Here is the code:
Java Code: import java.io.*;
import java.lang.Integer.*;
import java.lang.Math.*;
import java.util.*;
class lsb2
[code]....
View Replies
View Related
Apr 8, 2015
I get the following error when trying to run my hangman program:
Exception in thread "main" java.lang.StringIndexOutOfBoundsExceptiion.
Why am I getting this error?
import java.util.*;
import java.io.*;
public class hangman {
public static void main(String[] args) throws FileNotFoundException{
Scanner kb = new Scanner(System.in);
String filename;
[Code] .....
View Replies
View Related
Jan 28, 2014
java.lang.ArrayIndexOutOfBoundsException: 991
at Champion.main(Champions (1) (1).java:209)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
[Code]....
View Replies
View Related
Mar 22, 2014
class test{//class
public static void main(String[]args) {
String booking [][]= new String [30] [6] ;//two dimensional array
System.out.println("Enter the seat column you want");//column entry
char column=Keyboard.readChar();
System.out.println("Enter the seat row you want");//row entry
int row=Keyboard.readInt();
[code]...
cant get my in put to go in to the index
View Replies
View Related
Nov 11, 2014
I have a 2-D character array and I was wondering if I can compare a given index to an out of bounds index and what would I have to write? For instance
if(array[0 - 1][0 - 1] == 'indexoutofbounds'){ **// I know that 'indexoutofbounds' is probably not the right wording**
stuff happens
}
If I can do this what would I actually have to write in where it says 'indexoutofbound' ?
I will be using this for many indexes so it wont always be [0 - 1][0 - 1] it could be [4 - 1][3 - 1] in which case it wouldn't be out of bounds and wouldn't move onto "stuff happens"....
View Replies
View Related
Mar 20, 2015
The following code generates an array out of bounds exception
int[] myArray1 = new int[5];
for(int j = 0; j < myArray1.length; j++)
{
myArray1[j] = 10;
System.out.println("myArray1["+j+"] : " + myArray1[j]);
}
for (int i : myArray1) {
System.out.println("Blah myArray1["+i+"] : " + myArray1[i]);
}
The exception
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at Tutorial2.main(Tutorial2.java:40)
View Replies
View Related
Jan 24, 2015
I have two codes of needed to run but after processing, the said above error is preventing it from getting the general output of the code.I can't seem to find my errors or I don't have any clue at all, if perhaps you know, here's the codes:
Error :
PHP Code:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at simpleOutput.main(simpleOutput.java:13)
public class simpleOutput {
public static void main(String args[]){
int n = Integer.parseInt(args[0]);
for(int i = 1; i <= n; i++){
}
for(int i = 1; n <= i; n++){
System.out.print((i*n)+" ");
[code]....
View Replies
View Related
Apr 23, 2015
I am advised to use a while loop the Scanner method hasNextLine() and in the while loop body, call the Scanner method nextLine(), and add the returned String to the ArrayList of Strings. what I have gotten so far:
Scanner input = new Scanner(new File(""));
while(input.hasNextLine()) {
String line = input.nextLine();
System.out.println(line);
View Replies
View Related
Mar 3, 2015
The question pretty much says it all. My problem seems to be when adding the neighbours, I am always getting a Index out of Bounds problem. I know this is because the code is reaching for the edge of the table, for example if the column, i = 0 and the statement says to perform i - 1 and return it, then we are going to have a problem.
Similarly with anything like j = 20, j + 1 (as the grid only has 20 spaces). I understand the problem, but I am unsure of how to solve it. I have tried messing around with the if statements, but I continue to get the 'out of bounds' problem...
import javax.swing.*;
public class GameOfLife {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int cellChoice = 0;
int newCells = 1;
int generation = 1;
int neighbours = 0;
[Code] .....
View Replies
View Related
Jul 13, 2014
My problem is that I can't even run the program, because it gives me
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 13
at domashno.ten.longestSortedSequence(ten.java:37)
at domashno.ten.main(ten.java:17)
Code :
public static void main(String[] args) {
int[] arr = {3, 8, 10, 1,9, 14, -3, 0, 14, 207, 56, 98, 12};
longestSortedSequence(arr);
System.out.println(longestSortedSequence(arr));
}
public static int longestSortedSequence(int[] arr) {
[Code] ....
View Replies
View Related
Mar 16, 2015
I have an issue with an IndexOutOfBoundsException. I am trying to populate a db from another using arraylists and arrays. I can get the data but the program fails when trying to run the inserts. I am trying to perform SQL in batches of 5. I have added a comment to the failing line.
/*Set in code at beginning*/
ArrayList<String[]> privacyList = new ArrayList<String[]>();
ArrayList<String[]> statementBuffer = new ArrayList<String[]>();
/*Some sql is performed and the following String array is populated*/
while (rs.next()) {
String[] row = new String[55];
resultSetIsEmpty = false;
row[0] = rs.getString("ID");
[Code] ....
View Replies
View Related
Apr 8, 2015
I am working on a class that sorts and matches items in a collection of arrays. In the end, elements common to all 3 arrays should be printed. The idea is that as the first array is compared to the second it stores the matched items in tempArray. When all items are compared the tempArray should overwrite the checked array, and the process continues until all arrays are checked. I have set all the checking in a do loop that should run while the value is <= to the array length. This allows all items in the reference array to be checked. I get an index out of bounds message but not where I would expect it. I have tried varying the condition in the do while loop in the match method, but it did not change the result. There may be other issues I have not addressed with solving the algorithm, but this one has me stumped and I am not able to progress.
package testing;
public class TestMatchMain {
public static void main(String[] args) {
Comparable[] innerCollection0 = {1,2,3,4,5};
Comparable[] innerCollection1 = {1,1,5,6,7};
[Code] .....
View Replies
View Related
Feb 6, 2015
I wanna learn to create a method where I use a scanner or any other way to input.The stuffs I input will be strings. The whole thing will be like a destination creator.So if I input the string USA, it will write this to a file:
1:USA
When I go to the method again the ID will be added so if I input UK, the file will contain:
1:USA
2:UK
Now if I terminate the program, next time when I run it again it will be able to read the ArrayList, so if I wanna go to the method again and write: Norway the file will contain:
1:USA
2:UK
3:Norway
Later I also want to be able to use println to show all of these lines from the file, as well I would like to be able to delete an ID, so if I delete ID 2 the file would contain:
1:USA
3:Norway
I'm quite new to Java, I'm in a Java course since soon 3 weeks back and I have sure learned a lot, all the loops and basic statements that exist in most languages, but now when it comes to this I just don't understand.
View Replies
View Related
Nov 21, 2014
I have wrote this class who read from text line by line and save the words in fileOnTable.. Now i don't know what to read in ReadOffer to save the words in object offers and return this..What JUnit test can write for this code..?
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import com.example.crazysellout.Offer;
[code]....
View Replies
View Related
Nov 15, 2014
So I am working on an assignment and ran into an annoying bug. Basically i have a menu that accesses an ArrayList of Videos which may or may not be read from a file, one of the options of the menu is to edit an existing Video. For this I ask the user for the number of the video and it is checked against the list of video numbers if it returns a match, the method gets the index of the Video object and stores it in a temporary variable the user is allowed to edit the details and the object is put back into the ArrayList using the variable and the ArrayList's set() method
My problem is that once i finish editing the details of a video it gives me a indexOutOfBounds exception
On further investigation using a method that goes through the index of every object in the ArrayList using indexOf() i found out that every single object has been given the index of -1 and not 0,1,2,3 etc.. This is my first error and have not expierenced any other before.
The objects do exist because I have read them from a file. I can also add new Objects and view them successfully but they still have the same index . I have checked my syntax and everything and no errors, this happens at run time only.
I have even created some other ArrayLists seperately and debugged them and their index order is fine. I am too far into this project to start over. I've also tried cleaning the project(my IDE is Eclipse).
View Replies
View Related
Nov 12, 2014
How could I check if an index is exist in an array list? I mean, I should enter an integer and it should return me a boolean result that saying whether if that entered value is an index or not.
View Replies
View Related
Feb 1, 2015
I am just studying over ArrayLists and i have encountered a problem, i'm trying to get the index of an element but it keeps returning -1. Here's the code
Java Code:
import java.util.ArrayList;
public class PhoneEntry {
String name;
String phone;
public PhoneEntry(String name, String phone) {
[Code] ...
I know the list is not empty because it returns a size.
View Replies
View Related
Apr 27, 2015
I would like to know how to set a int and a string to the same index in Array list. For example index 0 would have 5 and "Apple".
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