why "Keyboard.readDouble()" doesn't work? - java

All I want it to do is read input typed by the user as double type, and then convert it to another number. I'm also aware the equation isn't complete, not worried about that right now, just want it to run. I don't understand what I have done wrong.
public class EuroShoe {
public static void main(String[] args) {
double
footLength,
euroSize;
System.out.println("EUROPEAN SHOE SIZE");
System.out.println("Enter the length of your foot in inches:");
footLength = Keyboard.readDouble(); // line 25
euroSize = (((footLength - 9) * 3 / 2) + 15);
System.out.println("Your European shoe size is " + euroSize);
}
}

If you are following a tutorial, please check on the imports that were mentioned. But to answer your question and make your program work here's an answer.
import java.util.Scanner
public class EuroShoe {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int i =
double footLength, euroSize;
System.out.println("EUROPEAN SHOE SIZE");
System.out.println("Enter the length of your foot in inches:");
// The statement below calls the "scanner" object to get the user input of value "double"
footLength = scanner.nextDouble();
euroSize = (((footLength - 9) * 3 / 2) + 15);
System.out.println("Your European shoe size is " + euroSize);
}
}
Make sure to put this statement above
import java.util.Scanner // imports the specific Scanner class under the 'util' namespace
or you can use this as well
import java.util.* // imports every class under the 'util' namespace
Hope this helps you with your problem and keep coding! :)

This worked:
package euroshoe;
import java.util.Scanner;
public class EuroShoe {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("EUROPEAN SHOE SIZE");
System.out.println("Enter the length of your foot in inches:");
double footLength = input.nextDouble();
double euroSize = (((footLength - 9) * 3 / 2) + 15);
System.out.println("Your European shoe size is " + euroSize);
}
}

Related

Getting this error: "main" java.lang.NumberFormatException"; may be a fundamental issue

I guess I can preface this with the assignment?
In any case, I barely understand the assignment, Im gonna be honest. Been truly freeballing, but I've gotten it down to this:
import java.util.Scanner;
import java.text.DecimalFormat;
public class TimeTravel
{
static final double STUDENT_DISCOUNT = .50;
static final double EMPLOYEE_DISCOUNT = .25;
/**
* Discounts described
*
* #param args Command line arguments (not used).
*/
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
//double ticketcode;
System.out.println("Enter ticketcode: ");
//ticketcode = userInput.nextDouble();
Scanner input = new Scanner(System.in);
String ticketcode = input.nextLine();
System.out.println((char)Integer.parseInt(ticketcode));
int tcode = Integer.parseInt(ticketcode);
//String s = Double.toString(ticketcode);
if (tcode <= 26) {
System.out.println("*** Invalid ticket code ***"
+ "\nTicket code must have at least 26 characters.");
}
else {
System.out.println("Time: ");
System.out.println(ticketcode.substring(0, 4));
System.out.println("Date: ");
System.out.println(ticketcode.substring(5, 12));
System.out.println("Category: ");
System.out.println(ticketcode.substring(0, 4));
System.out.println("Seat: ");
System.out.println(ticketcode.substring(27, 29));
System.out.println("Itinerary: ");
System.out.println(ticketcode.substring(30, 54));
double price = Double.parseDouble(ticketcode);
DecimalFormat df = new DecimalFormat("#,###.00");
System.out.println("Price: " + df.format((ticketcode.substring(14, 22))));
System.out.println(ticketcode.substring(0, 4));
System.out.println("Cost: ");
System.out.println("Prize Number: ");
System.out.println(ticketcode.substring(0, 4));
}
}
}
every comment within the main body is pretty much my rambling.
If you insert into your standard input(in your case ticketcode) Non integer character then Integer.parseInt(ticketcode) will throw NumberFormatException. This is because non integer input can not parsed.

Compound interest

