-1

I'm trying to make a USD <-> JPY converter. If the numbers seem off to you, trust me - I did the math. Also, I know the spacing's off, but it doesn't matter. Morph it in N++ if you need to.

import java.util.Scanner;
import java.lang.Math;

public class jpy_usd {
public static void main(String[] args) {
    //Initializing variables and the scanner
    boolean isSourceUSD;
    double usd;
    double usdMult = 0.00803568;
    int jpy;
    double jpyMult = 124.449;
    Scanner scanner = new Scanner(System.in);

    //Selecting an input currency and checking for valid input arguments
    System.out.println("Choose your input currency. Type \'USD\' for U.S. Dollars or \'JPY\' for Japanese Yen.");
    while(isSourceUSD != true && isSourceUSD != false) {
        if(scanner.nextLine == "USD") {
            isSourceUSD = true;
        } else if(scanner.nextLine == "JPY") {
            isSourceUSD = false;
        } else {
            System.out.println("Invalid argument. Please use \'USD\' for U.S. Dollars or \'JPY\' for Japanese Yen.");
        }
    }

    //Asking for the input in the chosen currency and converting
    if(isSourceUSD) {
        System.out.println("Please enter the value to convert");
        usd = scanner.nextDouble;
        System.out.println(usd " in JPY is " (Math.round(usd * jpyMult)));
    } else if(!isSourceUSD) {
        System.out.println("Please enter the value to convert");
        jpy = scanner.nextInt;
        System.out.println(jpy " in USD is " (Math.round(jpy * usdMult)));
        }
    }
}

If you want me to add the error messages, I suppose I could. Without much explanation, there's 6 errors split evenly across these two lines:

System.out.println(usd " in JPY is " (Math.round(usd * jpyMult)));
System.out.println(jpy " in USD is " (Math.round(jpy * usdMult)));

There's two of each error - one per line - so the similarities in the lines have to be the same issues (duh). So, anybody got any clues?

P.S.: If there's anyhing wrong with the code other than that, please do mention it.

P.S. 2: I know I'm basically making this code public by putting every single character into this block of text, but please don't steal. I know someone's about to do it, but please don't. ~wundr

  • `while(isSourceUSD != true && isSourceUSD != false)` Tell me what is wrong with this logic. By my estimate, your code should not even touch the portions related to the `Scanner`. – Tim Biegeleisen Aug 18 '15 at 04:29
  • If you want to concatenate Strings you should use a `+` : `System.out.println(usd + " in JPY is " + (Math.round(usd * jpyMult)));` – das Keks Aug 18 '15 at 06:29

1 Answers1

0

There are ideally many problems with your code, starting with the Naming conventions to method names, String comparison. Below is the minimal code changes required to make your code work:-

import java.util.Scanner;
import java.lang.Math;

public class jpy_usd {
public static void main(String[] args) {
    //Initializing variables and the scanner
    String isSourceUSD;
    double usd;
    double usdMult = 0.00803568;
    int jpy;
    double jpyMult = 124.449;
    Scanner scanner = new Scanner(System.in);

    //Selecting an input currency and checking for valid input arguments
    System.out.println("Choose your input currency. Type \'USD\' for U.S. Dollars or \'JPY\' for Japanese Yen.");

        if(scanner.nextLine() .equalsIgnoreCase( "USD")) {
            isSourceUSD = "USD";
        } else if(scanner.nextLine() .equalsIgnoreCase( "JPY")) {
            isSourceUSD ="JPY" ;
        } else {
            isSourceUSD="NA";
            System.out.println("Invalid argument. Please use \'USD\' for U.S. Dollars or \'JPY\' for Japanese Yen.");
        }

    //Asking for the input in the chosen currency and converting
    if(isSourceUSD.equalsIgnoreCase("USD")) {
        System.out.println("Please enter the value to convert");
        usd = scanner.nextDouble();
        System.out.println(usd +" in JPY is "+ (Math.round(usd * jpyMult)));
    } else if(isSourceUSD.equalsIgnoreCase("JPY")) {
        System.out.println("Please enter the value to convert");
        jpy = scanner.nextInt();
        System.out.println(jpy+ " in USD is " +(Math.round(jpy * usdMult)));
        }
    else{
        System.out.println("Do nothing!!!");
    }
    scanner.close();
    }
}
Amit Bhati
  • 5,569
  • 1
  • 24
  • 45
  • Thank you very much. It seems like I need to get back up to par on my Java. Inputting JPY for currency doesn't work, but I'll check it out. – wundrweapon Aug 18 '15 at 15:07