"Can't find symbol" error in the java.math.BigInteger class - java

I'm trying to write code for a function that inputs String and returns its remainder when divided by 7 as an 'int'.
For some reason I'm getting the following error,
Main.java:16: error: cannot find symbol
n=java.math.BigInteger.bg.intValue();
^
symbol: variable bg
location: class BigInteger
1 error
My code is as follows,
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
int remainderWith7(String num)
{
// Your code here
java.math.BigInteger bg=new java.math.BigInteger(num);
Integer n=java.math.bg.intValue();
//int n=java.util.Integer.parseInt(num);
//hello
return (int)n%7;
}
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Ideone id=new Ideone();
id.remainderWith7("10");
}
}
Please help.
Thank You.

There is no attributes in math that's named bg.
Change the line to:
Integer n= bg.intValue();

import java.math.BigInteger;
import java.util.Scanner;
public class BigIntergerSumExample {
public static void main(String args[]) {
BigInteger number1;
BigInteger number2;
BigInteger sum;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the value of number 1");
number1 = sc.nextBigInteger();
System.out.println("Enter the value of number 2");
number2 = sc.nextBigInteger();
BigInteger a = new BigInteger(""+number1);
BigInteger b = new BigInteger(""+number2);
BigInteger result = a.add(b);
System.out.println("Sum is Two numbers : -> " + result);
}
}
Answer is
Enter the value of number 1
1111111111111111111111111111111111111111111111111
Enter the value of number 2
2222222222222222222222222222222222222222222222222
Sum is Two numbers : ->
3333333333333333333333333333333333333333333333333

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

How to user input here?

Here is the below code:
import comp102x.IO;
public class CalculatorEx01 {
public static void multiply() {
// Please write your code after this line
System.out.print("Enter an integer, x: ");
int x =IO.inputInt();
System.out.print("Enter an integer, y: ");
int y = IO.inputInt();
System.out.print("Answer = "+ (x*y));
}
}
And what does these errors mean?
[ERROR] cannot find symbol, symbol: method inputInt(), location: class comp102x.IO.
[ERROR] cannot find symbol, symbol: method inputInt(), location: class comp102x.IO.
How about using Scanner?
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
multiply();
}
public static void multiply() {
// Please write your code after this line
Scanner IO = new Scanner(System.in);
System.out.print("Enter an integer, x: ");
int x = IO.nextInt();
System.out.print("Enter an integer, y: ");
int y = IO.nextInt();
System.out.print("Answer = " + (x * y));
IO.close();
}
}
You are using a function (method)
inputInt()
which you haven't defined anywhere or isn't present in the import class which you have used , this what the error is trying to tell you.

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

How to format code containing random numbers into a service and support methods?

I'm just having trouble with a specific question. My task is to write a program (Rearrange this code) which displays a random integer within a specific range. The app class should read the high and low integer values required from the user. create an instance of the support class, and display the result of a call to the random range method. The support class should contain no date fields and just one method. I'm a real newbie and when I arrange it keep getting errors to do with the the randomRange not being resolved to a type. If anyone could rearrange this in a way that works and tell me what you did along the way, it'd be great. Thanks :)
My code:
import java.util.Scanner;
import java.util.Random;
public class RandomApp{
public static void main(String[]args){
int lo = readInt("Enter lowest value");
int hi = readInt("Enter highest value");
RandomRange r = new RandomRange();
System.out.println("Random integer between " + lo + " and " + hi + " : " + r.randomRange(lo, hi));
}
public static int readInt(String message){
Scanner sc = new Scanner(System.in);
System.out.println(message);
return sc.nextInt();
}
/** Returns an integer entered by the user*/
}
Another class:
import java.util.Scanner;
import java.util.Random;
public class RandomRange{
/** Returns random integer between high and low parameters.*/
public int randomRange(int low, int high){
Random generator = new Random();
return generator.nextInt(high-low+1) + low;
}
}
Looks like you haven't imported the class
Do the following after importing Scanner:
**import your.package.RandomRange;**
If you are using Eclipse then you can simply press ctrl+shift+o
Here is working code for me.(I did not check the logical part)
I have added few line to test it :
import java.util.Scanner;
import java.util.Random;
public class RandomApp{
public static void main(String[]args){
int lo = readInt("Enter lowest value");
int hi = readInt("Enter highest value");
RandomRange r = new RandomRange();
System.out.println("Random integer between " + lo + " and " + hi + " : " + r.randomRange(lo, hi));
for(int i=1;i<5;i++)
System.out.println(r.randomRange(lo, hi));
}
public static int readInt(String message){
Scanner sc = new Scanner(System.in);
System.out.println(message);
return sc.nextInt();
}
/** Returns an integer entered by the user*/
}
//import java.util.Scanner;
//import java.util.Random;
class RandomRange{
/** Returns random integer between high and low parameters.*/
public int randomRange(int low, int high){
Random generator = new Random();
return generator.nextInt(high-low+1) + low;
}
}
Compile : javac RandomApp.java
Run : java RandomApp

Java: "error: cannot find symbol"

(Rookie mistake, I'm sure.)
I'm a first year computer science student, and attempting to write a program for an assignment, with the code;
import java.util.Scanner;
public class Lab10Ex1 {
public static void main(String[] arg) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Please type a number: ");
int n = keyboard.nextInt();
calcNumFactors();
}
public static void calcNumFactors(){
System.out.print(n + 1);
}
}
But upon compiling, I get the error;
Lab10Ex1.java:10: error: cannot find symbol System.out.print(n +
1);
^
symbol: variable n
location: class Lab10Ex1
If someone could explain to me what I've done wrong, or how to fix it, I would greatly appreciate it.
The n variable was declared in the main method and thus is visible only in the main method, nowhere else, and certainly not inside of the calcNumFactors method. To solve this, give your calcNumFactors method an int parameter which would allow calling methods to pass an int, such as n into the method.
public static void calcNumFactors(int number) {
// work with number in here
}
and call it like so:
int n = keyboard.nextInt();
calcNumFactors(n);
You must declare the variable n in public static void calcNumFactors()
In your code, you have to pass the value of n as an argument to the function calcNumFactors() as Hovercraft Full Of Eels said.
import java.util.Scanner;
public class Lab10Ex1 {
private static int n;
public static void main(String[] arg) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Please type a number: ");
n = keyboard.nextInt();
calcNumFactors();
}
public static void calcNumFactors(){
System.out.print(n + 1);
}
}
In my case, I copied an Enum file from a Grails (.groovy) project and forgot to change the extension to .java

Categories

Resources