How to user input here? - java

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.

Related

How to execute this java double scanner?

When calling scanDouble(); I get an error.
package com.company;
import java.util.Random;
import java.util.Scanner;
public class Main {
Scanner in = new Scanner(System.in);
public static void printLogarithm(double x){
if(x <= 0.0){
System.err.println("Error: x must be positive.");
return;
}
double result=Math.log(x);
System.out.println("The log of x is"+ result);
}
public static void scanDouble(Scanner in) {
System.out.print("Enter the name: ");
if (!in.hasNextDouble()){
String word = in.next();
System.err.println(word + "is not a number");
return;
}
double x = in.nextDouble();
printLogarithm(x);
}
public static void main(String[] args) {
scanDouble();
}
}
Does anyone know how to execute scanDouble(); Is there something I should include in the method call?
If I follow your logic, try passing the Scanner variable in inside the scanDouble() method like this:
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
scanDouble(in);
}
Passing a double value x for instance as an argument won't work, I just tested it.

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

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

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

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

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