String Out Of Bounds Caused In While Loop
Jul 24, 2014
The code is supposed to insert HTML formatting on a specified string. (i.e o<b>the</b>r )Most of the code works correctly as I verified the outputs without a while loop. The loop is causing a string out of bounds error. I've tried adding the if statement and also formatting the (result += line) nothing is getting appropriate results...
public static String addFormat(String webpage, String toMatch, String format) {
String result = "";
String insert = "";
format = format.toLowerCase();
switch (format){
case "bold":
insert = "<b>" + toMatch + "</b>";
[code]....
View Replies
ADVERTISEMENT
Jan 18, 2014
So the following is my code for the Terrian Generation of my game, however the way i have it rendering all the tile rectangles as opposed to just rendering whats visable on my JFrame causes lots of lag.
These are the variables and Rectangles inside _TerrianGen.class
//CHUNK
static int chunkx =2048;
static int chunky =2048;
//RECTANGLES
public static Rectangle[][] tile = new Rectangle[64][64];
static int[][] blockType = new int[64][64];
//0=Grass 1=Dirt 2=Stone 10=Brick 11=Coal 12=Iron 21=Gold 22=Diamond -1=null
This is the Init() code
for (int x =0; x < chunkx-32; x+=32){
for (int y=0; y < chunky-32; y+=32){
tile[y/32][x/32] = new Rectangle(x, y, 32, 32);
blockType[y/32][x/32] = -1;
[Code] ....
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
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
Mar 16, 2014
I can't figure out how to have all of the random characters generated to go into the String. Below I can only get the last character to covert over to a String.
System.out.println("Original random character string:");
String printingString = "a";
for (int i = 0; i < 50; i++)//loop to obtain 50 random characters
{
char randomChar = (char) ((Math.random() * 255) +32);
System.out.print(randomChar);
printingString = Character.toString(randomChar); }
return printingString; }
View Replies
View Related
Nov 18, 2014
Here, I want to avoid using the line **return"";** and instead return all of the values I output with **System.out.println();**
I have tried using a **StringBuilder** method, but this outputs nothing when I run my test program. I am guessing that StringBuilder must work differently in a while loop, but I can't find how!
[URL] ....
public String toString(){
int topLine = size;
int topCurrentLine = size;
int bottomLine = 1;
int bottomCurrentLine = size;
[Code] ....
View Replies
View Related
Apr 26, 2015
I'm trying to code a little text RPG. I've made a little "personality test", with 4 questions (answers a b c), where every letter stands for a type of personality. The analysis of the result is simple counting of the answers, if you have 3 answers a (4 questions), then a has won. If 2 on a, and 2 on b, a simple Random method shows weather result a or b.
Now... the test should give you a "partner" (imagine the pokemon game with 3 different starter pokemon). I have now 3 string variables, like 3 different partners. They are all declared somewhere outside, but i only need one later.
Like...the test is completed, you have your partner and how can i make now that the program is just showing me my partner? Like...when i type " System.out.println(partner); " (outside of the test loop! ) that i only get the one i got through the test?
I was trying to declare in every loop the partner String with every result. But outside the loop java isn't recognising that String, bcs...ofc...it was declared for the loop. So i had in every if or else if clause a " String partner = anwsA;" and answB and answC, thats why i cant declare them outside.
Short: i need a partner String variable that could have 3 possible results... i jut need one
I'm using Eclipse Luna 4.4
View Replies
View Related
Apr 7, 2014
I am looking to pass a long list of results in a String toString() method,this is my code
public void newlist(){
for(int i = 0 ; i <= nbComposant;i++){
System.out.print(ref+i+" (quantity "+quantity+i+")");
}
}
public String toString(){
return newlist();
}
View Replies
View Related
Feb 7, 2007
import javax.swing.*;
public class mostFrequent{
public static void main(String args[]){
char index;
String s;
s = JOptionPane.showInputDialog("Enter String here");
int currFrequency = 0;
for(index = 0; index<s.length(); index = index++){
int i = 0;
for(i = 'A'; i<='Z'; i++){
if(i==s.charAt(index)){
currFrequency = currFrequency + 1;
}
}
}
System.out.println("end");
}
}
//my code so far
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
Nov 24, 2014
Why I have this out of bounds exception? I highlighted the code that the error is in
public String encrypt() {
//TODO: Complete this method
int groupMoves = 0;
int testing = 0;
String encrypted = "";
//a b c d e f g h i j k l
//1 2 0 1 2 0 1 2 0 1 2 0
[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
Jan 16, 2014
Goal this time is to take a charArray, copy it into another charArray while reversing the things in it.
E.g. charArray["!ollaH"] into charArrayNew["Hallo!"]
My first idea was to revert the stuff in the Array with a ! cause i saw earlier that u can work with that too revert booleans. Sadly i didnt happen to make it work.
Next thing i thought of was a for loop to go trough the charArray and copy every section into charArrayNew just at the opposite end.
Java Code:
import java.util.Arrays;
public class aufgabe43 {
public static void main(String[] asgr){
char[] charArray
[Code] .....
Eclipse doesn't show any errors, and as u told me last time i did include import java.util.Arrays; to output the array in the end.
When i try to compile the code eclipse returns with an error
Java Code:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 68
at aufgabe43.main(aufgabe43.java:8) mh_sh_highlight_all('java');
Which I frankly don't understand since the array . Length is exactly the same.
View Replies
View Related
Sep 29, 2014
I am trying to avoid an out of bounds error for my heap array by checking to see if the current values left and right child are greater than the size of the array and then ending the execution of the code. However, I am still getting an out of bounds error even with that condition.
public void trickleDown(){
System.out.println("TRICKLE DOWN");
boolean repeating = true;
int temp, leftChild, rightChild, leftIndex, rightIndex, parent = 0;
while(repeating){
[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
May 26, 2014
I 'm trying to draw a button by resizing a simple button background image to fit the size of the text. Everything works well except for the actual drawing of the string. The y value that I give the Graphics object designates the baseline, however I would like to draw the the string above the y. So that the image below doesn't happen.
I gave the graphics object the y of the bottom of the "P" and would instead like to give it the value of the bottom of the "y". Is there a way to do that by reading the String's bounds or something, or is there a way to get the Graphics object to use the passed y value as the definite bottom of the String?
View Replies
View Related
Apr 14, 2015
I am trying to print out all of the values of an array and the average. I am getting part of the printout but I am also getting an array out of bound error message. what I am doing wrong?
int [][] points = new int [2] [3];
points [0][0]= 3;
points [0][1]= 2;
points [0][2]= 4;
points [1][0]= 2;
points [1][1]= 2;
points [1][2]= 2;
int totalPoints = 0;
int totalShots = 0;
[code]...
View Replies
View Related
Mar 26, 2014
It seems that all of my arrays in the class Calculations are out of bounds, and I do not seem to know why.
Java Code:
/**
public class DataAnalyzer
{
public static void main (String[] args) {
//This creates an instance of ReadFiles
ReadFiles aReadFiles = new ReadFiles();
Calculations aCalculations = new Calculations();
[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
Nov 30, 2014
I have been working on this program for a while and now i seem to be stump it throws an outof Bound array exception error, this program is a matrix multiplication program and spits out the resulting matrix using multithreading. i have a running one and result is
2 -1 0
1 0 3
-1 1 3
but this program's result is:
2 0 0
1 0 0
-1 0 0
it reads a txt document as an commandline arguement the text file reads just like this below:
3 2 2 3
1 0
0 1
-1 1
2 -1 0
1 0 3
the following is my code:
import java.util.*;
import java.io.*;
public class P3 {
public static int matrix1[][];
public static int matrix2[][];
[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
Jan 10, 2015
I am having trouble with an array out of bounds exception. I understand what the error means (that I am trying to access part of array that does not exist). But I do not see the error in my code.
Java Code:
public class bubbleSort
{
public static void main(String []args)
{
int i;
int array [] = {12,9,4,99,120,1,3,10};
[code]....
View Replies
View Related
Oct 31, 2014
I am writing a java program that takes a FROM image, a TO image, and a ratio (this is a slide bar in the GUI). Here's my code:
Java Code:
public static BufferedImage rollUp (BufferedImage from,
BufferedImage to,
double ratio) {
BufferedImage finalBufferedImage = new BufferedImage(from.getWidth(),
from.getHeight(),
from.getType());
int packedColor = 0;
for (int r = 0; r < from.getHeight(); ++r) {
for (int c = 0; c < from.getWidth() ; ++c) {
[code]...
So from 0 to 1 (ratio is a double between 0 and 1) the image will "roll up". The effect works completely in the GUI, but the console freaks out at about 0.33 ratio.. This program runs for testing in a class Main and uses a class Splittinimage.. this method is in class TwixPix. When the main class is run, a box pops up with a combo box and a slider. You pick an effect (in this case, roll up) and then slide the slider to set the ratio. The image below those two things performs the effect that was selected. Imagine a PowerPoint presentation slide effect.
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