For some reason, the below code is not working out for me. I thought I had the scanner right and Eclipse is only throwing an error on the last line that states "else(ernie == 0)". What am I missing here?
Basically just trying to take user input, incorporate some if and else if statements to return a line based on if the condition is met.
Java Code:
import java.util.Scanner;
//Importing the scanner now for user input
public class Ernie {
public static void main(String[] args) {
double ernie;
I am trying to make a program that calculates the change due in dollars and cents. The user inputs both the amount due and the amount tendered. My program only works with whole numbers?
I am writing a code in which a user inputs data as a string and that data must be verified as a valid number. A valid number is anything from 0-100. Then all valid numbers are converted into double numbers.
I am having trouble in how to write the validation part of the code.
Is it suppose to be an if, else statement? And if so how is it suppose to be validated?
import static java.lang.System.out; import java.util.Scanner; import javax.swing.JFrame; public class Main { public static void main (String args[]) { Scanner read = new Scanner (System.in); out.print ("> "); double num = read.nextDouble(); out.println (num); } }
When I compile the code it gives me no errors; however when I run the program and write a floating point number it gives me the following message:
java.util.InputMismatchException: null (in java.util.Scanner)
I used to use an online compiler and the nextDouble() method worked perfectly there.
I'm trying to write a program where the user has the option to enter either an int or double but whatever input I enter just gets cast to a double. Do I even have the right Idea by using if else statements?
import java.util.*; public class VS { public static void main(String[] args) { Scanner Input = new Scanner(System.in); System.out.println("Please enter a number"); if (Input.hasNextDouble()) {
I am supposed to write a program on PuTTY using UNIX (I have Windows 8), but I am not comfortable with it yet, so I am using Java through NetBeans (IDE 7.4).
The program has to follow these instructions (ignore the Linux part of the instructions, the rest is in bold):
Write a program on the Linux system using the putty utility. The program should get strings of data from the command line (that is, look for the data in the "args" array of strings). Use a loop to convert each of the strings in the array into a double and add the number to a total. Print the total after all of the strings have been processed.
The program will use try-catch to catch any error that occurs. If an error occurs, the program will print a message saying that the error occurred. The program can end at that point.
You should create the Java program using the nano editor. The input data should be a list of numbers on the line that runs the program.
The problem so far is that I keep getting an error when converting a String value into a new double value. I have yet to code the try-catch method in my program
Java Code:
import java.util.*; public class Program13Strings { public static void main(String[] args) { Scanner scan = new Scanner (System.in); System.out.println("How many lines of data do you wish to enter?"); int size = scan.nextInt();
[Code] ....
*NOTE: right where my code says double newDouble = Double.valueOf(newResponse); is where the error is occurring.
*ERROR: Exception in thread "main" java.lang.NumberFormatException: For input string: "abc"
at sun.misc.FloatingDecimal.readJavaFormatString(Floa tingDecimal.java:1241) at java.lang.Double.valueOf(Double.java:504) at program13strings.Program13Strings.main(Program13St rings.java:21)
So i need to write a program that prompts the user for 3 double numbers x, y, z that outputs 2x^3 + 3y^5 + 3x^3y^2 +xyz with
(1) 4 digits precision (2) with a ',' to separate thousands (3) all digits of the result including the precision are put to 20 positions (from right to left)
I am not really sure what it is suppose to look like or how to start it.how to mix Math.pow with multivariable and printf.
Write a java program that will ask a user to input grades until the user inputs the sentinel value -1. The program should test each input to determine whether 0<=grade<=100. If the grade entered is within this range, the program should print "Grade accepted;" if not, the program should print "Invalid input".
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;
class Client{ public static void main(String []args){ Bike R1=new Bike(5.0, 60.0,30.0);//create bike object with params Bike R2=new Bike();//without params System.out.println(R1.increaseSpeed());//calling methods System.out.println(R1.maxDistance()); System.out.println(R2.increaseSpeed()); System.out.println(R2.maxDistance()); } }
I have been coding in my class at school (Grade 11 Computer science) and i just downloaded the program on my computer at home, unfortunately i cannot access my computer notes at home and i also dont remember certian specifics of coding, so my question is how would i get user input to create a program. The comments are the parts i dont remember. (I am trying to slowly build my memory with this stuff)
Here is my code so far:
import java.util; [highlight=java] public class hello_world { public static void main(String[] args) { string name; //WHAT DO I PUT HERE????
I'm working on creating a dice game and getting the users input is giving me a really hard time. I have the main game working but this part I'm stuck on. The problem is that it's not giving me the chance to enter the second input when I ask for the game.
//Create Scanner object Scanner keys = new Scanner(System.in); //Get chips
[Code]....
*****This is what I get when I run it
run:
How much money would you like to change? 50
You now have $50 in chips.
What game do you want to play? You did not pick dice.
I have been given a piece of work to do for College that requires me to format user input. For example: This is an example of text that will have straight left and right margins after formatting
The user inputs the width, in the case above being 20. Each line has to therefore be 20 characters long. All spaces are to be replaced with full stops.
This.is.an.example.o f.text.that.will.hav e.straight.left.and. right.margins.after. formatting.......... public class Main { public static void main ( String args[])
ArrayList<Integer> leftCU = new ArrayList<>(); { System.out.println ("Please enter one at a time the number of CU's for each class that is left to complete. Enter Q when done."); leftCU.add(in.nextInt());
I'm working on creating a dice game and getting the users input is giving me a really hard time. I have the main game working but this part I'm stuck on. The problem is that it's not giving me the chance to enter the second input when I ask for the game.
//Create Scanner object Scanner keys = new Scanner(System.in); //Get chips System.out.print("How much money would you like to change? "); int chips = keys.nextInt(); System.out.println("You now have $" + chips + " in chips.");
[code]...
This is what I get when I run it run/How much money would you like to change?
I created a scanner object but it's not asking for any input. when I create a new scanner and then tell it to print it it just prints a bunch of weirdness when AFIK it should be asking me to type something and then it should repeat what i entered.
I trying to get this code to get user input instead of reading from a hardcoded array. I'm getting compile errors while trying to get user input. Here's some of the code:
I would like to multiply the amount stated inside program by user input. I currently have
public static void setTicket(double ticket) { //if statement for tickets if (ticket == 1) Ticket = 10.00; else if (ticket == 2) Ticket = 20.00; else if ( ticket == 3) Ticket = 30.00;
The user should enter 2 and the value should show 20.00. This does work however im looking for a way to say enter 2 = 2*10 instead of stating each value individually.
I have a program that asks you for your password and if it is incorrect it reduces your attempts until you are at zero, but I want to be able to tell the user what passwords they have attempted when they run out of attempts. I've tried a couple of things but how to get it to work. This is my code:
import javax.swing.JOptionPane; public class PasswordV4 { //Begin Class public static void main(String[] args) { //Begin main program //initialize variable int passwordAttempt = 5; int value = 0;