Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I'm making a quick hangman game, and came across an IndexOutOfBoundsException and was wondering why. I don't see the problem/how this error would come about.
It happens at this line:
array[index]+=c;
Any feedback is appreciated.
import java.io.IOException;
import java.util.Scanner;
public class Driver {
public static void main(String[]args) throws IOException {
Scanner console = new Scanner (System.in);
String[] phrase={"television"};
String[] array= new String[phrase.length];
int body =6;
while(array!=phrase) {
char c=(char)System.in.read();
int index= console.nextInt();
array[index]+=c;
if(array[index].charAt(index)==phrase[index].charAt(index)){
System.out.println("the new array");
}
}
}
}
There are many Issues with the code. few of them are below.
you are creating array of size "phrase.length" which will be of size 1, when i enter 2 for "console.nextInt();" it will throw index out of bound.
Array Equality check is wrong, you need to do something like
if( Arrays.equals(array1, array2) )
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I'm setting up this do- while loop . Where do i need to correct this code
so when 000000 is given , the loop ends.
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
String am ;
{
do
{ System.out.println("give am number");
am = kb.next();
if (am.matches("[0-9]+") && am.length() <= 6)
{System.out.println("am = "+am);
{break;}}
else
{System.out.println("wrong try again");
am = kb.next();
}
} while(!"000000".equals(am));
Right now, you are checking for equality, not matching. "000000" is not equal to "[000000]", so the loop keeps going.
It's not completely clear what you want, but I think just ditching the square braces is probably the answer.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
recently my teacher showed us how to make a summ methode but i cant remember it.I think i got it a bit ritght.But it tells me error unexpected token a
void main(){
summ(6, 7);
}
int summ(a, b){
a = int
b = int
return a+b }
It should be like this
static int summ(int a, int b)
{
return a+b;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
if(args.length != 1){
System.out.println("Incorrect number of arguments");
return;
}
String inString = "";
Scanner s = new Scanner(new FileReader(args[0]));
while(s.hasNextInt){
int temp = s.nextInt();
inString += temp;
}
This is an excerpt of code from my main method. Code won't compile; the two errors are cannot find symbol (s.hasNextInt) and illegal start of type (while(s)). The other question I found here about .hasNextInt and cannot find symbol was someone trying to call .hasNextInt on a string instead of a scanner, and I can't figure out why my scanner can't run hasNextInt. I imported .util and .io.
Can someone help me figure out what I'm doing wrong?
You're missing the brackets () for the method call hasNextInt().
while(s.hasNextInt()){
int temp = s.nextInt();
inString += temp;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I'm having an issue with multiple NullPointerExceptions, I'm getting one for my if statements within iterators and I can't figure out why
one of the problem blocks:
public int removeAllBooks(String author, String title){
Iterator<Book> itr=library.iterator();
int i=0;
while(itr.hasNext()){
Book book=itr.next();
if(b.getAuthor().equals(author)&&(b.getTitle().equals(title))){
itr.remove();
i++;
}
if(i>0){
return i;
}
}
return 0;
}
The NPE error points to my if-statement line for some reason.
Thanks.
What is b? Where is it declared/initialized? Your itr.next() is assigned to the book variable, not b.
You code should probably be :
Book book=itr.next();
if(book.getAuthor().equals(author)&&(book.getTitle().equals(title))){
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am having a problem when I am trying to compile a pattern program. I am creating this program in BLUEJ and when I am trying to compile it shows the error : "not a statement"
class pattern
{
public static void main()
{
int p=0;
for(int i=1;p=1;i<=4;i++,p++)
{
for(int j=1;j<=i;j++)
{
System.out.print(Math.pow(p,2);
}
System.out.println();
}
}
}
What is the problem?
A couple of issues there, the main one being this:
for(int i=1;p=1;i<=4;i++,p++)
// ^ ^ ^
The for statement consists of three, not four, parts separated with ;. I suspect you wanted
for(int i=1,p=1;i<=4;i++,p++)
// ^--- comma here
Separately, I believe you have to specify the argument to main even if you're not using it, so:
public static void main(String[] args)
In a comment on the qustion, cadrian pointed out a further problem:
System.out.print(Math.pow(p,2);
// Missing ) here ------------^