Hello im having problems with my code.
i wanted to make a program that would let me enter anything
and then place each one of them inside the array.

import java.util.*;
class charAt
{
public static void main(String[]args)
{
Scanner input=new Scanner(System.in);
String[]sample=new String[52];
String enter;
int x;
System.out.println("Enter something");
enter=input.next();
for(x=0;x<sample.length();x++)
{
sample[x]=enter.charAt(x);
}

}
}

example i would enter "abcd1234"
i wanted it to placed inside the array
sample[0]=a
sample[1]=b
sample[2]=c
sample[4]=d
sample[5]=1
sample[6]=2
sample[7]=3
sample[8]=4

but my code seems to have an error... i need help
i only can use a String array.

Recommended Answers

All 6 Replies

Your problem is with this line:

for(x=0;x<sample.length();x++)

If you enter something that is less than 52 characters (because sample.length() = 52 in your code), then when you do the enter.charAt for something past the end of what has been entered, you will receive an error. You need to change the condition of the for-loop so that you don't go past the end of what has been entered, and you also don't exceed the length of sample (which you already have checked for).

HTH,
darkagn.

The problem is in this line for(x=0;x<sample.length();x++) . First of all, in order to access the size of an array, you need to call .length without the () afterward. However, even if you modified the line to read for(x=0;x<sample.length;x++) , your code would still be flawed, because the sample array may be greater in size than the String in question. For instance, if your enter string is "hello," and x got to 5 (out of 52) and you tried to call enter.charAt(5), you would have an IndexOutOfBoundsException. There are 2 ways to solve this problem:

1. Instead of using for(x=0;x<sample.length;x++) where you are iterating according to the size of the array in the for-loop, use for(x=0;x<enter.length();x++) , where you are iterating according to the size of the inputted string. However, this could also be a problem -- for instance, if your string is 70 characters long and sample[] only has 52 indexes, you will have an ArrayIndexOutOfBoundsException when you try to store the 52nd character in the non-existent slot sample[52]. You could use a combined boolean expression to be sure that neither error happens: for(x=0;x<sample.length && x<enter.length();x++) , but there is a better method (#2).

2. The second method that I tend to prefer is to set the size of the array to the length of the inputted string. That way, the array's size and the string's size will require the same number of iterations, and there will be no out-of-bounds issues:

import java.util.*;
class charAt
{
public static void main(String[]args)
{
Scanner input=new Scanner(System.in);
int size= 0;
System.out.println("Enter something");
String enter=input.next();
size = enter.length();
String[]sample=new String[size];
int x;
for(x=0;x<size;x++)
{
sample[x]=enter.charAt(x);
}

}
}

Hello there, the input size doesnt matter.
i just want to know how can i place each letter or digit inside the array
using a String array.

example i would enter "abcd1234"
i wanted it to placed inside the array
sample[0]=a
sample[1]=b
sample[2]=c
sample[4]=d
sample[5]=1
sample[6]=2
sample[7]=3
sample[8]=4

i corrected my previous code but i have an error saying
incompatible types
found : char
required: java.lang.String
sample[x]=enter.charAt(x);

import java.util.*;
class charAt
{
public static void main(String[]args)
{
Scanner input=new Scanner(System.in);
String[]sample=new String[52];
String enter;
int x;
System.out.println("Enter something");
enter=input.next();
for(x=0;x<enter.length();x++)
{
sample[x]=enter.charAt(x);
}

}
}

That is because the charAt() method of the String class returns a "char" and you are assigning it to a String type.
Use the Charater.toString(char), to convert you 'char' to a String type.
Or alternatively you could make your "sample" variable from a String array to a 'char' array.

sample[x]=enter.charAt(x); is causing issues, because enter.charAt(x) is returning a char, but sample holds an array of Strings. An easy way to fix this is as follows: sample[x]=""+enter.charAt(x); This creates an empty string before the character and adds the character to the string, so basically sample[x] would store a string of 1 character in length. It's a useful trick to know that saves typing and avoids potential type casting confusion.

thanks a lot for the big help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.