Please, I need your help. Whenever I tried to run the below program, it will say incompatible types, String cannot be converted to Integer.
import javax.swing.JOptionPane;
public class Addition
{
public static void main(String[] args)
{
String num1 = (Integer)JOptionPane.showInputDialog("Enter num1");
String num2 = (Integer)JOptionPane.showInputDialog("Enter num2");
String sum =
(Integer)String.format("The sum is: %d", (num1 + num2));
JOptionPane.showMessageDialog(null, sum);
}
}
To expound on the other answers and show the code, here is what you need to make it work:
import javax.swing.JOptionPane;
public class Addition
{
public static void main(String[] args)
{
int num1 = Integer.parseInt(JOptionPane.showInputDialog("Enter num1"));
int num2 = Integer.parseInt(JOptionPane.showInputDialog("Enter num2"));
String sum = String.format("The sum is: %d", (num1 + num2));
JOptionPane.showMessageDialog(null, sum);
}
}
You want to take the input from the JOptionPanes as an ints then add them and put the result in a string, not cast a string to an int.
NOTE: I wrote this on my mobile, so I’ll compile and run it when I get home, but above is the general idea.
Your best bet is to take an int in the first place.
int num1 = Integer.parseInt(JOptionPane...
And you can either do the same with the output, or parse it again.
Its because your typecasting a String to an Integer and trying to store it in a String.
Remove (Integer) from the statement to get rid of that error.
Try this
int num1 = Integer.valueOf(JOptionPane.showInputDialog("Enter num1"));
int num2 = Integer.valueOf(JOptionPane.showInputDialog("Enter num2"));
String sum = String.format("The sum is: %d", (num1 + num2));
JOptionPane.showMessageDialog(null, sum);
It's seems you want, something like this:
public static void main(String[] args) {
Integer num1 = Integer.parseInt(JOptionPane.showInputDialog("Enter num1"));
Integer num2 = Integer.parseInt(JOptionPane.showInputDialog("Enter num2"));
String sum = String.format("The sum is: %d", (num1 + num2));
JOptionPane.showMessageDialog(null, sum);
}
Related
We have an assignment in class to create a greatest common divider (gcd) program using functions. I missed out on the lesson where we learned how to properly use them. I finished the part that actually does the division but I don't know how to separate it into a function and have it work. I'd like to have the input in the main class and the process in function.
This is what I have, it does not work when I run it
package gcd.function.java.program;
import java.util.Scanner;
/**
*
* #author sarah_000
*/
public class GCDFunctionJavaProgram {
public static void main(String[] args) {
int num1;
int num2;
int div;
Scanner input = new Scanner(System.in);
System.out.print("Enter your first number: ");
num1 = input.nextInt();
System.out.print("Enter your second number: ");
num2 = input.nextInt();
System.out.printf("The GCD is %d ", div);
}
public static void GCDFunction() {
if(num1 > num2)
div = num2;
else div = num1;
while((num1 % div!= 0)||(num2 % div != 0))
{
div --;
}//end of while loop
}
}
Any tips or help you can give to me will be greatly appreciated, I'm very new
You declare two parameters and modify the return type in your GCDFunction like this:
public static int GCDFunction(int num1, int num2)
You are currently trying to access the variables in the main method but are out of scope.
Also, you never actually call your GCDFunction
Think of it like passing functions in math. The GCDFunction() has to receive the numbers into the function so we do
public static void GCDFunction(int num1, int num2)
That also lets Java know the type it is, type int. And your java variables are scoped inside of the functions so you have to print the output in the function that created the variable in your scenario.
So once you have that function set up to receive the variables and output after processing, you call the function in the main with a
GCDFunction(num1, num2);
Where num1 and num2 are the variables that have your integers stored in.
The end result after a little rearranging looks like this.
import java.util.Scanner;
public class GCDFunctionJavaProgram {
public static void main(String[] args) {
int num1;
int num2;
Scanner input = new Scanner(System.in);
System.out.print("Enter your first number: ");
num1 = input.nextInt();
System.out.print("Enter your second number: ");
num2 = input.nextInt();
GCDFunction(num1, num2);
}
public static void GCDFunction(int num1, int num2) {
int div;
if(num1 > num2){
div = num2;
}
else{ div = num1;}
while((num1 % div!= 0)||(num2 % div != 0))
{
div --;
}//end of while loop
System.out.printf("The GCD is %d ", div);
}
Trying to give you a example of how the code should be to take in variable number of parameters.
public int gcd(Integer... numbers) {
int gcd = 1;
int miNNumber=Collections.min(Arrays.asList(numbers));
boolean isDivisible;
for(int i=2; i<=miNNumber;i++) {
isDivisible=true;
for(Integer eachNumber : numbers) {
if(eachNumber%i!=0) {
isDivisible=false;
break;
}
}
if(isDivisible) {
gcd=i;
}
}
return gcd;
}
You can call it
gcd(10, 200, 400);
or
gcd(10, 200, 400, 4000, 40);
I'm a beginner at Java programming. I encountered an error -
javax.swing.JComponent cannot be resolved whenever I execute the following program.
import javax.swing.JOptionPane;
class apples
{
public static void main(String args[])
{
String a = JOptionPane .showInputDialog("Enter a number");
String b = JOptionPane.showInputDialog("Enter another number");
int num1 = Integer.parseInt(a);
int num2 = Integer.parseInt(b);
int sum = num1 + num2;
JOptionPane.showMessageDialog(null,"Sum is "+sum, Answer,JOptionPane.PLANE_MESSAGE);
}
}
Can anyone please suggest a solution?
public static void main(String[] args) {
String a = JOptionPane .showInputDialog("Enter a number");
String b = JOptionPane.showInputDialog("Enter another number");
int num1 = Integer.parseInt(a);
int num2 = Integer.parseInt(b);
int sum = num1 + num2;
JOptionPane.showMessageDialog(null,"Sum is "+sum, "Answer",JOptionPane.PLAIN_MESSAGE);
}
You were using incorrect JOptionPane. You need to use PLAIN_MESSAGE instead of PLANE_MESSAGE. Also the Answer field which is the title needs to be a string
I suggest using an IDE like eclipse or netbeans so that you can avoid such issues.
I can't get this to run.There is a tester program and a method below. it says error identifier expected.Thanks in advance
public class 121tester{
public static void main(String[]args){
Scanner input= new Scanner(System.in)
System.out.println("Enter first number");
int num1=input.nextInt();
System.out.println("Enter second number");
int num2=input.nextInt();
System.out.println("The Greatest common factor of "+num1+" "+num2+" is "+GCD(num1,num2));
}
}
private static int GCD(int num1, int num2){
if(num2==0){
return num1;
}
return(GCD(num2, num1%num2);
}
Class name can't start with number. Change from
class 121tester
to
class Tester121
Another thing your GCD method should declare into inside class. It is better to use some IDE at initial stage of programming to remove compiler error.
Try the following:
import java.util.Scanner;
public class GCDTester{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter first number");
int num1 = input.nextInt();
System.out.println("Enter second number");
int num2 = input.nextInt();
System.out.println("The greatest common factor of " + num1 + " " + num2 + " is " + gcd(num1,num2));
}
private static int gcd(int num1, int num2){
if (num2 == 0) {
return num1;
}
return gcd(num2, num1 % num2);
}
}
But #Masud is correct, you should place the gcd method in a class of its own so that it can be used as an object in its own right.
I'm writing a program for my class where I have to use a for loop to takes two numbers from the keyboard. The program should then raise the first number to the power of the second number. Use a for loop to do the calculation. I'm getting the error that inum3 is not being initialized (I understand because the loop may never enter) but I cannot figure out how to make this work. Line 25 and 28 to be specific.
import javax.swing.*;
public class Loop2
{
public static void main(String[] args)
{
int inum1, inum2, inum3, count;
String str;
str = JOptionPane.showInputDialog("Please Enter a Numer");
inum1 = Integer.parseInt(str);
str = JOptionPane.showInputDialog("Please Enter a Numer");
inum2 = Integer.parseInt(str);
for (count = 1; count == inum2; count+=1)
{
inum3 = inum3 * inum1;
}
JOptionPane.showMessageDialog(null, String.format ("%s to the power of %s = %s", inum1,inum2, inum3), "The Odd numbers up to" + inum1,JOptionPane.INFORMATION_MESSAGE);
}//main
}// public
you need to initialize the variable inum3. As it stands right now, when your program tries to execute
inum3 = inum3 * inum1;
inum3 has no value, so it can't do the multiplication.
I think you want it to be 1 in this case.
So instead of
int inum1, inum2, inum3, count;
you can do
int inum1, inum2, inum3 = 1, count;
initialize num3 to one because you cand use something to define itself.
num3 = one;
import javax.swing.JOptionPane;
public class Loop2 {
public static void main(String[] args) {
int base, exp, result = 1;
String str;
str = JOptionPane.showInputDialog("Please Enter a Number");
base = Integer.parseInt(str);
str = JOptionPane.showInputDialog("Please Enter an Exponent");
exp = Integer.parseInt(str);
for (int count = 0; count < exp; count++) {
result *= base;
}
JOptionPane.showMessageDialog(null, String.format("%s to the power of %s = %s", base, exp, result),
"The Odd numbers up to" + base, JOptionPane.INFORMATION_MESSAGE);
}
}
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.