import java.util.*;
public class Project3{
public static void main(String[] args)
{
Scanner key = new Scanner (System.in);
double rate = 0.05;
double annually, monthly, daily;
double balance;
int year = 10 ;
System.out.println("Enter the amount you will like to deposit or type exit to end.");
int deposit = key.nextInt();
annually = deposit * Math.pow((1 + rate/1),year);
monthly = deposit * Math.pow((1 + rate/12),year);
daily = deposit * Math.pow((1 + rate/365),year);
while (deposit)
{
}
System.out.println(annually);
System.out.println(monthly);
System.out.println(daily);
}
}
This is what I currently have. What I am trying to accomplish is to make a loop to add the first outcome with the next one. Also make one formula instead of having three to find the annually, monthly and daily.
First and foremost, asking someone to write out your homework is really unethical, and not helpful for you in the long run. If you don't care about the long run, consider taking a different class. In a career scenario, you're expected to write code on your own.
Secondly, to actually answer your question, here are some tips:
It seems like you want to gather a value (deposit) from the user, and then calculate the Compound Interest for said value. Your program also needs to not exit until the user says to exit. i.e. they want to calculate the CI for a set of numbers.
First step is to check the value from the user. If it is a number, then do calculations on it. If it is a String, then check if it is "exit". In Java, this amounts to writing out an if-statement, and making use of the very helpful "instanceof" keyword. If you haven't learned about that, give this a read, or ask your teacher.
For the calculations part, you simply do calculations on the user's input while the input is not a string set to "exit".
Finally, print out your calculations.
That's it. Your code already has the calculation formulas down, so you just need to code the logic for handling user input.
import java.util.Scanner;
import java.lang.Math;
public class HelloWorld {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("How much money you want to deposit?");
int principle = sc.nextInt();
System.out.println("what is the rate you want?");
float rate = sc.nextFloat();
System.out.println("After how many years, you want to see your money?");
int year = sc.nextInt();
System.out.println("How many compounds in a year?");
int partialTime = sc.nextInt();
double b = year * partialTime;
double a = 1 + (rate/(partialTime*100));
double x = principle * (Math.pow(a,b));
System.out.println("Your interest in given time would be " + x);
}
}
A couple of suggestions - since you want to check user input against both String and int types, you could define a String type variable to hold the user input, and then do a try/catch to parse it as an Integer, if it's not an Integer check if the input equals "exit" (using the String.equals() method).
import java.util.*;
public class Project3{
public static void main(String[] args)
{
Scanner key = new Scanner (System.in);
double rate = 0.05;
double annually = 0, monthly = 0, daily = 0;
double balance;
int year = 10, deposit = 0 ;
String userinput = "";
do {
try {
System.out.println("Enter the amount you will like to deposit or type exit to end.");
userinput = key.nextLine();
deposit = Integer.parseInt(userinput);
}
catch (Exception e){
if (!userinput.equals("exit")){
System.out.println("Didn't recognize that input, please try again...");
}
else{
break;
}
}
} while (!userinput.equals("exit"));
annually += deposit * Math.pow((1 + rate/1),year);
monthly += deposit * Math.pow((1 + rate/12),year);
daily += deposit * Math.pow((1 + rate/365),year);
System.out.println(annually);
System.out.println(monthly);
System.out.println(daily);
}
}
Depending on how you want the output, you can easily adjust the scope of the loop to display the amounts after each valid deposit input, or just once at the end, after the user enters "exit".
Hope this helps.

Describe the algorithm of currency conversion

CurrencyConversion that offers the user the choice of converting US Dollars into British Pounds or the other way around. (You may assume that 1 US Dollar = 1.64 Pounds.)
// Import scanner
import java.util.Scanner;
public class MoneyConversion {
public static void main (String[] args) {
// Declare variable
final double RATE = 1.64;
// Declare currency
double dollar;
double Pounds;
// Call scanner from keyBoard
Scanner fromKeyboard = new Scanner(System.in);
// Ask user to enter the amount of pounds
System.out.print("Please write number of pounds");
// Read the value into variable
Pounds = fromKeyboard.nextDouble();
// Write the method for currency conversion
dollar = Pounds * RATE;
System.out.println("The number of dollars is " + dollar);
} // End of main
} // End currency conversion
Make different classes for different currency conversions as follows:
public static void main(String[] args) {
Scanner fromKeyboard = new Scanner(System.in);
System.out.print("Please write number of pounds");
System.out.println("The number of dollar is " + convertToDollar(fromKeyboard.nextDouble()));
}
public static double convertToDollar(double pound){
double rate = 1.64;
double Pounds;
return( pound * rate);
}

