I need to receive input from the user, as many times as he wants. tha is done with the while loop. I also need to output the min and the max numbers the user enters, therefore i have the if statememnts. also if the user enters only one value i need to output that one value as the min and the max.
int min =0; int max = 0; int option = JOptionPane.YES_OPTION; while ( option == JOptionPane.YES_OPTION){ String dataString = JOptionPane.showInputDialog("Enter an integer"); int data = Integer.parseInt(dataString);
import java.util.Scanner; public class Project_5 { public static void main (String[] args) { Scanner input= new Scanner (System.in);
[code]....
So I'm attempting to have this program take the users input of integers, pressing enter in between each one. If they enter a negative number it lets them know that its invalid. At the end of the program it takes all of the valid integers entered and must add them and average them. I've put the final println in there as a placeholder. If the user types in "6" then presses enter and types in "3" , the output is:
There were 3 valid numbers entered. The sum of the valid numbers was --- and the average was ---. There were 0 invalid numbers entered.
It then continues on allowing the user to enter more values. Here is the example output for correct code"
Enter a positive value (EOF to quit): 4 Enter a positive value (EOF to quit): 7 Enter a positive value (EOF to quit): 8 Enter a positive value (EOF to quit): 2 Enter a positive value (EOF to quit): -1 The number "-1" is invalid.
Enter a positive value (EOF to quit): 8 Enter a positive value (EOF to quit): 0 Enter a positive value (EOF to quit): -4 The number "-4" is invalid.
Enter a positive value (EOF to quit): CTRL-D
There were 6 valid numbers entered. The sum of the valid numbers was 29 and the average was 4.83. There were 2 invalid numbers.
In my computer science project the program has to end if the user enters 0 as his choice from a menu given to him, so how would I go about ending the program.
My question is how can I make the program repeat until the user enters the number 4 to exit?
/** * Write an application for a furniture company; the program determines the price of a table. Ask the user to choose 1 for pine, 2 for oak, or 3 for mahogany. The output is the name of the wood chosen as well as the price of the table. Pine table cost $100, oak tables cost $225, and mahogany table cost $310. Also ask the user to specify a
(1) large table or a (2) small table.
Add $35 to the price of any large table and add nothing to the price for a small table. Display the output. Your program must repeat until the user chooses to exit.
*/ import java.util.Scanner; public class Wood { public static void main(String[] args) { Scanner input = new Scanner (System.in); System.out.println ("Table Prices");
I have been struggling with this program for weeks. This program is supposed to take a user's inputted odd number and then print out all prime numbers lower than that number.
public class PrimeNumber { Scanner scan = new Scanner(System.in); int userNum; String neg;
I'm trying to do a user validation loop that runs until the user enters a valid binary, q, or Q. I think the rest of my program should work as intended, but here is the areas of concern:
public static boolean isBinary(String num) //Check if the entry is binary { for(int i = 0; i < num.length(); i++) { if(num.charAt(i) != 0 || num.charAt(i) != 1){ return false;
In my cs class, we have to write a program that throws an exception if the user enters a negative number, the program should prompt the user to enter only positive numbers and then let them retype the number. But everytime I throw an exception, my program gets suspended.
What am I doing wrong, or is there no way to continue a program if an exception is thrown? Does the program automatically get suspended when an exception is thrown?
try{ do { N = kb.nextDouble(); if(N<0) { throw new NegativeArraySizeException(); } else j++; } while(j==0); fill.size(N); } catch(NegativeArraySizeException e) { System.out.println("The number you entered must be positive. Please try again."); }
Any better way to write a program that takes a user number input and the program determines whether or not the number is prime or not. It was suppose to be a number between 0 and 8,000,000.
import java.util.Scanner; public class prime1 { public static void main(String args [])
I tried to create file and write the output of my program in it in java when i use WriteLong then the file does not contain long value, how I create this file my program is to print prime numbers between 500000 to 10000000
public class primenumber { public static void main(String[] args) { long start = 5000000; long end = 10000000; System.out.println("List of prime numbers between " + start + " and " + end); for (long i = start; i <= end; i++) { if (isPrime(i)) { System.out.println(i);
import java.awt.*; import java.applet.*; import java.awt.event.*; public class Assignment extends Applet implements ActionListener { TextArea textInput = new TextArea(); // user input Button analyzebutton = new Button("Analyze"); Button resetbutton = new Button("Reset"); Label lbloutput = new Label ("Please enter text into the textbox!");
This specific assignment requires me to write a program that calculates the total sales after each football game. There are 4 types of tickets: box, sideline, premium, and general admission. After each game, data is stored in a file... then output the number of tickets sold and total sale amount. Format your output with 2 decimal places.
Okay I know there is a I/O section and wasnt sure where to post this. I need some insight on how to use file input and output as well ... Here is my program:
import javax.swing.JOptionPane; import java.text.DecimalFormat; public class ticketsSold { public static void main(String[] args) { String BoxSeat; String SideLine;
I'm supposed to take an input from the user and calculate the value of pi using this formula: 1/1 - 1/3 + 1/5 - 1/7 etc... so on alternating between minus and plus and denominator increasing by 2 every time. If the user inputs the number 3, then it should print (1/1 - 1/3 + 1/5 - 1/7 + 1/9) *4. The idea here is that the higher the input of the user is, the higher the equation will be close to pi.
This is what i have so far :
import java.util.Scanner; public class Pi { public static void main(String[] args) { double first = 1/1; double second = 1/3; Scanner input = new Scanner(System.in); System.out.println("Enter a Number to calculate how far you want the formula to perform: "); double count = input.nextDouble(); } }
This is literally all i have, i know i need a while loop and an if loop.
I have to create a program that calculates the nth Fibonacci number and returns that to the user. Fibonacci said his number sequence would describe the ideal breeding patterns of immortal rabbits. So, you are going to make this vision a reality.
First, take in a numeric value from the user and calculate that value in the fibonacci series. Next, find an image of a rabbit and display the image on a GUI (put the image as an icon on a label!) the number of times returned by the algorithm (Put all the aforementioned labels on one panel with FlowLayout!).
You need to remove the old images from the Panel. Probably the easiest way to do this is to create a whole new panel and remove the old one (hint: the remove method of JPanel should come in handy)
You could use an array of JLabels You will need to create a new JLabel and add each member of the array to the panel I should be able to scroll to see any images that are off screen
I am having difficultly on to making the array list for JLabel, and getting the Fibonacci sequence to show the pictures of rabbits. Below is my current code.
import java.awt.FlowLayout; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Rabbit extends JFrame
This is the first time we are using 2 different classes. This is the code I have so far. What I am having trouble is doing the calculations and storing the value into the planet the user selected and keeping it going. I will attach the instructions ....
public class FakeGravity{ // Instance variables private String planet; private int VelocityDecay; private double BouncinessFactor; // default constructor public FakeGravity(){
I am doing an assignment that is asking for the user to put in the radius of a circle and the program figures out the area, diameter and circumference. It is using 2 different java programs to accomplish this. One with the info on how to get area, diameter and circumference and one is the demo that runs the program. I keep getting errors on my demo.
// Circle Class public class Circle { private double rad; private double Pie; private double area; private double diameter;
How do you use power of math. I'm trying to write a calculator to calculate the volume of a cylinder by asking the user to enter the height and radius but when I use pow(2) it doesn't work. I imported java.lang.math class so i dont have to keep using math. for now my code runs just fine since I'm using radius * radius but I would really luv to use the power instead times each other when i have to use higher powers.
import java.util.Scanner; import static java.lang.Math.*; public class Lab2 { public static void main(String[] args) { Scanner scan = new Scanner(System.in);
The purpose of this program is to translate a user inputed sentence into morseCode. It seems like everything is right to me, but I'm simply not getting output! What am I doing wrong, or what should I add/change?
Here is the main class:
public class MorseCode { public static String myInput; public static String[] myMorse; public static String myUserInput; public static char[] myAlph = {'A','B','C','D','E','F','G','H','I','J','K','L','M', 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z'}; public MorseCode()
I'm using one method to take input from the user and append some text to it, then I am trying to return the value of the variable and use another method to print it out. But for some reason whenever I try doing this I can't print out anything and I only get a "null" output. Why this is happening?
package Homework3; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Homework3 {
Using JOptionPane,ask for 10 numbers from the user.Use an array to store the values of these 10 numbers.Output on the screen the number with the greatest value.
I created my own class and it basically randomizes the amount of 'spells' [magic] a user uses (range 1-5). I can't figure out how to increment the initial randomized output to make something like this :
User X : uses 2 spell. User Y : uses 3 spells.
User X : uses 3 spells. (Taking the randomized 2 and increment by 1) User Y : uses 4 spells. (Taking the randomized 3 and increment by 1)
My current setup looks like this : First one is the class I created and the second one is just how the output would look like. (seperate by +++++...)
My goal is to create a METHOD to increment it.
public class Spell { // Max number of spells private final int MAX_FACE_VALUE = 5; // Current face value private int faceValue; // New instance of Spell public Spell() { faceValue = 1;
[Code] ....
+++++++++++++++++++++++++++++++++
public class SpellOutput { public static void main(String[] args) { //Object creation Spell spellCasterOne = new Spell(); Spell spellCasterTwo = new Spell(); Spell spellCasterThree = new Spell();
I had to make a program that allows the user to enter 3 sides and it should output "invalid" if it does not make a triangle and if it does make a triangle it calculates the area of the triangle and it doesnt seem to do anything but say "invalid" as the out put.
import java.util.Scanner; public class MyTriangle { public static void main(String[] args) { Scanner input = new Scanner(System.in); boolean valid = true; double side1, side2, side3, area, A;
import java.util.Scanner; public class Practice { public static void main(String[] args) { //get user input Scanner user_input = new Scanner(System.in); System.out.print("Enter a number: " );