This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Closed 2 years ago.
`this has been bugging me .giving me error.....can anyone tell me how to solve that:
exception in thread "main" StringIndexOutOfBoundsException: String index out of range: 8'
public class Solution {
public static boolean checkit(String input){
int i,j;
boolean s=false;
for(i=0;i<=input.length()-3;i++){
for(j=i+2;j<=input.length()-1;j++)
{
if(input.charAt(i)==input.charAt(j+1) && input.charAt(i+1)==input.charAt(j))
s=true;
}
}
return s;
}
public static void main(String [] args){
System.out.print(checkit("ABcBAxxs"));
}
}
having
for(j=i+2;j<=input.length()-1;j++)
{
if(input.charAt(i)==input.charAt(j+1) && input.charAt(i+1)==input.charAt(j))
when j values input.length()-1 then input.charAt(j+1) access at input.length() so out of your String
Out of that, when s become true it is useless to continue to loop because s cannot become false back. You can remove your variable s, replace s=true; by return true; and return s; by return false;
Related
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
I cannot seem to find why I am getting this error message. I thought I have already instantiated my array in my main.
Exception in thread "main" java.lang.NullPointerException
public class A1ArrayList<E> {
private E[] e;
private int capacity = 0;
public A1ArrayList(){
}
public int size(){
return e.length;
}
public boolean add(E addElement){
e[capacity] = addElement;
capacity = capacity + 1;
return true;
}
public static void main(String[] arg){
A1ArrayList<Object> e = new A1ArrayList<Object>();
e.size();
}
You have to initialize your array. Right now you have a field e that has place for an Array of E. But there's no array in that field! So if you try e[capacity] = addElement; you will try to add something to nothing, which is why you'll get a null pointer.
In your constructor you could use this to initialize the array.
public A1ArrayList(){
E=new E[5];
}
Like that you have an Array where you can store 5 instances of E.
Your Array e is null. Therefore you get a Nullpointer Exception.
This question already has answers here:
Java, Simplified check if int array contains int
(15 answers)
Closed 6 years ago.
for example I want to do something like this:
package tst;
public class forInif {
public static void main(String[] args) {
int[] a = {1,2,3,4,5};
if (for(int i : a) {i == 5;}) {/**/}
}
}
I know normally I should make a boolean value of this situation by my own, but just in case, is there anyway to do something like that rather than make a boolean value somewhere else?
In Java 8+, you can use IntStream.anyMatch:
if (IntStream.of(a).anyMatch(i -> i == 5)) { ...
Not directly no: the condition check in a Java if needs to be a boolean type. But you could build a function:
public class forInif {
public static void main(String[] args) {
int[] a = {1,2,3,4,5};
if (foo(a)) {/**/}
}
}
where foo is a function returning a boolean that takes an int[] as a parameter.
There is no way to make a for loop return a boolean value. However, streams sound like a good way to do this in one or two lines:
int[] a = {1,2,3,4,5};
int fives = Arrays.stream(a).filter(e -> e == 5).count();
if (fives > 0) {
//replace this with whatever you want to do when a five is found
System.out.println("One or more fives exists in the array");
}
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I'm trying to return the reverse of a string using recursion but I got a few errors. I'm new to recursion so I don't really know where to start. The errors I'm getting are:
Exception in thread "main" java.lang.NullPointerException
at java.lang.String.concat(String.java:2027)
at Recursion.reverseString(Recursion.java:30)
at Recursion.reverseString(Recursion.java:30)
at Recursion.reverseString(Recursion.java:30)
at Recursion.reverseString(Recursion.java:30)
at Recursion.reverseString(Recursion.java:30)
at Recursion.main(Recursion.java:46)
Here's my code:
public static String reverseString (String inString) {
String result = "";
if (inString.length() > 0) {
// if the string is empty
result = inString.charAt(inString.length()-1) + "";
result.concat(reverseString(inString.substring(0, inString.length()-1)));
return result;
} else {
return null;
}
}
// the testers
public static void main(String[] args){
String inString = "abcde";
// test the reverseString
String revString = reverseString(inString);
System.out.println(revString);
}
}
The exception stacktrace is telling you that null is being passed to the concat() method. The argument to concat() is your reverseString() method which returns null when the string is empty. Instead of returning null, you could return an empty String to avoid the NullPointerException. Replace the line:
return null;
with
return "";
In addition to what #Asaph said you also need to assign this expression
result.concat(reverseString(inString.substring(0, inString.length()-1)));
to your result variable so that it becomes:
result = result.concat(reverseString(inString.substring(0, inString.length()-1)));
Otherwise it won't work because the concat method of the String class does not return the same object from which it was called
This question already has answers here:
how to explain return statement within constructor?
(6 answers)
Closed 7 years ago.
I don't know what is the usage return;. What does it mean ? Does it return something ? I think return keyword use to put back some value but I am not sure because if constructors return only instance of class below code should be invalid.
public class Test {
public Test() {
return;
}
}
Any suggestions ?
It does not returns anything.
It means the end of any method which returns void, so in this case, it means the end of the constructor. See the example below:
class Test {
private int sum;
public Test(Object otherObj) {
if (otherObj != null){
sum = 42;
return;
}
sum = 0;
}
public static void main(String[] args) {
Test testZero = new Test(null);
Test testNonZero = new Test(testZero);
System.out.println(testZero.sum); // 0
System.out.println(testNonZero.sum); // 42
}
}
if the otherObj is not null, the sum'll be 42, and the constructor will stop running.
You can use the same strategy, if you have a public void someFunction() method, to exit at some point.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
Why is this coming out as false?
public class practice
{
public static void main(String [] args)
{
System.out.println(startHi("hi "));
}
public static boolean startHi(String str)
{
System.out.println(str.substring(0,2));
if(str.length() < 2)
{
return false;
}
else if(str.substring(0,2) ==("hi"))
{
return true;
}
else
{
return false;
}
}
}
You should use the .equals method to check for equality of strings, not ==. See here.
Using == is checking to see if the objects have the same address in memory. That's not usually what you're looking for when checking if the value of two strings are the same.