i have a strings array.
i want to check the length of a string contained in X cell in that array,
in order to create another array with the length of that string.
i've tried using:
public static String[] originalToPrint = new String[10];
then getting strings from the user that will enter the strings array,
then:
int[] temp = new int[ originalToPrint[y].length() ];
but i get a NullPointerException
do you have any ideas?
thanks
The code looks fine, it seems you didn't write any string to the position originalToPrint[y], so you get a nullpointer when you accesss it and call .length() on null. You should post the method where you fill the array originalToPrint and where you get your index y from.
Related
I am trying to store more than 1 String value at a single
index in my arraylist. i try the folowing code but it cannot print the desired output.it only write A input on file only and donot B and C input
List<String[]> arr = new ArrayList<String[]>(2);
String A=jTextField1.getText();
String B=jTextField2.getText();
String C=jTextField3.getText();
String[] element1 = new String[] {A,B,C}; /// A,B,C is an input String
arr.add(element1);
try{
FileWriter f1=new FileWriter("test.txt",true);
try(BufferedWriter out=new BufferedWriter(f1)){
int sz=arr.size();
for(int i=0;i<sz;i++){
out.write(arr.get(0)[i].toString()+"\n");
}
}
you are using for loop with the size of list variable. where the value of
int sz=arr.size(); //its is 1 because there is only 1 variable inside array list.
you need to use the below code to loop with the size of string array inside the list as below.
int sz=arr.get(0).length;
you can do something like this:
arr.addAll(Arrays.asList(element1));
you are using arr.size() where arr is the arrayList. So, it will not be equal to the size of the string array. user the size of the String array. place
int sz=arr.get(0).length; instead of this. int sz=arr.size();
I need to check if a specific variable is in a very large array of Strings (1000+), however doing it with just a for loop and comparing each time would be slow. Is there an alternate method to the way I am using below?
String[] easyWords= new String[]{"integer","project","octopus"}; //+1000s more words
String easyrnd = (easywords[new Random().nextInt(easywords.length)]);
String letterguess = consolereader.nextLine();
for(int i=0;i<easyWords.length;i++){
if(letterguess==easywords[i]){
// do something
}
You can use easyrnd.contains(letterguess) to check if letterguess exists in easyrnd.
So I initialized an array lets say
`string example = new string [5];
When I called the split method on a line of
string x = "abc, def, g";
example = x.split(",");
example[0] = abc
example[1] = def
example[2] = g
I can no longer access example[3] and example[4] as I am getting null pointer
shouldn't these still be accessible with values of null?
Even though you created a array with some initial length for example
String [] sample = new String[5];
after assigning new Array to the variable It will create a new array with new array size. for example.
String s = "Hi how are you";
sample = s.split(" ");
so you can not access old array elements.
It doesnt preserve the values. "example" is assigned with a completely new and different array.
If you want to preserve the number of elements previously present in the array, you can do something like:
int num = example.length;
example = x.split(",");
example = Arrays.copyOf(example, num);
// initialize the new array elements here.
Of course doing so is not very efficent and should be avoided. I suggest you take a look at array lists instead.
Your initial example array (of length 5 with null values) gets overwritten by the new array returned by the call to the split() method, in this case an array of length 3. The initial array referenced by the example reference is not accessible anymore, it will be garbage collected.
If you want to keep both arrays you can assign the result of the split to another variable
String[] example2 = x.split(",");
I am not understanding may be its some thing silly mistake in it
i have array
String[] Data
Below string is at zero index of Data,
[ ["Walls", "Floors", "Ceilings", "Pillars", , "];
and i am willing to put it in loop in some new array at zero index like
String[] Description = null;
Description[0]=Data[0]
It gives error Array index out of bond??
what mistake is going on ?
i wanna put above string at index 0 of Description
hopes for your suggestion
DETAIL:
String[] Description=null;
String Manupulate ="";
for(int ques=0;ques<MultiQuestion.length;ques++)
{
Manupulate = "["Walls", "Floors", "Ceilings", "Pillars"], "";
String[] Detail = Manupulate.split("]");
Description[ques] = Detail[0];
}
Detail[0] will get ["Walls", "Floors", "Ceilings", "Pillars"]
I want to get Detail[0] value in Description[0].
I am using i will have to get Detail value in Descriotion[1] and so on till loop ends
Hopes for your suggestion
If you don't initialize Description, it should give you NullPointerException, not ArrayIndexOutOfBoundsException.
Try
String[] Description = new String[Data.length];
Description[0]=Data[0];
You have not defined length for description.You need to allocate that array memory by :
Description = new String[ARRAY_SIZE];
Does anyone know why this doesn't compile?
public class ArrayCompare{
public static void main (String []args){
String words= "Hello this is a test";
String delimiter=" ";
String [] blocker=words.split(delimiter);
String [][] grid= new String [100][100];
grid[0]="Hello";
if (grid[0].equals(blocker[0])){
System.out.print("Done!");
}
}
}
I would like to perform this function of comparison using a 2 dimensional array. I am a newbie! Please help if you can. Thanks in advance!
Try this:
grid[0][0]="Hello";
grid is a two-dimensional array. For the same reason, you need to do this:
if (grid[0][0].equals(blocker[0]))
grid[0] is a String[] type, not a String. So your code should be like this
grid[0] = new String[100];
grid[0][0] = "Hello";
if (grid[0][0].equals(bloker[0])) {
//your logic...
}
String [][] grid= new String [100][100];
grid[0]="Hello";
There's your problem. You are trying to assign a string to a string array. Think of a 2d array as an array of arrays.
You probably want something like
grid[0][0]="Hello!";
grid is 2d array. you can't do like d[0] = "Hello". So, if you want to assign value at 0 position
d[0][0] = "Hello";
if (grid[0][0].equals(blocker[0])){
System.out.print("Done!");
}
It won't compile because grid[0] is not String type.
It is String[] (Array) type. Variable grid[0] is actually pointing to a String[100] array.
You are attempting to assign a string "Hello" to an array by
grid[0]="Hello"; statement.
If you want to assign a string to a location in grid, you must provide two index(s) - following is legal:
grid[0][0]="Hello";
May I suggest using eclipse or BlueJ to edit your Java code? so that such basic errors are shown on real-time and explained well?
First thing you can't assign value to the element of the multi dimensional array using single index
grid[0]="Hello";
you need to specify both the indices like grid[0][0] = "Hello"
this will set the 0th element of 0th row to Hello
Likewise while compairing
if (grid[0].equals(blocker[0])){
System.out.print("Done!");
you have to pass the same indices here (You cannot compare a String to a Array Object)
if (grid[0][0].equals(blocker[0])){
System.out.print("Done!");