Need help for Java export - java

I exported my Java file, and its kind of working proberly.. theres just a little problem, that when the scanner have calculated my inputs, it outputs the result, but the application closes 1 sec after i recieved my output. How do i prevent my application from close?? the code is:
best regards
Oliver
import java.util.Scanner;
import java.util.HashMap;
import java.util.*;
public class Valuta {
public static void main(String[] args){
double euro, usd, gpb, dkk, done;
Scanner input = new Scanner(System.in);
System.out.println("Convert from " +"USD,GPB, DKK or EURO?");
String temp = (input.nextLine()).toUpperCase();
System.out.println("to " + "USD,GPB, DKK or Euro?");
String tempp = (input.nextLine()).toUpperCase();
Map<String, Double> lookUpMap = new HashMap<String, Double>(){{
put("EURO", new Double(7.46));
put("USD", new Double(5.56));
put("GPB", new Double(8.84));
put("DKK", new Double (1.0));
}};
System.out.println("amount of " + (temp));
double amount = input.nextDouble();
done = (lookUpMap.get(temp) / lookUpMap.get(tempp)) * amount;
System.out.println(done);
}
}

try this..
public static void main(String[] args) {
double euro, usd, gpb, dkk, done;
Scanner input = new Scanner(System.in);
String ch = "";
do{
System.out.println("Convert from " + "USD,GPB, DKK or EURO?");
String temp = (input.nextLine()).toUpperCase();
System.out.println("to " + "USD,GPB, DKK or Euro?");
String tempp = (input.nextLine()).toUpperCase();
Map<String, Double> lookUpMap = new HashMap<String, Double>() {
{
put("EURO", new Double(7.46));
put("USD", new Double(5.56));
put("GPB", new Double(8.84));
put("DKK", new Double(1.0));
}
};
System.out.println("amount of " + (temp));
double amount = input.nextDouble();
done = (lookUpMap.get(temp) / lookUpMap.get(tempp)) * amount;
System.out.println(done);
System.out.println("Do you want continue ? Y/N");
ch = input.nextLine();
} while(ch.equals("Y"));
}

Related

Can't solve this:

I'm a beginner.....I cant seem to solve this problem involving multiple methods....the method using the scanner isn't doing anything
package bucky;
import java.util.Scanner;
public class test {
public static void main(String args[]){
input();
for(int time=1;time<values[2];++time){
double A=values[1]*Math.pow(1+values[3], time);
System.out.println("You have: "+A+ " subscribers today");
}}
public static double[] input(){
Scanner s=new Scanner(System.in);
double[] values=new double[3];
System.out.println("Enter the Principal value: ");
values[1]=s.nextDouble();
System.out.println("Enter the no. of days: ");
values[2]=s.nextInt();
System.out.println("Enter the Rate of growth : ");
values[3]=s.nextDouble();
return values;
}
}
input() has to be assigned to values right? Like: double[] values = input();
Also, the indexing in array starts with 0... so values[0] is the first value and so on.
Try this code:
public static void main(String args[])
{
double[] values = input();
for (int time = 1; time < values[2]; ++time)
{
double A = values[1] * Math.pow(1 + values[3], time);
System.out.println("You have: " + A + " subscribers today");
}
}
public static double[] input()
{
Scanner s = new Scanner(System.in);
double[] values = new double[3];
System.out.println("Enter the Principal value: ");
values[0] = s.nextDouble();
System.out.println("Enter the no. of days: ");
values[1] = s.nextInt();
System.out.println("Enter the Rate of growth : ");
values[2] = s.nextDouble();
return values;
}
Try double[] values = input(); in main method.
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class test { public static void main(String args[])
{
double[] values = input();
for(int time=1;time<values[2];++time){
double A=values[0]*Math.pow(1+values[2], time);
System.out.println("You have: "+A+ " subscribers today");
}}
public static double[] input()
{
Scanner s=new Scanner(System.in);
double[] values=new double[3];
System.out.println("Enter the Principal value: ");
values[0]=s.nextDouble();
System.out.println("Enter the no. of days: ");
values[1]=s.nextInt();
System.out.println("Enter the Rate of growth : ");
values[2]=s.nextDouble();
return values;
}
}
Again it is going to throw an arrayoutofbondsexception...change values[1] with values[0] values [2] with values[1] and values[3] with values[2]..

Trying to show the list of the hashmap keyset in the other method

