Identifier expected in java - java

I wrote a simple program in java:
public class Will {
int static square(int x) {
int y = x * x;
return y;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a number...");
int n = sc.nextInt();
int result = Will.square(n);
System.out.println("Square of " + n + " is " + result);
}
}
When I try to compile it, I'm getting those errors:
square.java:6: error: <identifier> expected
int static square (int x)
^
square.java:6: error: invalid method declaration; return type required
int static square (int x)
^
2 errors
How to resolve those?

It has to be:
static int square (int x)
i.e the return-type has to be after the access-modifiers and before the method name.

Change it like
public/private/protected(Access specifier) static int square(int x)
as per JAVA standard rules

Related

My Java code is showing a compiler error when I create method

package aj;
import java.util.Scanner;
public class ConvertingNumber {
public static void main(String[] args) {
Scanner a = new Scanner(System.in);
System.out.println("Enter the Number");
int num = a.nextInt();
System.out.println("Enter the base for the given number");
int base = a.nextInt();
converting(num,base);
public static int converting(int num , int base) {
String sum="";
while(num > 0) {
int rem = 0;
rem = num % base;
num = num / base;
sum = rem + sum;
}
System.out.println(sum);
}
}
}
for my above java code,am getting compier error saying:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method converting(int, int) is undefined for the type ConvertingNumber
void is an invalid type for the variable converting
Syntax error on token "(", ; expected
Duplicate local variable num
Syntax error on token ",", ; expected
Duplicate local variable base
Syntax error on token ")", ; expected
at aj.ConvertingNumber.main(ConvertingNumber.java:12)
Please anyone help me out in solving this. Thanks in advance.
converting method should be out side from the main method
package aj;
import java.util.Scanner;
public class ConvertingNumber {
public static void main(String[] args) {
Scanner a=new Scanner(System.in);
System.out.println("Enter the Number");
int num=a.nextInt();
System.out.println("enter the base for the given number");
int base=a.nextInt();
converting(num,base);
}
//This method should be out side the main method
public static void converting(int num , int base) {
String sum="";
while(num>0) {
int rem=0;
rem=num%base;
num=num/base;
sum=rem+sum;
}
System.out.println(sum);
}
}
I have Corrected Your code..
package aj;
import java.util.Scanner;
public class ConvertingNumber {
public static void main(String[] args) {
Scanner a=new Scanner(System.in);
System.out.println("Enter the Number");
int num=a.nextInt();
System.out.println("enter the base for the given number");
int base=a.nextInt();
ConvertingNumber .converting(num,base);
//converting(num,base);
}
public static int converting(int num , int base)
{
int sum=0;
while(num>0)
{
int rem=0;
rem=num%base;
num=num/base;
sum=rem+sum;
}
//System.out.println(sum);
return sum;
}
}

Eclipse outputting strange string at end of code

Recently I began noticing that at the end of every code I run through Eclipse, it will end with an anomaly:
[0x7FFABD5170E3] ANOMALY: use of REX.w is meaningless (default operand size is 64)
If seeing the entire code helps, here it is:
import java.util.Scanner;
public class FracCalc {
static Scanner sc = new Scanner(System.in);
public static int inputA(){
System.out.print("Integer A: ");
int x = sc.nextInt();
return x;
}
public static int inputB(){
System.out.print("Integer B: ");
int x = sc.nextInt();
return x;
}
public static double division(double n, double d){
double a = (n / d);
return a;
}
public static void main(String[] args) {
int numerator = inputA();
System.out.println("Divided by...");
int denominator = inputB();
double answer = division(numerator, denominator);
System.out.println(answer);
}
}
If someone could help explain why this is happening that would be very appreciated.

How am I able to call a method that returns a value from a different method?

I am trying to call a method that has a value that comes from a different method but modified. Here is the code:
import java.util.Scanner;
public class Test{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int num;
System.out.println("Please enter an integer");
num = in.nextInt();
System.out.println(Multiply(num));
System.out.println(Multiply2(x));
}
public static int Multiply(int num){
int x = num*2;
return x;
}
public static int Multiply2(int x){
int val = x*2;
return val;
}
}
I know I have to declare x inside of main but then I have to initialize it but I want the value of x to equal to the one that equals to the Multiply2 method which multiplies the num from the method, Multiply, by 2. x would be equal to num multiplied by 2; How am I able to do this?
Your methods are exactly the same
The name of the variable doesn't matter. Wheter its called x or val, it makes no difference.
With that in mind, if you want x to be the value returned from your Multiply function, just store that in a variable and use it later. For instance,
int someValue = Multiply(num);
System.out.println(someValue);
System.out.println(Multiply2(someValue));
PS: By convention, we don't capitalize methods names. So they should be multiply and multiply2
import java.util.Scanner;
public class Test {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int num;
System.out.println("Please enter an integer");
num = in.nextInt();
System.out.println(Multiply(num));
System.out.println(Multiply2(Multiply(num)));
}
public static int Multiply(int num) {
int x = num * 2;
return x;
}
public static int Multiply2(int x) {
int val = x * 2;
return val;
}
}

