<identifier expected> when declaring Random [closed] - java

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 managed to get an error in just 3 lines of code:
import java.util.Random;
public class Test
{
private Random rnumber;
rnumber = new Random();
private int nmb = rnumber.nextInt(36);
}
The error is right after rnumber. I don't understand what is wrong.
If i change it to:
private Random rnumber = new Random();
private int nmb = rnumber.nextInt(36);
The error disappears, but when I add println:
private Random rnumber = new Random();
private int nmb = rnumber.nextInt(36);
System.out.println("" + nmb);
Same error comes back, but now right after println.

Put your code in some method, constructor or static initializer block, as required.
public class Test
{
public static void main (String[] args)
{
Random rnumber;
rnumber = new Random();
int nmb = rnumber.nextInt(36);
}
}
or
public class Test
{
private Random rnumber;
private int nmb;
public Test ()
{
rnumber = new Random();
nmb = rnumber.nextInt(36);
}
}

The only instructions you have the right to execute out of a block are declarations/initializations, that's why your code compiles when you remove some lines of code which are not declarations.
Put your logic in a method or a block and it will be fine.

Related

Passing array into a constructor and getting "cannot convert double[] to int" error [closed]

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'm trying to pass a simple array into a constructor of a class and kept getting a "cannot convert double[] to int" error. I'm really lost as to why.
import java.util.*;
public class Demo {
public static void main(String[] args) {
double[] data = {1,2,3,4,5,6,7,8,9,10,11,12};
Rainfall[] rain = new Rainfall[data];
}
}
import java.util.*;
public class Rainfall {
private double[] monthlyRain;
public Rainfall(double [] array) {
monthlyRain = array;
}
}
My IDE kept showing a red squiggly line underneath the "data" in
Rainfall[] rain = new Rainfall[data];
in the main method.
Use this instead...you are invoking constructor wrongly... with Rainfall[] rain = new Rainfall[data];
public class Demo {
public static void main(String[] args) {
double[] data = {1,2,3,4,5,6,7,8,9,10,11,12};
// Rainfall[] rain = new Rainfall[data]; wrong...
Rainfall[] rain = new Rainfall[]{new Rainfall(data) }; // correct
}
}
The thing is... Rainfal[] only holds an array of Rainfall objects...not one..
So thats..the correct way...sorry for error...

Why is the integer 0 when I create an object and get that integer value using getters and setters? [closed]

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 2 years ago.
Improve this question
public class Test {
public static void main(String[] args) {
Test2 t2 = new Test2();
t2.test();
System.out.println(t2.getNum());
}
}
public class Test2 {
private int num;
public void test() {
Test2 t = new Test2();
t.setNum(3);
System.out.println(t.getNum());
}
public int getNum() {
return num;
}
public void setNum(int bruh) {
this.num = bruh;
}
}
Whenever I create an object of Test2 and set the value of num using t2.test(), It prints out on the console as 0 when using t2.getNum(). Why does it return a value of 0? And how do I fix it so it actually gets the value I want? Class Test and Test2 are separate files as a side note.
public void test() {
Test2 t = new Test2();
t.setNum(3);
System.out.println(t.getNum());
}
You create a new instance of Test2 locally in the method, which has no impact on this instance.
public void test() {
setNum(3);
}
would be enough.
t2 and t or 2 different objects.
When you call t2.test(), you create a new object t and you set the attribute num of the t object, not t2. So when you call t2.getNum(), the num attribute of t2 keep is default value which is zero for integer in Java.

Sorting Arrays method not working [closed]

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.

Why doesnt string.equals("astring") work? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
Why don't I get 20 after delivering e.g. "Obsidian" to Test(String pStr) if I call getMatInt() ?
Also tried .toString() after all String-declariations, also declarated e.g. "Obsidian" as new String a. Nothing works.
getBonus is aways returning a 0 instead of a 20/30/... .
I already tried "Obsidian" and "obsidian", both doesnt work for me ...
public class test
{
private String str;
private int matInt;
private int bonus;
private int magic;
public test(int pMagic, String pStr)
{
int magic = pMagic;
str = pStr;
}
private void materialEquals()
{
if(str.equals("Obsidian"))
{
matInt = 20;
}
.....
}
private void calcBonus()
{
materialEquals();
bonus = magic * matInt;
}
public int getBonus()
{
calcBonus();
return bonus;
}
}
try:
public int getMatInt()
{
materialEquals();
return matInt;
}
There is no reason for this in your constructor: str = new String(pStr);, just use str = pStr.
In fact, you might be better off setting matInt in your constructor:
public test(String pStr)
{
str = pStr;
materialEquals();
}
And depending on how many materials you have, you might want to look into using enumeration.
Ok after your edits:
public int getBonus()
{
calcBonus(); //bonus won't be calculated otherwise
return bonus;
}
After further edits:
Your constructor is wrong, you're not initializing int magic. Try this constructor instead.
public test(int pMagic, String pStr)
{
this.magic = pMagic; //int magic = pMagic was a new variable only in the constructor scope
this.str = pStr;
calcBonus();
}
Also you might as well calculate the bonus on construction.
You will need to call materialEquals() so that 20 can be assigned to matInt upon equals comparison, as integers are always initialized to default value 0 upon declaration.

Is the code I wrote for my exercise efficient? [closed]

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);

Categories

Resources