So, I want to call the hashmap keyset list from the main class and list them in console. I am trying to show the keyset before each printing:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// The keyset can be set here to show the alternatives to convert to the user
System.out.println("What length you want to confert from");
String to = input.nextLine();
System.out.println("What length you want to confert to");
String from = input.nextLine();
System.out.println("Input length");
double value = input.nextDouble();
int result = (int)Length.convert(value, from, to);
System.out.println((int )value + from + " = " + result + to);
}
**
Here is the second method in Length for converting the length:
**
public static double convert(double value, String from, String to){
HashMap<String, Double> table= new HashMap<>();
table.put("mm", 0.001);
table.put("cm", 0.01);
table.put("dm", 0.1);
table.put("m", 1.0);
table.put("hm", 100.0);
table.put("km", 1000.0);
table.put("ft", 0.3034);
table.put("yd", 0.9144);
table.put("mi", 1609.34);
double from_value = table.get(from);
double to_value = table.get(to);
double result = from_value / to_value * value;
return result;
}
Fix the Length class :
class Length {
//Declare the map as class variable
static Map<String, Double> table = new HashMap<>();
//Initialize the map
static {
table.put("mm", 0.001);
table.put("cm", 0.01);
table.put("dm", 0.1);
table.put("m", 1.0);
table.put("hm", 100.0);
table.put("km", 1000.0);
table.put("ft", 0.3034);
table.put("yd", 0.9144);
table.put("mi", 1609.34);
}
public static double convert(double value, String from, String to) {
double from_value = table.get(from);
double to_value = table.get(to);
double result = from_value / to_value * value;
return result;
}
//Print the KeySet
public static void printMap() {
System.out.println(table.keySet());
}
}
Update the main method :
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//Show Keyset
Length.printMap();
System.out.println("What length you want to confert from");
String to = input.nextLine();
System.out.println("What length you want to confert to");
String from = input.nextLine();
System.out.println("Input length");
double value = input.nextDouble();
int result = (int) Length.convert(value, from, to);
System.out.println((int) value + from + " = " + result + to);
}

I am getting an exception due to nextDouble(), how can I avoid this?

I'm an AP computer science and I am coding java. There is only a limited number of methods and classes that I can use. For instance I am not allowed to use hasNextLine() . This is the error it gives me once I enter the value for "how many euros is one dollar." It allows me to enter that value and then asks to enter the dollar value. However, before I can enter it, this error shows up:
Exception in thread "main" java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)
at CurrencyConverter.main(CurrencyConverter.java:20)
public class Currency
{
private double rate;
public Currency()
{
rate = 0.0;
}
public Currency(double newRate)
{
rate = newRate;
}
public double convert(double dollar)
{
double euro = dollar * rate;
return euro;
}
}
import java.util.Scanner;
import java.util.Scanner;
public class CurrencyConverter
{
public static void main(String [] args)
{
Scanner input = new Scanner(System.in);
System.out.println("How many euros is one dollar?");
double exchangerate = input.nextDouble();
System.out.println("Dollar value (Q to quit):");
String dollarvalue = input.nextLine();
double dv = 0.0;
String bvalue = "";
String bvaluetwo = "Q";
if (dollarvalue.equals(bvalue))
{
dollarvalue = "test";
}
else if (!dollarvalue.equals(bvaluetwo))
{
dv = Double.parseDouble(dollarvalue);
}
Currency exchange = new Currency(exchangerate);
while (dollarvalue != "Q")
{
double eurovalue = exchange.convert(dv);
System.out.println(dv + " dollar = " + eurovalue + " euro");
}
}
}
Try puting this into your main method instead:
public static void main(String [] args)
{
Scanner input = new Scanner(System.in);
System.out.println("How many euros is one dollar?");
double exchangerate = input.nextDouble();
//You have to put this in order to continue
input.nextLine();
System.out.println("Dollar value (Q to quit):");
String dollarvalue = input.nextLine();
double dv = 0.0;
String bvalue = "";
String bvaluetwo = "Q";
if (dollarvalue == bvalue)
{
dollarvalue = "test";
}
else if ((dollarvalue != bvaluetwo))
{
dv = Double.parseDouble(dollarvalue);
}
Currency exchange = new Currency(exchangerate);
while (!dollarvalue.equals("Q"))
{
double eurovalue = exchange.convert(dv);
System.out.println(dv + " dollar = " + eurovalue + " euro");
System.out.println("Dollar value (Q to quit):");
dollarvalue = input.nextLine();
if (!dollarvalue.equals("Q")) {
dv = Double.parseDouble(dollarvalue);
eurovalue = exchange.convert(dv);
System.out.println(dv + " dollar = " + eurovalue + " euro");
}
}
System.out.println("You pressed Q, have a nice day");
}
}
double exchangerate = input.nextDouble(); this will give you exception if there is no input , always use scanner's hasNext() method before calling any of the next methods
Here is your main culprit :
System.out.println("Dollar value (Q to quit):");
String dollarvalue = input.nextLine();
double dv = 0.0;
String bvalue = "";
String bvaluetwo = "Q";
if (dollarvalue == bvalue) {
dollarvalue = "test";
} else if ((dollarvalue != bvaluetwo)) {
dv = Double.parseDouble(dollarvalue);
}
You are storing a String of "Q" or other string values entered by user , then
in dv = Double.parseDouble(dollarvalue); ,you are parsing(converting) it into a double which is throwing java.lang.NumberFormatException exception

