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 6 years ago.
Improve this question
I am getting this compilation issue, and I'm not able to figure out why. Can someone help?
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.getInt() ;
System.out.println(factorial(n)) ;
int factorial(int a){
if (a==0)
return 1;
else
return (a*factorial(a-1));
}}
Post Edit Note: I wasn't aware of the fact that another function can't be declared inside main(). On writing it outside, it worked fine.
You're trying to create a method called factorial inside your main method. It has to be next to it, not inside it.
Formatting your code readably and consistently helps make the problem clear:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.getInt();
System.out.println(factorial(n));
int factorial(int a) { // <=== Problem
if (a == 0)
return 0;
else
return (a * factorial(a - 1));
}
}
}
Instead:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.getInt();
System.out.println(factorial(n));
}
int factorial(int a) {
if (a == 0)
return 0;
else
return (a * factorial(a - 1));
}
}
The above still has a problem (factorial needs to be static, or you need to create an instance to call it on), but it's in the right place now.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am new to java, trying to learn by making mini projects right now. I have two classes and when I run my program I have this error : Cannot make a static reference to the non-static field Game.balance.
Not quite sure why I am getting it and wondering if anyone knows any fixes.
import java.util.Random;
import java.util.Scanner;
public class Mainone {
public static void main(String[] args) {
System.out.println("You have $1000. I hope you make good choices!");
Scanner user = new Scanner(System.in);
Game print = new Game(1000,0,0,true);
System.out.print(Game.operation);
}
}
this is the second class below (new file)
import java.util.Random;
public class Game {
int balance = 1000;
int operationAmount;
int randOperation;
boolean ad = true;
public Game(int b, int o, int r, boolean a) {
balance = b;
operationAmount = o;
randOperation = r;
ad = a;
}
}
System.out.print(Game.operation);
change to
System.out.print(print.operation);
In java, you can't access Object field values by Class directly. But you can access the static field by using Class.
public class Game {
public static String ABC = "1"; // can access by Game.ABC
int balance = 1000;
int operationAmount;
int randOperation;
boolean ad = true;
public Game(int b, int o, int r, boolean a) {
balance = b;
operationAmount = o;
randOperation = r;
ad = a;
}
}
public static void main(String[] args) {
System.out.println("You have $1000. I hope you make good choices!");
Scanner user = new Scanner(System.in);
Game print = new Game(1000,0,0,true);
System.out.print(Game.ABC); // here you can access the static field
}
you are trying to call Game.operation. Instead of this try print.operation.
I assume operation is function in class Game.
Here the state is managed by the print object of Game class.
Please call System.out.print(print.operation);
Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 5 years ago.
Improve this question
I guess my main method has an issue in it, but i'm not really sure. Hope anyone could help.
package testing;
import java.util.*;
public class mid08 {
public static int[] Sort(int[]x) {
int max=max(x);
int[] y=new int[max+2];
for(int i=0;i<x.length;i++)
y[x[i]+1]++;
return y;
}
public static int max(int[] x){
int max=x[0];
for(int i=0;i<x.length;i++){
if(x[i]>max)
max=x[i];
}
return max;
}
public static void main(String[] args) {
int[] z = new int[] {2,3,5,1};
Sort(z);
System.out.print(Arrays.toString(z));
}
}
I'm sure of both my methods but not of the main method itself.
You ignore the result of your sort:
z = Sort(z);
System.out.print(Arrays.toString(z));
Should fix your issue.
Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 6 years ago.
Improve this question
I am writing a java method which runs through an array and if a value is present, then it returns the index of the value. It is not compiling, but I don't know what part of my code isn't comprehensive.
import java.util.*;
import static java.lang.System.out;
public class Lab26 {
public static void main(String[] args) {
}
public static int simpleSearch(int[] nums, int value) {
int nul = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == value) {
return i;
}
}
}
}
What if if (nums[i] == value) is never satisfied? You will not return anything but the method signature expects you to return an int.
in simpleSearch method, return an integer at the end.
This is the link for the actual problem, here
I have submitted my code multiple times, but each time it had a compilation error. The original message was
"Main.java:4: error: class Egypt is public, should be declared in a file named Egypt.java
public class Egypt {
^
1 error"
I have no idea where I went wrong. I have copied my code for the problem below. please help me with this code:
import java.util.Scanner;
import java.util.Arrays;
public class Egypt {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (true){
int[] arr = new int[3];
for (int i = 0; i < 3; i++)
arr[i] = input.nextInt();
if((arr[0]+arr[1]+arr[2])==0)
return;
Arrays.sort(arr);
int d = (int)(Math.pow(arr[0],2) + Math.pow(arr[1], 2));
if(Math.sqrt(d)==arr[2])
System.out.println("right");
else
System.out.println("wrong");
}
}
}
From the Java specifications here,
All programs must begin in a static main method in a Main class.
Do not use public classes: even Main must be non public to avoid compile error.
So, I think you must use
class Main
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I've learned to write code implementing the Diffie-Hellman key exchange algorithm below, but I feel that my code isn't at its most efficient. Can anyone correct my code please...?
import java.math.BigInteger;
import java.security.SecureRandom;
import java.security.spec.AlgorithmParameterSpec;
import java.util.Random;;
public class DH_key {
static class DHGenParameterSpec implements AlgorithmParameterSpec{
static BigInteger p;
static BigInteger g;
static int a,b;
static BigInteger A,B;
static BigInteger getPrimeP_G() {
Random rand1= new Random(System.currentTimeMillis());
Random rand2= new Random(System.currentTimeMillis()*10);
p= BigInteger.probablePrime(32, rand1);
g= BigInteger.probablePrime(32, rand2);
System.out.println(""+p+","+g);
return null;
}
static int getExponent() {
SecureRandom ranGen1 = new SecureRandom();
a= ranGen1.nextInt(1000);
b= ranGen1.nextInt(1000);
System.out.println(a+"__"+b);
return 0 ;
}
public static Object pow (){
//g.pow(a);
A = g.pow(getExponent()).mod(getPrimeP_G());
B = g.pow(b).mod(p);
return null;
}
public static void main(String[]args){
//System.out.println(DHGenParameterSpec.getPrimeP_G());
DHGenParameterSpec.getPrimeP_G();
DHGenParameterSpec.getExponent();
A = g.pow(a).mod(p);
B = g.pow(b).mod(p);
BigInteger Sa,Sb;
Sa=B.pow(a).mod(p);
Sb=A.pow(b).mod(p);
System.out.println(""+A+"__"+B);
System.out.println(""+Sa+"__"+Sb);
}
}
}
Was the code above appropriate with java rules??
You have written modular exponentiation as:
A = g.pow(getExponent()).mod(getPrimeP_G());
B = g.pow(b).mod(p);
This is inefficient because the intermediate result from the exponentiation can be a large number. You should use the modPow method instead, which does the two operations with an efficient algorithm:
A = g.modPow(getExponent(), getPrimeP_G());
B = g.modPow(b, p);