Displaying "stacked" and added exponents in Java

So I have been asked to create a program that can evaluate and print the value of...
0.1 + (0.1)^2 + (0.1)^3 . . . + (0.1)^n
using a while loop. So far I have
import java.util.Scanner;
class Power
{
public static void main(String[] args)
{
Double x;
Scanner input = new Scanner(System.in);
System.out.println("What is the maximum power of 0.1?");
x = input.nextLine;
Double n = 0.1;
Int exp = 1;
while (exp <= x)
{
Double Answer = Math.pow(n, exp); //Had to look this one up
exp++;
}
System.out.print(Answer);
}
}
I'm still having trouble trying to decode the following few Compile-time errors I am getting with this program.
Power.java:11: error: cannot find symbol
x = input.nextLine;
^
symbol: variable nextLine
location: variable input of type Scanner
Power.java:13: error: cannot find symbol
Int exp = 1;
^
symbol: class Int
location: class Power
Power.java:19: error: cannot find symbol
System.out.print(Answer);
^
symbol: variable Answer
location: class Power
Any fix? Thanks guys
~Andrew
Here you go:
import java.util.Scanner;
class Power
{
public static void main(String[] args)
{
Double x;
Scanner input = new Scanner(System.in);
System.out.println("What is the maximum power of 0.1?");
x = input.nextDouble(); //Use nextDouble to take in double
Double n = 0.1;
int exp = 1;
Double Answer = 0.0; //You have to declare Answer outside of the while loop below or else Answer will be undefined when you try to print it out in the last line.
while (exp <= x)
{
Answer = Math.pow(n, exp);
exp++;
}
System.out.print(Answer);
}
}

Why do I get a DivideByZeroException in my Java code?

import java.io.IOException;
import java.util.*;
public class calling {
public static String s;
public static String t;
public static int y;
public static int x;
public static int num1() {
int x;
Scanner scanner = new Scanner (System.in);
System.out.println("Please enter a number called x: ");
x=scanner.nextInt();
return x;
}
public static int num2() {
int y;
Scanner scanner = new Scanner (System.in);
System.out.println("Please enter a second number called y: ");
y=scanner.nextInt();
return y;
}
public static void calculation() {
Scanner input = new Scanner(System.in);
System.out.println("What process would you like to do? *, /, + or - ?");
s=input.next();
if (s.equals("*")) {
System.out.println("\nThe product of these numbers is:" + (x*y));}
else
if (s.equals("+")) {
System.out.println("\nThe sum of these numbers is: " + (x+y));}
System.out.println("\nDo you want x or y to be the dividor/subtractor?: ");
t=input.next();
if (t.equals("y") || t.equals("Y") ) {
if (s.equals("/")) {
System.out.println("\nThe quotient of these numbers is: " + (x/y));}
else
if (s.equals("-")) {
System.out.println("\nThe difference of these numbers is: " + (x-y));}}
else
if (t.equals("x") || t.equals("X")){
if (s.equals("/")) {
System.out.println("\nThe quotient of these numbers is: " + (y/x));}
else
if (s.equals("-")) {
System.out.println("\nThe difference of these numbers is: " + ((y-x)));}}
}
public static void main (String [] args) throws IOException {
num1();
num2();
calculation();
}
}
i keep getting this error in what should be my final result which is simply the result of the calculations being performed
this is the error:" Exception in thread "main" java.lang.ArithmeticException: / by zero
at calling.calculation(calling.java:44)
at calling.main(calling.java:64)"
Since this is likely homework, I'll give you a hint to point you in the right direction.
When you run your program, you execute num1 and num2 to collect the values of x and y from the user. Within num2, y is declared as a local variable. What happens to that variable when num2 returns? And what does that imply for the class field (variable) y declared on line 7?
This is also a good time to learn how to use a debugger. Put a breakpoint on line 44, and see what the values of x and y are.
You need to make sure that the:
int x;
int y;
are the ones you want. Integer's default to zero when static.

Categories

Resources