I am VERY new to Java (ie week 2 of AP Computer Science A). we have to use JOptionPane to make message boxes to use with math operations. My code was fine at school but I had to reformat it because I saved it on Facebook (no other option). Now I get errors on all the JOptionPane lines saying "Multiple markers at this line" and " Syntax error on tokens".
How do I fix that. here is the code (please only fix what I am asking, nothing else, I know the code is probably weird)
import javax.swing.JOptionPane;
public class OptioPane {
public static void main(String[] args) {
}
String a = JOptionPane.showInputDialog("Enter an integer");
String b = JOptionPane.showInputDialog("Input another");
int x = Integer.parseInt(a);
int y = Integer.parseInt(b);
JOptionPane.showMessageDiaglog(null, "The numbers added together is " +(x+y));
String c = JOptionPane.showInputDialog("Enter an integer");
String d = JOptionPane.showInputDialog("Input another");
int f = Integer.parseInt(c);
int g = Integer.parseInt(d);
JOptionPane.showMessageDialog(null, "The second number subtracted from the first number is " +(f-g));
String s = JOptionPane.showInputDialog("Enter an integer");
String r = JOptionPane.showInputDialog("Input another");
int w = Integer.parseInt(a);
int q = Integer.parseInt(b);
JOptionPane.showMessageDialog(null, "The numbers multiplied together is " +(w*q));
String k = JOptionPane.showInputDialog("Enter an integer");
String j = JOptionPane.showInputDialog("Input another");
int n = Integer.parseInt(a);
int m = Integer.parseInt(b);
JOptionPane.showMessageDialog(null, "The first number divided by the second number is " +(n/m));
String fir = JOptionPane.showInputDialog("Enter an integer");
String tir = JOptionPane.showInputDialog("Input another");
int ah = Integer.parseInt(a);
int bh = Integer.parseInt(b);
JOptionPane.showMessageDialog(null, "The first number modulated by the second number is " +(ah*bh));
}
Basically, the body of your code is out side of any executable context (not where it belongs)....
public class OptioPane {
public static void main(String[] args) {
}
/* All your stuff - out of bounds and behaving badly */
}
Instead, you need to place this code within a executable context, such as the main method...
public class OptioPane {
public static void main(String[] args) {
/* All your stuff - playing nicely */
}
}
Oh, add the this line...
JOptionPane.showMessageDiaglog(null, "The numbers added together is " +(x+y));
Is wrong, see the extra g in Dialog, it should be...
JOptionPane.showMessageDialog(null, "The numbers added together is " +(x+y));
It seems that you did something wrong while re-formatting the code because the entire code block should be inside the curly braces of the main method
I saw your comment about multiplication and if you still have problems with it, it's because of this
String s = JOptionPane.showInputDialog("Enter an integer");
String r = JOptionPane.showInputDialog("Input another");
int w = Integer.parseInt(a);
int q = Integer.parseInt(b);
JOptionPane.showMessageDialog(null, "The numbers multiplied together is " +(w*q));
when you input 5 into 's' and 5 into 'r' and get 30, you probably had 5 and 6 in 'a' and 'b' respectively.
Try this:
import javax.swing.JOptionPane;
public class Dialogue1 {
public static void main(String[]arg)
{
String number1,number2;
number1=JOptionPane.showInputDialog("enter first number");
number2=JOptionPane.showInputDialog("enter second number");
int num1=Integer.parseInt(number1);
int num2=Integer.parseInt(number2);
int sum=num1+num2;
String message=String.format("the sum is %d",sum);
JOptionPane.showMessageDialog(null,sum);
}
}
Store your result in another variable and then display it. It will work for sure without any error.
If you face any issue I guess there are some suggestions given below the error in Eclipse. I have also encountered the same error it suggested me to import the java swing package then it got fixed.
Related
** I am Trying to print this pattern in JAVA
a/2 + a/3 + a/4 ....+ a/n
**
Here is my java code
import java.util.*;
public class ser
{
public static void main(String args[]){
Scanner in = new Scanner(System.in);
System.out.println("Enter the value of a and n");
int a,i,n;
double s=0;
a=in.nextInt();
n=in.nextInt();
for(i=1;i<n;i++)
s=s+(double)a/(i+1);
System.out.println("ssss = "+s);
}
}
I'm using Kali linux and today I'm facing this error while running my java code in terminal
Enter the value of a and n
a 12
Error:
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at ser.main(one.java:9)```
There is no problem1 with your code.
The problem is with the input that you are giving to the program.
Assuming that you are running from the command line, the way to compile and run the program is like this.
steve#greybeast:/tmp$ javac ser.java
steve#greybeast:/tmp$ java ser
Enter the value of a and n
2 12
ssss = 4.206421356421357
steve#greybeast:/tmp$
See?
When it asks me to "enter a and n", I type 2 12 <ENTER>.
And it works (as far as I can tell).
What you have been doing is entering letters, numbers in quotation marks and other things. But the program as you have written it2 expects only 2 integers with whitespace between them. And when it gets something that it doesn't expect, the nextInt() call throws an InputMismatchException.
1 - Actually, there is a problem. Your code ignores a number of important Java style conventions. For example, class names should NOT start with a lowercase letter, and code should be correctly indented. But these things only matter to humans who have to read your code.
2 - I hope that >you< wrote it. 'Cos if you copied it rather than writing it yourself, you are cheating yourself of the opportunity to learn to program.
Issue is with your input. Code is expecting integer value as input and you are providing 'a'. You need to provide integer.
You don't need to provide a and then 12, just provide 12, press enter and then provide the next Integer. Right now I think it's trying to decode a as an integer. Just provide the integers in order of their input and it should work.
brother!! just hardcore "a" to the print statement instead for taking it as an input.
may be this code will help u
import java.util.*;
public class ser
{
public static void main(String args[]){
Scanner in = new Scanner(System.in);
System.out.println("Enter the value of n");
int i,n;
n=in.nextInt();
for(i=2;i<=n;i++)
{
System.out.print("a/"+i);
if(i==n)
break;
else
System.out.print(" + ");
}
}
}
To clear confusion, I modified your code as below.
Provide your input in two separate lines.
import java.util.*;
public class ser
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter the value of a and n");
int a, i, n;
String a1, n1;
double s = 0;
a1 = in.nextLine(); // Reading as string value the complete line
n1 = in.nextLine();
// checking whether the value acquired is number or not
if (!isNumeric(a1.toString())) {
System.out.println("Please provide a valid number without alphbets or symbol, you have typed:" + a1);
System.exit(0);
}
if (!isNumeric(n1.toString())) {
System.out.println("Please provide a valid number without alphbets or symbol, you have typed:" + n1);
System.exit(0);
}
a = Integer.parseInt(a1);
n = Integer.parseInt(n1);
for (i = 1; i < n; i++)
s = s + (double) a / (i + 1);
System.out.println("ssss = " + s);
}
// will check whether the string passed is number or not
public static boolean isNumeric(String strNum) {
if (strNum == null) {
return false;
}
try {
double d = Double.parseDouble(strNum);
} catch (NumberFormatException nfe) {
return false;
}
return true;
}
}
I am trying to write a program for a coding problem for class. I was thinking of doing a while loop asking for the list of primes and the somehow comparing them to a string. Down below is my code. I know its rough. Any one pointing me in the right direction would help.
public static void godel(){
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a list of primes: ");
System.out.println("0 to stop loop ");
int input =0;
int count = 0;
//string 0 = int 1;
//string f = int 3;
//string -\ = int 5;
//string V = int 7;
//string \-/ = int 9;
//string ( = int 11;
//string ) = int 13;
//string /\ = int 15;
//string 3 = int 17;
//string = = int 19;
//string x = int 21;
//string y = int 23
while(true){
input = scanner.nextInt();
if (input != 0)
{
input += count;
}
else{
System.out.println("Here are your list of primes in Godel's Number");
System.out.println(count);
break;
}
} }
I assume nothing is working?
first off, you need "public static void main(String[] args)" for anything to pop up. The code you want to execute (not any of the helper functions or math, just want you want to print) should be within that statement as such
public static void main(String[] args)
{
//code you want to execute here
}
I'm not sure what the godel stuff is, also you need to import the scanner library like this at the top of the code:
import java.util.Scanner;
I don't know if you need to hear any of this, but this looks like a runner because your printing out a lot of stuff and you have a scanner. I was confused as to why there was no args statement or library but I guess you maybe just didn't include it.
I no nothing about number theory so I can't help with that part.
Hello I am trying to produce an If statement that produces a message if a user doesnt input 'M' or 'l'. If 'M' or 'l' has been inputted then the answer (4 or 5) should be displayed.
Whenever I input a letter an error pops up and if i input any number "Pizza size must be M (medium) or l (large):" message pops up. I want the to only input letters not numbers.
apologies if i havent explained the question correctly.
public static void main(String args[]) {
char invalid;
double M, l;
Scanner pizzasize = new Scanner(System.in);
double fstep = 0, sstep = 0, tstep = 0, ostep = 0, lstep = 0;
M = 4;
l = 5;
System.out.println("Enter pizza size:");
fstep = pizzasize.nextDouble();
if(fstep != M|| fstep != l ){
System.out.println("Pizza size must be M (medium) or l (large):");
}else{
String input = pizzasize.next();
if(fstep == M||fstep ==l){
fstep = M;
}
else if(input.equals("l")){
fstep = l;
}
pizzasize.close();
}
}
}
To be able to ask user to input M or L if previous input was wrong use this code:
package com.stackoverflow.json;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner pizzasize = new Scanner(System.in);
System.out.println("Enter pizza size:");
String input = pizzasize.next();
while (!(input.equals("M") || input.equals("l"))) {
System.out.println("Pizza size must be M (medium) or l (large):");
input = pizzasize.next();
}
System.out.println(input);
pizzasize.close();
}
}
Prints:
Enter pizza size:
a
Pizza size must be M (medium) or l (large):
M
M
fstep = pizzasize.nextDouble();
You are requesting the scanner to read a double value here which is why it gives an error when you input letters.
Change variable type of fstep to string and use pizzasize.next() to assign letters to it.
Also if you want to compare letters better to do string comparison in if statement rather than the number comparison you have done
I am working on writing a program that follows these instructions:
Your little sister asks you to help her with her multiplication, and you decide to write a Java program that tests her skills. The program will let her input a starting number, such as 5. It will generate ten multiplication problems ranging from 5×1 to 5×10. For each problem she will be prompted to enter the correct answer. The program should check her answer and should not let her advance to the next question until the correct answer is given to the current question.
After testing ten multiplication problems, your program should ask whether she would like to try another starting number. If yes, your program should generate another corresponding ten multiplication problems. This procedure should repeat until she indicates no.
I have the code correct to ask for the multiplication part, but I can't quite figure out how to get the program to ask if the user wants to continue.
The following code has the program run through once:
package hw5;
import java.util.Scanner;
public class HW5 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter number you would like to attempt: ");
int start = input.nextInt();
int mult;
for (mult = 1; mult <= 10; mult++) {
int num = start * mult;
System.out.print(start + " x " + mult + " = ");
int ans = input.nextInt();
while (ans != num) {
System.out.print("Wrong answer, try again: ");
int ans2 = input.nextInt();
if (ans2 == num) {
break;
}
}
//System.out.print("Would you like to do another problem? ");
}
}
}
When I uncomment out line 21 the program returns:
Enter number you would like to attempt: 1
1 x 1 = 1
Would you like to do another problem? 1 x 2 = 2
Would you like to do another problem? 1 x 3 =
etc...
If I take the code from line 21 and put it outside of the for loop the program runs the for loop once and then jumps straight to the question.
How do I go about fixing this and successfully completing the instructions?
Here's how I'd do it:
package hw5;
import java.util.Scanner;
public class HW5 {
public static void main(String[] args)
{
boolean wantsToContinue = true;
while(wantsToContinue)
{
wantsToContinue = mathProblem();
}
}
public static boolean mathProblem()
{
Scanner input = new Scanner(System.in);
System.out.print("Enter number you would like to attempt: ");
int start = input.nextInt();
int mult;
for (mult = 1; mult <= 10; mult++) {
int num = start * mult;
System.out.print(start + " x " + mult + " = ");
int ans = input.nextInt();
while (ans != num) {
System.out.print("Wrong answer, try again: ");
int ans2 = input.nextInt();
if (ans2 == num) {
break;
}
}
//System.out.print("Would you like to do another problem? ");
}
boolean wantsToContinue;
//Ask if the user would like to do another problem here, set "wantsToContinue" accordingly
return wantsToContinue;
}
}
So what I need help on is that I have this code and I do not know how the if statements can read ",", ".", and "$". I think the String X should be Char instead of String but I do not know how I could change my String X = in.next(); with char[] arr = new char[] and this statement need to be as an input statement! Thank you for helping me and after answering could you write what kinds of things I could do to make my questions better?
So what this code is supposed to do is that for example input is say &&&&&& and 456 then the output is supposed to be*** 456 so meaning &&&&&& = ** and as many numbers there are meaning length the 's go away so * -> ***456
and if input contains "," for example input is &&&,&&&&&(anywhere is fine for ",") and 10000 then output is 10,000 and so on with "." that puts decimal point where it is placed and round up to that point and "$" just puts a dollar sign in front of the numbers
import java.util.Scanner;
public class printFormatting
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the &'s and ,'s");
String X = in.next();
System.out.println("Enter the numbers");
String Y = in.next();
int lengthX = X.length();
int lengthY = Y.length();
if (X.equals(",") && X.equals("."))
{
System.out.println("Works , && .");
//This is to see if , and . both are being able to be seen by the code
}
else if (X.equals(","))
{
System.out.println("Works ,");
//This is to see if , can be seen
}
else if (X.equals("."))
{
System.out.println("Works ,");
//same
}
if (X.equals("$"))
{
System.out.println("Works $");
}
for (int z = lengthX-lengthY; z > 0;)
{
System.out.print("*");
z--;
}
System.out.println(Y);
}
}