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.
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'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...
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.
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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
Closed 9 years ago.
Improve this question
class Demo2
{
int i=1,j=2;
void fun1()
{
i=i+1;
j=j+1;
}
public static void main(String[] args)
{
Demo2 d1=new Demo2();
d1.fun1();
d1.fun1();
d1.fun1();
System.out.println("Hello World!");
}
}
symbol cannot find and func cant be applied errors are showing Please help me i am a basic learner....
There are several errors in the code. I've commented and suggested some working code.
class Demo2
{
void fun1()
{
i=i+1; //i has not been initialized
j=j+1; //j has not been initialized
}
public static void main(String[] args)
{
Demo2 d1=new Demo2();
d1.fun1(1);//"fun1" does not accept a parameter.
d1.fun1();
d1.fun1();
System.out.println("Hello World!");
}
}
Here is a working Demo2 class that may help you along:
class Demo2
{
int i = 0;
int j = 0;
public void fun1(int param)
{
i=i+param;
j=j+param;
}
public static void main(String[] args)
{
Demo2 d = new Demo2();
d.fun1(1);//adds 1 to both i and j
d.fun1(2);//adds 2 to both i and j
System.out.println("i is equal to " + i);
System.out.println("j is equal to " + j);
}
}
You Does not declare i and j and doesnot defined a method fun1 with one argument
It's very important to understand why your code does not compile, or you will not be able to do anything else even if it gets fixed.
For starters, declare i and j by putting them right under your class declaration
class Demo2 {
int i = 0;
int j = 0;
Now look at where you defined void fun1(). You don't allow it to accept any parameters. So d1.fun1(); is allowed but d1.fun1(1); is not. You did not define a function named fun1 that accepts an int as a parameter. You would have to define it like so:
void fun1(int input) {
// Whatever you wanted this function to do
}
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.