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.

View Replies


ADVERTISEMENT

Error String Index Out Of Range?

Jan 25, 2015

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

View Replies View Related

String Index Out Of Range Error?

Jul 25, 2014

this method is supposed to compute the decimal value of an entered binary the first 2 lines are causing a string out of index error.

public static int computeDecimalValue(String num)
{
int end = num.length();

[Code]....

View Replies View Related

Error String Index Out Of Range

Jul 2, 2014

I write a code but it show a error message

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));
}
}

View Replies View Related

Arithmetic Operation - String Index Out Of Range Error?

Nov 17, 2014

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;

[Code]....

View Replies View Related

Method To Format Telephone Numbers - String Index Out Of Range Error

Jan 25, 2015

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

[Code] ....

View Replies View Related

Unable To Match Input String With Range Of Values And Getting PatternSyntaxException

Jun 12, 2014

I am trying to match the input string with range of values and getting PatternSyntaxException. below is my code

public static void main(String[] args) {
String regex="01[4-6]";
String code="015";
boolean bool=code.matches(regex);
System.out.println(bool);
}

output: true

But when i try to give range in double digits, it throws exception. What if i have to match a number(as string) between 10 to 25 or like that.

public static void main(String[] args) {
String regex="0[14-16]";
String code="015";
boolean bool=code.matches(regex);
System.out.println(bool);
}

Output:

Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal character range near index 5
0[14-16]
^

View Replies View Related

Index Out Of Bound Error

Apr 5, 2014

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++) {

value.add(' ');

[code]....

View Replies View Related

Array Index Out Of Bound Error

Jan 18, 2014

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;

[Code] ....

View Replies View Related

Array Index Out Of Bounds Exception Error

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

Printing Out String Out Of Index?

Jan 24, 2014

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);
}

[code].....

View Replies View Related

String Index Out Of Bounds?

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

Setting Int And String To Index In ArrayList

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

String Index Out Of Bounds Exception

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

How To Avoid String Index OutOfBounds Exception

Feb 16, 2015

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.

View Replies View Related

If-else Statement Not Working - Syntax Error

Sep 14, 2014

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.");
 
[Code] ....

View Replies View Related

Number Guessing Program - Computer Guesses A Number That User Is Thinking Of Within Certain Range

Mar 28, 2015

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;

[Code] ....

View Replies View Related

HashMap - Searching Library Program By Book Index

Feb 9, 2014

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.

What's causing this?

View Replies View Related

Java Program To Find Index Of Greater Element Whose Value Is Sum Of Remaining Elements

Aug 25, 2014

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.

View Replies View Related

Identity Program - Check If Randomly Generated Number Match Index

Apr 14, 2015

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);

[Code] ....

View Replies View Related

BinarySearch Is Not Working For String Array?

Mar 3, 2014

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.

Java Code:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

[code]...

View Replies View Related

String 2D Array - Connect Four Not Working

Apr 18, 2014

I am trying to make a Connect Four game with 6 rows and 7 columns. The board is a String 2D array and is supposed to look like this:

. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .

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++)

[Code] .....

View Replies View Related

How To Get Resultset Into A String - Split Method Not Working

Mar 15, 2014

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++){

System.out.println(temp[i]);
}

View Replies View Related

Splitting String Not Working - Exception In Thread

Jul 19, 2014

I am designing the game of Snake but it crashes after i added the checkScore() method.

package com.complex.snake;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.*;
import javax.swing.JOptionPane;

[Code] .....

The error i get is : Exception in thread "Thread-3" java.lang.ArrayIndexOutOfBoundsException: 1

View Replies View Related

FileHandling Program Is Not Working?

Apr 28, 2015

this is my progarm

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?

View Replies View Related

Inventory Program - CompareTo Not Working

Jul 16, 2014

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 = "";

[Code] .....

View Replies View Related







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