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 ------------^
Related
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 4 years ago.
Improve this question
I hope my question was worded correctly.. here is a snippet of code, if I uncomment the line System.out.println(hs.add(ar)); console will print true, so why are reaching inside the following if statement?
public static void duplicateExists(String [] array)
{
Set<String> hs = new HashSet<String>();
for (String ar : array)
{
// System.out.println(hs.add(ar));
if((hs.add(ar)) == false);
{
System.out.println("reaches here every time but shouldn't ");
}
}
}
public static void main(String[] args) {
duplicateExists(new String[] {"1","2","5","3","6","8"});
}
This line:
if((hs.add(ar)) == false);
Does nothing, because of the ending semicolon. The element is actually added to the set, despite the condition of the if is never satisfied.
Then, you have the following block of code:
{
System.out.println("reaches here every time but shouldn't ");
}
It's awkward, but in Java, you can have arbitrary blocks of code surrounded by braces. This one is always executed, because (due to the semicolon) is totally independent of the previous if.
Try removing the semicolon:
if ((hs.add(ar)) == false) {
System.out.println("reaches here every time but shouldn't");
}
Formatting the code might be seen as something not very important, but here is a case that clearly shows that when code is not nicely formatted, unintuitive, unexpected, hard-to-debug issues might happen.
Besides, you could simplify your code:
if (!hs.add(ar)) {
System.out.println("reaches here every time but shouldn't");
}
Now it doesn't reach the println.
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) )
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 7 years ago.
Improve this question
I started learning core java from youtube but while writing same code in eclipse i am getting syntax error .
package Javatut;
public class ifelse {
public static void main(String[] args) {
int test = 10;
if (test == 10);{
System.out.println("yes");
}
else{
System.out.println("No");
}
}
}
I suggest you use an IDE as this will help you find such issues. After using the auto-formatter, I can see this warning in IntelliJ
You can see that the formatting is not what you think it should be as you have an extra ; in your code.
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
public class project5{
public static void main(String args[]){
String[] storage = {"123457897", "123456","654654654"};
int current;
current = Integer.parseInt(storage[1].subString(1,5));
System.out.println(current);
}
}
So I'm trying to, as an exercise, just get the first 5 numbers in the first thing of the array and parse it as an integer and store it as the variable current. It gives me the error:
test.java:5: error: cannot find symbol
current = Integer.parseInt(storage[1].subString(1,5));
^
symbol: method subString(int,int)
location: class String
1 error
What is it that I'm doing wrong?
There is no subString method on the String class. There only is substring (all lowercase).
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html
For variable, class and method names casing matters.
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 tried to use assertions in java from an example put forth from a site but it is not throwing an Assertion Error even if it does not conform to the requirements.
Where am I doing wrong?
package Sources;
import java.util.Scanner;
public class MyAssertion
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your age");
int age = scanner.nextInt();
assert age>=18:"Not Valid !!";
System.out.println("Age is -- "+age);
}
}
I have also used the following command :
java -ea -cp ./classes Sources.MyAssertion
But even if I enter the age as 33 it is not throwing an error . Why?
Any help is much appreciated :)
Assertions fire when the condition is not true. If you enter 33 then that is greater than equal to 18, so all is well.