Why doesnt my code compile, wanted to parseInt Fibonacci [duplicate] - java

This question already has answers here:
What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?
(18 answers)
Closed 2 years ago.
I created a code for Fibonacci numbers, but it doesn't compile and I don't know why, can someone help.
(And I also think that the last like with k-1 + k-2 doesn't make any sense.
public class Fibonacci {
public static void main(String[] args) {
int n = Integer.parseInt(args[n]);
System.out.println(FibNum(n));
}
public static int FibNum(int k) {
if (k == 0) return 0;
if (k == 1) return 1; else return k - 1 + k - 2;
}
}

int n = Integer.parseInt(args[n]); how could it work? variable n in not initialized

Related

How to pass an ArrayList to a function that swaps similar to java array? [duplicate]

This question already has answers here:
What is the reason behind "non-static method cannot be referenced from a static context"? [duplicate]
(13 answers)
Closed 7 months ago.
For a Java Array, we can swap the elements present at indexes first and second like this:
public void func(int[] array) {
int first = 1;
int second = 2;
swap(array, first, second);
}
public void swap(int[] array, int first, int second) {
int tmp = array[first];
array[first] = array[second];
array[second] = tmp;
}
How to do the same thing if array is an ArrayList?
Here is what I tried:
public void swap(ArrayList<Integer> array, int first, int second){
int tmp = array.get(first);
array.set(first, array.get(second));
array.set(second, tmp);
}
but it throws the following error:
error: non-static method swap(ArrayList,int,int) cannot be referenced from a static context
swap(A, i, j);
As the error messages says try turn swap methood into static.

I was doing a Distinct Values java Program. I attempt on the Error [duplicate]

This question already has answers here:
"Missing return statement" within if / for / while
(7 answers)
Closed 5 years ago.
This a Method I am using with my Program
static int check(int pos) {
int i, flag = 0;
for (i = 0; i < pos; i++) {
if (a[pos] == a[i]) {
flag = 1;
return 1;
}
}
if (flag == 0)
return 0;
}
When I compile it, I get the following Error:
Distinct.java:16: error: missing return statement
}
^
1 error
what would happen when neither
if (a[pos] == a[i])
nor if (flag == 0) conditions are met?
then you have a code that is not covering all possible cases, your method have to return something no matter which condition met!!

recursion + bug at my main [duplicate]

This question already has answers here:
What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?
(18 answers)
Closed 5 years ago.
I'm using java bluejay trying to solve a recursion problem.
The problem : given an array of natural numbers (higher then 0)
and an int, the method needs to tell if the array consist of a way to sum up the numbers to get the given int.
example {4,5} int = 13 >> 4+4+5 = 13 = true
example {4,5} int = 3 >>> false.
The 2nd problem is that when i use my main it says "cannot find symbol - method
isSumOf(int[],int) although i do have this kind of method...
Here is my classes:
public class Ex14
{
static int allways;
static int index = -1;
public static boolean isSumOf(int[] s, int n){
if(index == -1) index = s.length - 1;
return isSumOf(s, n, index);
}
private static boolean isSumOf(int[] s, int n, int index){
int temp = s[index];
if(allways == n) {return true;}
if(allways > n && index != 0) {allways -= temp + s[index - 1];
if(allways / s[index - 1] == s.length) index --;}
if(allways > n && index == 0) return false;
if(allways < n) allways += temp; return isSumOf(s, n);
}
}
My main class:
public class bdikot
{
public static void main(String[] args){
int [] hi= new int[1];
hi[0]=3;
System.out.println(isSumOf(hi,7));}
}
Thanks in advance Asaf :-) .
For your second problem, I think you need to change:
System.out.println(Ex14.isSumOf(hi,7));
Since isSumOf exists in a different class than your main method.
Also you might want to take a look at:
https://en.wikipedia.org/wiki/Programming_style
https://google.github.io/styleguide/javaguide.html
Since following these will make your code easier to read for others.

JAVA: What is the difference between a logical AND/OR and short circuit AND/OR? [duplicate]

This question already has answers here:
What is the difference between & and && in Java?
(15 answers)
Closed 6 years ago.
When I run a simple program, both of them ( & and && ) seem to work interchangeably. Could someone please explain when I should use each case?
public class Test1 {
public static void main(String [] args){
int counter = 0;
int dice1;
int dice2;
for (int loop = 0; loop <= 1000; loop++) {
dice1= (int) (Math.random()*6)+1;
dice2= (int) (Math.random()*6)+1;
if (dice1 == 6 && dice2 == 6){
counter ++;
}
}
System.out.println("Counter = " + counter);
}
}
This program also seems to work when I use & instead of &&. So what's the difference?
The main difference is that the && and || operators do some kind of "lazy evaluation"
In this line of code:
method1() & method2();
both methods will be called independant of the return value of method1. On the other hand in this line of code:
method1() && method2();
the method2 will only be called if method1 returns true.
When using || the second method will only be called when the first method returns false.

Trying to find largest palindrome, variable not updated [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
So I'm trying to find the largest product of 2 3-digit numbers that is a palindrome. Here's my code:
class project_euler4 {
public static int largest_palindrome = 0;
public static boolean isPalindrome(int number) {
String original = Integer.toString(number);
String reversed = new StringBuffer(original).reverse().toString();
return (original == reversed) ? true : false;
}
public static void main(String[] args) {
for (int i = 100; i < 1000; i++) {
for (int j = 100; j < 1000; j++) {
int candidate = i * j;
if (isPalindrome(candidate) && candidate > largest_palindrome) {
largest_palindrome = candidate;
}
}
}
System.out.println("The largest palindrome made from the product of 2 3-digit numbers is " + largest_palindrome);
}
}
When I compile and run, I get:
The largest palindrome made from the product of 2 3-digit numbers is 0
So for some reason, my largest_palindrome variable isn't getting updated when a product is a palindrome. I suspect it has something to do with my isPalindrome() function, but am not sure.
Any ideas?
Thanks for the help,
Mariogs
You used == instead of .equals() to compare the strings. Classic mistake.

Categories

Resources