Math.random() not being recognized [closed] - java

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
Why is my code giving me a compile error saying that it can not find random()?
public class Math {
public static void main(String[] args) {
int randnum;
randnum = (int)(Math.random() * 15);
while (true) {
System.out.println(randnum);
}
}
}

You named your class Math, which shadows java.lang.Math. Your Math class has no random() method, hence the error. Pick a different name for your class to resolve the issue.

Either call the function as
randnum = (int)(java.lang.Math.random() * 15);
or rename your class to something else, not Math- it hides the inbuilt java package.

Change the name of your own class Math to some other name, You class name conflict with java.lang.Math class name :
public class XYZ {
public static void main(String[] args){
int randnum;
randnum=(int) (Math.random()*15);
while (true){
System.out.println(randnum);
}
}
}
or you should use randnum = (int)(java.lang.Math.random() * 15);

Related

Why does terminal tell me to change main to string [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 1 year ago.
Improve this question
This is the program I'm trying to run
public class PracticeTwo {
public static void main(int[] args){
int[] lower = {-4,-3,-7};
for(int i=0; i<0; i++){
int[] greater = {5,2,6};
for(int j=0; j>0; j++){
if(greater.length>0);
System.out.println(Integer.toString(j) + " is greater than 0");
if(lower.length<0);
System.out.println(Integer.toString(i) + " is lower than 0");
}
}
}
}
There is no compiling issues but when i try to run it returns this:
Error: Main method not found in class PracticeTwo, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
The problem is in the declaration of your main method, as the error description reads, it should be:
public static void main(String[] args)
Pay close attention to the errors and warnings, they exist for a reason.
This is how it should be listed:
public static void main(String[] args){
System.out.println("Hello World!");
}
Anything inside this method will run when you run the program.

How to access a variable from a function in Java? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
there,
I'm a beginner in Java. I got a problem. I don't know how to access to a result to work with it. Like so:
Main file:
public class App
{
public static void main(String[] args)
{
test t = new test();
t.add(10, 20);
int result = test.sum; // accessing variable
System.out.println("sum = "+result);
}
}
test.java:
class test
{
static int sum;
public static int multi;
void add(int a, int b)
{
sum = a+b;
}
void multi()
{
//I'd like to work with the result above "sum"
}
}
I tried to make a getter and setter but it didn't work. I think I made some mistakes.
Can you help me please. Thank's in advance.
You shouldn't have any fields in test, much less static ones. The usual way to structure this API would be
class test
{
int sum(int a, int b)
{
return a+b;
}
int multi(int a, int b)
{
return a * b;
}
}
If you need to use sum in multi, then you should call it instead of getting the stored result.

Where is the solution stub in this program and how would I call a method in it? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
This is a specific question, don't downvote it just because it doesn't help you.
public class Answer {
public static String answer(int n) {
String nums="";
int limit = 10005;
int x=2;
while(limit>0){
if(isPrime(x)){
limit-=String.valueOf(x).length();
nums = nums + String.valueOf(x);
}
x+=1;
}
String out="";
if(n==0){
out="23571";
}else{
for(int i=1;i<6;i++){
out += String.valueOf(nums.charAt(n+i));
}
//Problem Solved: instead of this loop, it should be out = nums.substring(n,n+5)
}
return out;
}
public static boolean isPrime(int number) {
for(int check = 2; check < number; ++check) {
if(number % check == 0) {
return false;
}
}
return true;
}
}
Nothing is wrong with this code as far as I know, I'm just using it as an example for you to use.
"It must implement the answer() method in the solution stub." was in the directions for me, but I don't know much about the vocabulary of programming, I only understand logic behind programming, so this is the only thing I don't know how to solve. So what I am asking is where do I put the "answer()" at in this program?
It was looking for substring, which I didn't include because I haven't used java in about a year and simply forgot about it.
Here as I can figure out you have problems in understanding the meaning of "stub". It is simply the test method as provided by the answer here. And if you want to test the above code you have to implement the main method in your code to do the same. Something like this
public static void main(String [] args){
//Either use Scanner object or provide the hard coded input as per your requirements
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.println(answer(n));
}
EDIT AS PER OP REQUIREMENT
Okay so as per your requirement it is asking you for the unit test. There are many ways to do it but my preferred is to make stub concrete class
Implementation an stub concrete class in JUNIT
class Answer {
public String answer(int n){
// Code body
return "result"// in your case out variable
}
}
class solution extends Answer {
#Override
public String answer(int n){
//return "your stubbed result";
}
}

Why call to " this " must be first statement in constructor? [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 6 years ago.
Improve this question
I am trying to execute this below code sample to understand why call to " this " must be first statement in constructor ?? I had read lot of About it and I understand Why this is so !!
so I write the below simple program But Still showing me the same Error even I use 'this' as a First Statement in my Program .
import java.io.*;
import java.lang.*;
class Demo
{
int x=23;
Demo()
{
this(55);
}
Demo(int x)
{
this.x=x;
System.out.println("Inside Parameterise Constructor 2"+"\n Value of x:"+x);
}
}
class ThisDemo
{
public static void main(String []args)
{
Demo obj = new Demo();
}
}
To specifically answer your question, this or super needs to be the first call to ensure the base class has been setup correctly.
https://stackoverflow.com/a/1168356/154186
To solve your error above, remove the void type from the function call. e.g.:
Demo(int x) {
this.x = x;
}
Demo() {
this(50);
}
Remove void from Demo constructors
class Demo
{
int x=23;
Demo()
{
this(55);
}
Demo(int x)
{
this.x=x;
System.out.println("Inside Parameterise Constructor 2"+"\n Value of x:"+x);
}
}
You should remove void.Constructor must have no explicit return type.
Then it will work fine.

Vector Method Java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 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 am trying to create a vector method called readfromfile which would potentially read the input from a different text file. Why does it give an error?
Edit: Thanks for the help, I have edited the code and it works!
Looks like I was confusing parameters and methods! :P
Thanks guys :D
package cas.lab1.firsteclipsePackage;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Vector;
public class FirstEclipseClass {
public static void main(String[] args) {
Vector input = new Vector();
input.add("A");
input.add("B");
input.add("C");
input.add("D");
printVectorElements(input, 3);
Vector<String> results = readFromFile();
}
public static void printVectorElements(Vector input, int count) {
for (int i = 0; i < count; i++) {
System.out.println(input.get(i));
}
}
public static Vector<String> readFromFile(){ //yeah I did confuse methods and parameters
Vector<String> result = new Vector<String>();
try{
File f = new File("input.txt");
Scanner s = new Scanner(f);
while(s.hasNextLine()) {
int i = s.nextInt();
if(i % 2 == 0)
result.add("Even");
else
result.add("Odd");
System.out.println(i);
}
s.close();
}
catch(FileNotFoundException e){
e.printStackTrace();
}
return result;
}
}
I guess that you are confused here. From your method call, I see that you don't need to pass any parameters, and instead want a Vector back. So I suggest you to change this line:
public static readFromFile(Vector<String> results){
To this line:
public static Vector<String> readFromFile(){
First thing: you didn't specified the return type. You should have :
public static Vector<String> readFromFile()
if you do not need any parameters in the function.
Second, for future, you cannot have this same name in the function and as a function parameter

Categories