Type " = " and get result

as soon as I press enter after typing the expression,it displays result which is calculate grossProfit, Studio and Theater. And now I want that it should wait for me to type "=" and then show the result? can some one help me how to type "=" and then show the result? Thanks
package ticket;
import java.util.Scanner;
import java.text.NumberFormat;
public class Ticket {
public static void main(String[] args) {
//----------------------------------------------
//calculate grossProfit, Studio and Theater.
//------------------------------------------------
final double TICKETPRICE=8, PROFITERATE=.25;
int numberofticket;
double grossProfit,theater,studio;
String replace;
String name = "name";
NumberFormat fmtCur = NumberFormat.getCurrencyInstance();
NumberFormat fmtPct = NumberFormat.getPercentInstance();
Scanner scan = new Scanner(System.in);
System.out.print("Enter movie name:");
name = scan.nextLine();
System.out.print("Enter tickets....");
numberofticket= scan.nextInt();
replace = name.replace ('a', 'A');
grossProfit = TICKETPRICE * numberofticket;
theater = grossProfit * PROFITERATE;
studio = grossProfit - theater;
System.out.println("Box Office Report");
System.out.println("Movie Name =" + replace);
System.out.println("Tickets =" + numberofticket);
System.out.println("TICKETPRICE =" +fmtCur.format(TICKETPRICE));
System.out.println ("grossProfit= " + fmtCur.format(grossProfit));
System.out.println ("theater = " + fmtCur.format(theater) + " at " + fmtPct.format(PROFITERATE));
System.out.println ("studio = " + fmtCur.format(studio));
}
}
Just ask user a line before printline like:
do {
//message for user to add line?
String nextLine = scan.nextLine();
while (!nextLine.equals("="));

Temperature conversion code in Java won't run?

Ok,so I'm a complete novice at programming and I just started coding in Java. I tried to write a code for temperature conversion (Celsius to Fahrenheit) and for some reason it simply won't run! Please, help me find out errors in this code(however silly it may be).
Here's the code:
package tempConvert;
import java.util.Scanner;
public class StartCode {
Scanner in = new Scanner(System. in );
public double tempInFarenheit;
public double tempInCelcius;
{
System.out.println("enter the temp in celcius");
tempInCelcius = in .nextDouble();
tempInFarenheit = (9 / 5) * (tempInCelcius + 32);
System.out.println(tempInFarenheit);
}
}
You forgot to write the main method which is the start point for a program to run. Let me modify your code.
import java.util.Scanner;
public class StartCode
{
Scanner in = new Scanner (System.in);
public double tempInFarenheit;
public double tempInCelcius;
public static void main (String[] args)
{
System.out.println("enter the temp in celcius");
tempInCelcius = in.nextDouble() ;
tempInFarenheit = (9/5)*(tempInCelcius+32);
System.out.println(tempInFarenheit);
}
}
I think this is going to work better for you:
import java.util.Scanner;
public class StartCode
{
public static void main(String[] args) {
Scanner in = new Scanner (System.in);
double tempInFarenheit;
double tempInCelcius;
System.out.println("enter the temp in celcius");
tempInCelcius = in.nextDouble() ;
tempInFarenheit = 1.8*tempInCelcius+32;
System.out.println(tempInFarenheit);
}
}
You equation for Farenheit was incorrect. Integer division isn't for you, either.
You need a main method. I also suggest using an IDE such as Eclipse, which can generate the skeleton code for you (including the syntax of the main method).
import java.util.*;
public class DegreeToFahrenheit {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter a temperature: ");
double temperature = input.nextDouble();
System.out.println("Enter the letter of the temperature type. Ex: C or c for celsius, F or f for fahrenheit.: ");
String tempType = input.next();
String C = tempType;
String c = tempType;
String F = tempType;
String f = tempType;
double celsius = temperature;
double fahrenheit = temperature;
if(tempType.equals(C) || tempType.equals(c)) {
celsius = (5*(fahrenheit-32)/9);
System.out.print("The fahrenheit degree " + fahrenheit + " is " + celsius + " in celsius." );
}
else if(tempType.equals(F) || tempType.equals(f)) {
fahrenheit = (9*(celsius/5)+32);
System.out.print("The celsius degree " + celsius + " is " + fahrenheit + " in fahrenheit." );
}
else {
System.out.print("The temperature type is not recognized." );
}
}
}

Categories