Using user input to call an integer

I am a beginner in coding, and I am trying to create a simple program that when a reader types in their name, the program shows how much money they owe. I was thinking of using a Scanner, next(String), and int.
import java.util.Scanner;
public class moneyLender {
//This program will ask for reader input of their name and then will output how much
//they owe me. (The amount they owe is already in the database)
public static void main(String[] args) {
int John = 5; // John owes me 5 dollars
int Kyle = 7; // Kyle owes me 7 dollars
//Asking for reader input of their name
Scanner reader = new Scanner(System.in);
System.out.print("Please enter in your first name:");
String name = reader.next();
//my goal is to have the same effect as System.out.println("You owe me " + John);
System.out.println("You owe me: " + name) // but not John as a string but John
// as the integer 5
//Basically, i want to use a string to call an integer variable with
//the same value as the string.
}
}
As a beginner, You might be want to use a simple HashMap, which will store these mappings as key, value pair. Key will be name and value will be money. Here is example:
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class moneyLender {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("John", 5);
map.put("Kyle", 7);
Scanner reader = new Scanner(System.in);
System.out.print("Please enter in your first name:");
String name = reader.next();
System.out.println("You owe me: " + map.get(name)); //
}
}
Output :
Please enter in your first name:John
You owe me: 5
If you want the read the user input as a String, it would be a great idea to use the nextLine() method.
You would also want to create a method which takes a String parameter i.e. the names and returns the owed amount.
public int moneyOwed(String name){
switch(name){
case "Kyle": return 5;
case "John": return 7;
}
}
public static void main(String[] args) {
int John = 5; // John owes me 5 dollars
int Kyle = 7; // Kyle owes me 7 dollars
Scanner reader = new Scanner(System.in);
System.out.print("Please enter in your first name:");
String name = reader.nextLine();
System.out.println(name +" owes me " + moneyOwed(name) + " dollars");
}

Weird error on driver class

I'm writing a driver class for a piggy bank class that I created. The idea is that it is supposed to add different types of coins (user input) and then total the cents and display them until "X" is input by the user. I think I have the code right, but there is a weird issue where if I use the "countMoney" accessor into the code, it tells me that all of my variables in the driver class are uninitialized. If I remove it, there are no errors shown by Eclipse. I've printed my source and driver class below:
package piggy;
/**
* #author Kevin
*
*/
import java.util.Scanner;
import piggy.PiggyBank;
public class PiggyBankTester {
/**
* #param args
*/
public static void main(String[] args) {
String num = "str", num1;
int count = 0;
int money;
Scanner scan = new Scanner(System.in);
Scanner scan2 = new Scanner(System.in);
PiggyBank total = new PiggyBank();
System.out.println("Welcome to the Piggy Bank Tester");
System.out.println("What type of coin to add (Q, H, D or X to exit)?");
num1 = scan.nextLine();
num = num1.toUpperCase();
{
if (num.equals("X"))
System.out.println("Goodbye.");
else if (num != "X")
{
System.out.println("How many do you wish to add?");
count = scan.nextInt();
if (num.equals("Q"))
total.addQuarters(count);
else if (num.equals("H"))
total.addHalfDollars(count);
else if (num.equals("D"))
total.addDollars(count);
else if (num.equals("X"))
System.out.println("Goodbye.");
}
}
{
total.calculate();
money = total.countMoney();
System.out.println("The piggy bank now contains " + money + " cents.");
}
}
}
You don't need (String Q,D,H,X) .
Also you have declared this variables without give them any value just name.
A way you can do it is to change your if-else statements and set , for example if you want num to be equal to Q ---> if (num.equals("Q") ) <---

Categories

Resources