0

This is my first post so sorry for any misunderstandings you may have, I made this calculator in C++ and it worked wonderful so I decided to make it in java. When I run the program it gives me no errors but my if statements inside my while loop don't work

import java.util.Scanner;

public class ohmlaw 
{
    public static void main(String args[])
 {
    float current;
    float resistance;
    float voltage;
    String calchoice = new String();
    Scanner cc = new Scanner(System.in);
    System.out.println("OHMCAL, OHM'S LAW CALCULATOR BY RASHAAD BRYAN");
    System.out.println();
    System.out.println();
    System.out.println();
    System.out.println("Instructions");
    System.out.println("If you want to calculate the voltage type voltage");
    System.out.println("If you want to calculate the current type current");
    System.out.println("If you want to calculate the resistance type resistance");
    System.out.println("If you want to stop the program type stop");
    System.out.println();
    System.out.println();
    System.out.println();
    System.out.print("Please enter calculation choice ");
    calchoice = cc.nextLine();
    System.out.println();
    while (calchoice != "stop")
    {
        if (calchoice == "current")
        {
            System.out.print("Please enter voltage (V) ");
            voltage = cc.nextFloat();
            System.out.println();
            System.out.print("Please enter resistance () ");
            resistance = cc.nextFloat();
            System.out.println();
            current = voltage/resistance;
            System.out.println("The current is = " + current  + "A");
            System.out.println();
        }
        if (calchoice == "voltage")
        {
            System.out.print("Please enter current (V) ");
            current = cc.nextFloat();
            System.out.println();
            System.out.print("Please enter resistance () ");
            resistance = cc.nextFloat();
            System.out.println();
            voltage = current * resistance;
            System.out.println("The voltage is = " + voltage  + "V");
            System.out.println();
        }
        if (calchoice == "resistance")
        {
            System.out.print("Please enter Voltage (V) ");
            voltage = cc.nextFloat();
            System.out.println();
            System.out.print("Please enter current (I) ");
            current = cc.nextFloat();
            System.out.println();
            resistance = voltage/current;
            System.out.println("The resistance is = " + resistance  + "");
            System.out.println();
        }
        System.out.print("Please enter calculation choice ");
        calchoice = cc.nextLine();
        System.out.println();
        System.out.println();
    }
    System.out.println("Thank you for using OHMCAL, have a nice day :D");
    System.out.println();
}

}

RashaadBryan
  • 55
  • 1
  • 1
  • 6

2 Answers2

0

Comparing strings in Java using == doesn't do what you want. Use calchoice.equals(...) instead.

jacobm
  • 13,790
  • 1
  • 25
  • 27
0

just use .equals() instead of ==

Secondo
  • 451
  • 4
  • 9