Program Is Working But String Index Out Of Range Error
Sep 5, 2014
My Computer Programming teacher has given the class a problem that requires the use of var.charAt(), var.length() and while/for. Basically, the problem is that we have to create program that'll show a mirrored or reverse version of the entered word. Like for example, if your input is the word "Hello" (the quotation marks aren't included), the output will be "olleH".
Here's the code:
import java.io.*;
public class StringMirror
{public static void main (String [] args) throws IOException
{BufferedReader scan = new BufferedReader (new InputStreamReader(System.in));
String enteredWord = "";
int lengthOfTheWord = 0;
int lengthOfTheWordMinusOneVersion = 0;
[code]....
It is working, but the problem is that after the output, there's this error that says String index out of range: -1.
the program is working but I kind of wanted it to have no errors in the end.
public class op{ String word = "Hello"; //my variable public void reverseword() //My function { for(int i =word.length();i>=0 ;i--) { System.out.println(word.charAt(i));
[code]....
when i call function in main i have this error:
run: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5 at java.lang.String.charAt(String.java:658) at javacourse.Car.opname(Car.java:35) at javacourse.JavaCourse.main(JavaCourse.java:24) Java Result: 1
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String ind ex out of range: 10 at java.lang.String.charAt(String.java:658) at StringChar.main(StringChar.java:11)
Code :
class StringChar{ public static void main(String ss[]){ String str="HelloWorld"; int a; System.out.println("String is = " + str); a=str.length(); System.out.println("String is After Reverse"); for(int i=a;i>=0;i--) System.out.print(str.charAt(i)); } }
I am trying to write a program which asks the user to enter two numbers and then do the arithmetic operation based on user's input . No compiling errors but when I run the program I keep getting "StringIndexOutOfBounds" error .
class arithmetic { public static void main(String[] args){ Scanner in = new Scanner(System.in); int ent1 = 0;
I'm trying to put together a method that formats telelphone numbers, but there's a part of my code that not working. If I enter (305)912-5458 into the variable phoneNumb I get an index is out of range error. I added a lot of system out messages so that I can try to get an idea of why the code was crashing.
public static void main(String[] args) { int intBegin = 0; int intEnd = 1; int length; String charact; StringBuilder numbuilder = new StringBuilder();
[Code] .....
The error message I'm getting is:
run: The length is 13 intBegin is at 0 intEnd is at 1 index is at 0 Charcter ( was not inserted
I'm new to java and I write a simple "Five in a row" game. I have a fixed 13*13 board which contains 169 buttons in an arraylist. Everytime player clicks the button the game set a value of 'x' or 'o' to the arraylist name value. And I have a method named isWon() to check if the it is won or not after player click the button. The game sometime gets and index out of bound exception when checking for whose win.
ArrayList<Character> value = new ArrayList<>(); constructor() { for (int i = 0; i < 169; i++) {
I am getting error array index out of bound ... Error detail is :
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at abhiExample.arraytwodiam.main(arraytwodiam.java:27 )
And program is
Java Code:
package abhiExample; import java.util.Scanner; public class arraytwodiam { public static void main(String [] args) { char[][] Atrace={}; int i,j,k,l=0,row ,characters;
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)+" ");
I am writing a Java permutation program, it takes in N and k and gives k amount of combinations based on N length.
That portion works, but the portion I have that does not work is when k is greater then 1, the array is then then printing strings out of index.
The perm algorithm ends on index - 1 for moving characters but then I push all those characters into an Array List, I would think I could print them off however I want from there but that is not the case. I'll include the minimum amount of code possible.
//permString = 1234 or something, it doesn't matter //Use Case N = 4 k = 3, prints out 123,124,132,134 ect //Use Case N = 4 k = 2, error index out of range but only on the printing function; the perm function doesn't take k and //is based on length //the "" is just an empty string permNow("", permString); }
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.*;
The method I am trying to successfully write is startsWith(String s1, String s2)
The intention of the code is to return true if s2 starts with the string s1, and false otherwise. For example,
x.startsWith("Maplestory","Maple"); Should return true.
This code partially works, depending on the values entered into it. When it is supposed to return true, it does do this. When it is meant to return false, I get a StringIndex OutOfBounds Exception.
public class CC02{ String remove1(char c, String s){ String to_return=""; while(true){ if(s.equals("")) return to_return;
[Code] ....
The method I have written uses one other method from my code, which is "reverse". Any way to return false without getting this error.
My if else statement is not working...it keeps telling me that the else in the statement is a syntax error and that I should remove it. Whats wrong with it?
package Homework2; import java.util.Scanner; public class Homework2 { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("Welcome to the Triangle Program.");
I have a beginning Java Program I have been working on that creates a number guessing program where the computer guesses a number that the user is thinking of within a certain range. I so far have the program below, but need getting rid of a few kinks/ adding features.
-First, I need to set it up so that the user will be prompted and give a range. It should run and produce something like this:
Welcome to this first-ever mind-guessing program!
Please pick a range (higher than 1 and no larger than 50): 32
You will choose a number between 1 and 32... and I will try to guess it.
With each of my guess, you will tell me whether I am too high (h or H), too low (l or L), match (m or M), or you want to quit (q or Q). My objective is to find the number using as few guesses as possible.
-Second, the game is supposed to give up and restart after failing the five, guesses, but for some reason, after it fails the fifth time, it prompts a fifth guess once again instead, then restarts after- I need to prevent this, so that it should look something like this:
My fourth guess is 17: h My guess is too high?
My fifth guess is 16: h *** I am unlucky this round. I give up.
Let's play!
My first guess is 10: etc..
import java.util.*; import java.lang.Math; public class numguessprac1 { // Declaring variables public static String input; public static int quit; public static int guess; public static int wins;
I'm trying to search a library program by book index and running into some trouble with my HashMap. Here's the code in question:
Java Code:
public String searchByBookIndex(int input) { NumberFormat fmt = NumberFormat.getCurrencyInstance(); Map<Integer, Book> bookMap = new HashMap<>(); for (int i = 0; i < author.booksByAuthor.size(); i++) { bookMap.put(author.booksByAuthor.get(i).getBookIndex(), book);
[Code] ....
The search itself works fine; however, the problem is that the values of everything in the HashMap are overwritten by the last book entry. For example, let's say there are four books (index in parenthesis):
The Book (1), A Wonderful Book (2), A Great Book (3), A Bad Book (4).
Once the code above iterates, the HashMap out is this (via println):
1 = A Bad Book, 2 = A Bad Book, 3 = A Bad Book, 4 = A Bad Book.
I need to write a java program to find the index of the element whose value is the sum of the remaining elements. Recently I have been asked this question in an Interview which I couldnt solve properly.
I have this program where I'm supposed to fill an array with 1000 indices with 1000 randomly generated numbers between 1 and 1000. The program is supposed to check if any of the numbers match an index that is the same value (so for example, the number 4 is in index 4). How to check for that condition, especially using a binary search (I'm also told to use a binary search).
Right now the variable index isn't initialized because I don't know what to initialize it to exactly. How do I check to see if any numbers match the value of the same index?
import java.util.*; public class Identity { public static void main(String[] args) { int [] integers = new int [1000]; // Fill array with randomly generated numbers int [] display = GenerateRandom(integers);
I don't know why, but my binaryStringSearch method isn't correctly printing even though it's virtually the same thing as my binaryIntSearch method, which works perfectly.This is the objective of the exercise:
1) Use binary search to output the position of the following numbers from file numsearch.txt
5, 9, 43 and 79.
2) Read from file namesearch.txt
Use binary search to output the position of the following names Dennis, Billy-Bob and Steve.The file contains 20 entries.
Whenever I enter a column number to drop a checker, the program immediately stops running and gives me a "java.lang.ArrayIndexOutOfBoundsException" and points to my checkWinner method. There are two int constants, one is LOWEST_ROW_INDEX = 5, and the other is RIGHTMOST_COLUMN_INDEX = 6. I cannot figure out what to do differently with my checking algorithm to stop this from happening.
public static String checkWinner(String[][] gb) { //tests for a horizontal line made by four of the same color checker for(int row = 0; row <= LOWEST_ROW_INDEX; row++) { for(int col = 0; col <= RIGHTMOST_COLUMN_INDEX; col++)
I am trying to get resultset into a string and then use split method to store it in an array but it is not working. i tried to work it seperately as java program but it throws exception "could not find or load main class java."
String ar = "This.is.a.split.method"; String[] temp = ar.split("."); for(int i=0;i<temp.length;i++){
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Random; public class FileHandlingDemo {
[code].....
As you can see my Intention is to save randoly generate Integer in the output file And this is not working but if I use Something like this
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class FileHandlingDemo {
[code].....
it's working fine reading that from Input.txt and writing that data in output.txt file. but not in first case Both file are in working directory already created.Where is wrong?
My instructor gave me this code to use as my Array sort. I can not get this to work. I do not get any compile errors, the code just does not do anything.
Java Code:
package inventoryprogram4; import java.util.Scanner; public class Inventoryprogram4 { static GUI mainGUI = new GUI(); static String outText = "";