The code gives a boolean error when I try to run it. It says boolean can not be adressed with static. What could be the answer.?
package csd;
class Uti {
public static void main(String[] args) {
boolean result;
result = Sample.foo() && Sample.bar();
System.out.printf("result%b%n",result);
}
class Sample {
public static boolean foo() {
System.out.println("foo");
return true;
}
public static boolean bar() {
System.out.println("bar");
return false;
}
}
}
Error message:
Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method foo cannot be declared static; static methods can only be declared in a static or top level type
Making your Sample class static will resolve your error:
class Uti {
public static void main(String[] args)
{
boolean result;
result = Sample.foo() && Sample.bar();
System.out.printf("result%b%n",result);
}
static class Sample {
public static boolean foo() {
System.out.println("foo");
return true;
}
public static boolean bar()
{
System.out.println("bar");
return false;
}
}
}
Making it a top level class will also work:
class Sample {
public static boolean foo() {
System.out.println("foo");
return true;
}
public static boolean bar()
{
System.out.println("bar");
return false;
}
}
class Uti {
public static void main(String[] args)
{
boolean result;
result = Sample.foo() && Sample.bar();
System.out.printf("result%b%n",result);
}
}
Well there is a compilation error in your code, you can fix your problem by adding a static modifier to parent type like this in your code:
public static void main(String[] args)
{
boolean result;
result = Sample.foo() && Sample.bar();
System.out.printf("result%b%n",result);
}
static class Sample {
public static boolean foo() {
System.out.println("foo");
return true;
}
public static boolean bar()
{
System.out.println("bar");
return false;
}
}
public class positiveCount {
private static int countPositive(int[] elems) {
int positive = 0;
for (int i=0;i<elems.length;i++) {
if (elems[i] > 0){
positive++;
}
}
return positive;
}
//This gives me the number of the positive numbers.
public static void main(String[] args) {
}
}
in the main method, i want to enter a list, for example {3,4,5,-2,-3,0},how to call the method positveCount
If the main method is in the same class :
public static void main(String[] args) {
countPositive(new int[] {3,4,5,-2,-3,0});
}
Else (works also for case 1) :
public static void main(String[] args) {
PositiveCount.countPositive(new int[] {3,4,5,-2,-3,0});
}
I want find Connectivity number in Java. when i try to execute code,any message is not shown(empty)in console. no error message in window o.s. program execute well but when i try again in Linux, error message is shown that no suitabe method 'main' in class.
ConnectivityNumber.java
public class ConnectivityNumber
{
final private int graph[][]={
{0,1,0,1,0,0,0,0,0,0},
{1,0,1,0,0,0,0,0,0,0},
{0,1,0,1,0,0,0,0,0,0},
{1,0,1,0,0,0,0,0,0,0},
{0,0,0,0,0,1,0,0,0,0},
{0,0,0,0,1,0,1,0,0,0},
{0,0,0,0,0,1,0,1,1,0},
{0,0,0,0,0,0,1,0,1,0},
{0,0,0,0,0,0,1,1,0,0},
{0,0,0,0,0,0,0,0,0,0}
};
private int vertex[]={1,0,0,0,0,0,0,0,0,0};
private int connectivityNum=0;
private int startVertex=0;
public boolean isFinishCount()
{
for(int i=0;i<vertex.length;i++)
{
if(vertex[i]==0)
return false;
}
return true;
}
public void randomChoose()
{
for(int i=0;i<vertex.length;i++)
{
if(vertex[i]==0)
startVertex=i;
}
}
public void chooseVertex(int index)
{
startVertex=index;
if(vertex[index]==1)
{
connectivityNum++;
randomChoose();
}
else
{
vertex[index]=1;
}
}
public void rowSearch(int row)
{
for(int i=0;i<vertex.length;i++)
{
if(graph[row][i]==1&&vertex[i]==0)
{
chooseVertex(i);
break;
}
}
}
public void findConnectivityNum()
{
while(!isFinishCount())
{
rowSearch(startVertex);
}
}
public void printConnectivityNum()
{
System.out.println(connectivityNum);
}
}
ConnectivityNumberTest.java
public class ConnectivityNumberTest
{
public static void main(String[] args)
{
ConnectivityNumber connect=new ConnectivityNumber();
connect.findConnectivityNum();
connect.printConnectivityNum();
}
}
My For loop is not working and my print statement (i) is showing error in eclipse
Following is the code.
public class For_Loop {
public static void main(String[] args) {
for (int i = 0; i < 10; i++);
System.out.println(i);
}
}
Remove the ; at end of for
public class For_Loop {
public static void main(String[] args) {
for (int i=0;i<10;i++){
System.out.println(i);
}
}
}
Please remove the semicolon from for loop.
public class For_Loop {
public static void main(String[] args) {
for (int i=0;i<10;i++)
System.out.println(i);
}
}
I cannot figure this out for the life of me. I have a very simple piece of code that keeps terminating on the console window.
public class IsUnique {
public int add(){
return 4;
}
public static void main(String[] args){
IsUnique a = new IsUnique();
a.add();
}
}
public class IsUnique {
public int add(){
return 4;
}
public static void main(String[] args){
IsUnique a = new IsUnique();
a.add();
System.out.println(a);
// Wait until user hits key before quitting
System.in.read();
}
}