I need help calling my method. It says "the method fahrtoCel" is undefined for the "TempConversion".
import java.util.Scanner;
public class TempConversion {
public static void main(String[] args) {
System.out.println("===CONVERTING TEMPERATURE=====");
ConvertTemp();
}
private static void ConvertTemp(){
Scanner s = new Scanner(System.in);
System.out.println("Press 1: Convert Celcius to Fahrenheit\nPress 2: Convert Fahrenheit to Celcius\n");
int select = s.nextInt();
if (select==1){
System.out.print("Enter the fahrenheit: ");
celtoFahr();
} else if (select==2){
System.out.print("Enter the celcius: ");
fahrtoCel();
} else{
System.out.println("exit");
}
private static void celtoFahr(){
double temperature = s.nextDouble();
double celcius = 5.0/9.0*(temperature-32);
}
private static void fahrtoCel(){
double temperature = s.nextDouble();
double fahrenheit = 9.0/5.0*(temperature+32);
}
}
}
You are missing a closing }
private static void ConvertTemp(){
Scanner s = new Scanner(System.in);
System.out.println("Press 1: Convert Celcius to Fahrenheit\nPress 2: Convert Fahrenheit to Celcius\n");
int select = s.nextInt();
if (select==1){
System.out.print("Enter the fahrenheit: ");
celtoFahr();
} else if (select==2){
System.out.print("Enter the celcius: ");
fahrtoCel();
} else{
System.out.println("exit");
}
// add closing curly
}
private static void celtoFahr(){
...
If you do however, s will be out of scope so change your method to accept the Scanner.
e.g.
private static void ConvertTemp(){
Scanner s = new Scanner(System.in);
....
celtoFahr(s);
....
}
private static void celtoFahr(Scanner s){
....
}
If you use an IDE like Eclipse this error will be immediately obvious
Related
I'm new to Java. I am tasked with creating a menu program, one option is to generate a username in the form of first initial and surname.
The method is stringOperation(String f, String s)
Variables are fName and sName.
Here is the code. I have highlighted the areas I need help with. The rest of the code is OK, I think. This is a section of the pseudocode that explains what is required:
stringOperation(String f, String s)
3.1.1 Assign first character of first initial to variable using f.substring(start position, length of string).
3.1.2 Concatenate first initial with users surname.
3.1.3 print username to console.
import java.util.Scanner; // imports scanner class
public class Assessment {
public static void main(String[] args) { //main method
menu(); //call menu method
}
public static void menu() { //method to display menu options
int choice;
String fName;
String sName;
Scanner sc = new Scanner(System.in);
//displays menu options
System.out.println("Welcome");
System.out.println("1. Username");
System.out.println("2. Factorial");
System.out.println("3. Area of triangle");
System.out.println("4. Circumference of circle");
System.out.println("5. Exit");
//asks for user input
do {
System.out.println("Enter your first name");
fName = sc.next();
System.out.println("Enter your surname");
sName = sc.next();
System.out.println("Thank you. Now enter a selection (1-5):");
choice = sc.nextInt();
//menu loop
switch (choice) {
case 1:
**stringOperation(String fName, String sName);**
break;
case 2:
numberFactorial();
break;
case 3:
areaTriangle();
break;
case 4:
circumferenceCircle();
break;
}
}while (choice!=5);
}
**//stringOperation method
private static void stringOperation(String f, String s) {
String initial = f.substring(0,1);
String username = initial + s;
System.out.println("Your username is " + initial + s);
}**
public static void numberFactorial() { //method to calculate factorial of a number
//variables
int number;
int factorial = 1;
int i;
//input
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a number: ");
number = sc.nextInt();
//for loop
for (i = 1; i <= number; i++) {
factorial = factorial * i;
}
System.out.println("Factorial of " + number + " is " + factorial);
}
public static void areaTriangle ()//method to calculate area of a triangle
{ //input
Scanner sc = new Scanner(System.in);
//variables
double width;
double height;
double area;
//input
System.out.println("Enter the width: ");
width = sc.nextInt();
System.out.println("Enter height: ");
height = sc.nextInt();
area = (height * width) / 2;
System.out.println("The area is :" + area);
}
public static void circumferenceCircle ()//method to calculate circumference of a circle
{ //variables
double radius;
double circumference;
Scanner sc = new Scanner(System.in);
System.out.println("Enter radius: ");
radius = sc.nextDouble();
circumference = Math.PI * 2 * radius;
System.out.println("The circumference is : " + circumference);
}
}
If you want to make the method cleaner you could do something like this
private static void stringOperation(String f, String s) {
System.out.println("Your username is " + f.substring(0,1) + s)
}
If you need to refer back to the new username then have a global variable that your method can set to refer to later like this.
private static void stringOperation(String f, String s) {
Assessment.username = f.substring(0,1) + s;
System.out.println("Your username is " + Assessment.username);
}
I have 2 methods in Java.
In the first method, I am asking the user to make a choice, then i want to store this choice for using in the future.
The second method I wrote only to call the first one to use this choice.
Now, I want to use this variable and add it into an ArrayList. Is it possible to do it?
public static void letUserChooseAgain () {
System.out.println("Please choose an option (1/2):");
System.out.println("1. Dollars to Pounds");
System.out.println("2. Pounds to Dollars");
Scanner scanner = new Scanner(System.in);
double userChoice = scanner.nextDouble();
userChoiceToRemember(userChoice);
}
public static void userChoiceToRemember (double number) {
double remember = number;
}
I belive, I understood your requirement properly if it is kindly find the source code
It can be proposed in two different approach:
APPROACH 1
import java.util.ArrayList;
static ArrayList < Double > numbers = new ArrayList < Double > ();
public static void letUserChooseAgain ()
{
System.out.println ("Please choose an option (1/2):");
System.out.println ("1. Dollars to Pounds");
System.out.println ("2. Pounds to Dollars");
Scanner scanner = new Scanner (System.in);
double userChoice = scanner.nextDouble ();
numbers.add (userChoice);
userChoiceToRemember (userChoice);
}
public static void userChoiceToRemember (double number)
{
double remember = number;
System.out.println ("Remembered User Choice :" + numbers.get (0));
}
APPROACH 2
import java.util.ArrayList;
static ArrayList < Double > numbers = new ArrayList < Double > ();
public static void letUserChooseAgain ()
{
System.out.println ("Please choose an option (1/2):");
System.out.println ("1. Dollars to Pounds");
System.out.println ("2. Pounds to Dollars");
Scanner scanner = new Scanner (System.in);
double userChoice = scanner.nextDouble ();
userChoiceToRemember (userChoice);
}
public static void userChoiceToRemember (double number)
{
double remember = number;
numbers.add (remember);
System.out.println ("Remembered User Choice :" + numbers.get (0));
}
I hope the above code will help you, have a nice day !!
If i understand you right you want to safe the number in a array like this?
Array<Double> numbers = new Array<>();
public static void letUserChooseAgain () {
System.out.println("Please choose an option (1/2):");
System.out.println("1. Dollars to Pounds");
System.out.println("2. Pounds to Dollars");
Scanner scanner = new Scanner(System.in);
double userChoice = scanner.nextDouble();
userChoiceToRemember(userChoice);
}
public static void userChoiceToRemember (double number) {
double remember = number;
numbers.add(number);
}
You need to create a data structure that will keep the value that needs to be shared . Normally, it is done by defining a class that have members that keep the data needed by the class and methods that can access and change the members. This simple java class with members and accessor methods is called Java Bean.
Another option in your case is to have a static variable of type ArrayList<Double> to keep user choices.
I would go for a simple class that keeps user choices with a ArrayList<Double> member. An additional class would be the one that controls the flow of your program using the input provided by the user.
In your main class - the entry point to your program you woould need to instantiate both the class that controls the flow and the one that stores the user choices.
Either give it an ArrayList to add to:
public static void letUserChooseAgain () {
Scanner scanner = new Scanner(System.in);
double userChoice = scanner.nextDouble();
ArrayList<Double> myArrayList = new ArrayList<Double>()
userChoiceToRemember(userChoice, myArrayList);
}
public static void userChoiceToRemember (double number, ArrayList<Double> anArrayList) {
anArrayList.add(number);
}
Have the method create it's own
public static void letUserChooseAgain () {
Scanner scanner = new Scanner(System.in);
double userChoice = scanner.nextDouble();
ArrayList<Double> userChoiceInList = userChoiceToRemember(userChoice);
}
public static ArrayList<Double> userChoiceToRemember (double number, ArrayList<Double> anArrayList) {
ArrayList<Double> myArrayList = new ArrayList<Double>()
myArrayList.add(number);
return myArrayList;
}
or add it to the ArrayList outside of the loop
public static void letUserChooseAgain () {
Scanner scanner = new Scanner(System.in);
double userChoice = scanner.nextDouble();
ArrayList<Double> myArrayList = new ArrayList<Double>()
myArrayList.add(userChoiceToRemember(userChoice));
}
public static void userChoiceToRemember (double number) {
return number;
}
That's assuming there's a good reason you can't just:
public static void letUserChooseAgain () {
Scanner scanner = new Scanner(System.in);
ArrayList<Double> myArrayList = new ArrayList<Double>()
myArrayList.add(scanner.nextDouble());
}
I want to do an operation for calculating Body Mass Index in my CalBMI method, which depends on the inputs for CalWt and CalHt methods. Then the CalBMI method will return a BMI answer. How can I do such operation?The operation in my CalBMI
import java.util.*;
public class PracticeMethods_returningValues {
static Scanner type=new Scanner(System.in);
public static void main(String[] args)
{
double Wt=0, Ht=0, BMI;
System.out.println("Your Weight in Kg is: " + CalWt(Wt) );
System.out.println("Your Height in meters is: " + CalHt(Ht) );
System.out.println("Your BMI is: " + CalBMI(Wt,Ht) );
}
public static double CalWt(double a){
System.out.println("Please enter your Weight in lbs: ");
double Wt=(type.nextDouble() * .454); //Converts to Kg.
return Wt;
}
public static double CalHt(double b){
System.out.println("\nPlease enter your Height in inches: ");
double Ht=(type.nextDouble() * .025); //Converts to m.
return Ht;
}
public static double CalBMI(double a, double b){
double BMI=a/(Math.pow(b, 2));
return BMI;
}
}
Please tell me if the formula is wrong
public class BMI {
public static double CalWt(Scanner type) {
System.out.println("Please enter your Weight in lbs: ");
return type.nextDouble() * .45359;
}
public static double CalHt(Scanner type) {
System.out.println("Please enter your Height in inches: ");
return type.nextDouble()* .025;
}
public static double CalBMI() {
Scanner type = new Scanner(System.in);
return CalWt(type) / Math.pow(CalHt(type),2);
}
public static void main(String[] args) {
System.out.println(CalBMI());
}
}
I am receiving errors during compilation. It's expecting a .class. I don't think it should require one. I'm pretty new at coding so forgive my ignorance. I also would like some guidance on how to nullify Case when the user inputs C or F so they can put c or f and not get an error messages.
This is my code:
import java.util.Scanner;
import java.util.InputMismatchException;
public class TempCALC
{
public static void main(String[] args)
{
System.out.println("This Program will allow the user to calculate temperature.");
calculateTemp();
}
private static void calculateTemp() {
int F;
int C;
F=0;
C=1;
Scanner input = new Scanner (System.in);
System.out.println("Please enter a F to convert Fahrenheit to Celsius.");
System.out.println("Please enter a C to convert Celsius to Fahrenheit.");
int option = input.nextInt();
if (int=0) {
System.out.println("Please enter a temperature in degrees Fahrenheit.");
ftoc();
} else if (int= 1) {
System.out.println("Please enter a temperature in degrees Celsius.");
ctof();
} else {
System.out.println("ERROR PLEASE ENTER A F OR A C TO PROCEED!");
}
}
private static void ftoc() {
Scanner input = new Scanner(System.in);
Double celsius = input.nextDouble();
System.out.println(celsius + "celsius is" + ((celsius * 9 / 5.0) + 32) + "Fahrenheite");
calculatetemp();
}
private static void ctof() {
Scanner input = new Scanner(System.in);
Double Fahrenheit = input.nextDouble();
System.out.println(Fahrenheit + "Fahrenheit is" + ((Fahrenheit - 32) * (5 / 9.0)) + "Celsius");
calculatetemp();
}
private static void print(String string); {
System.out.println("\n" + string);
}
}
if (int=0)
This is not valid Java syntax. int is a type not a variable. You meant to write:
if(option == 0)
Notice the == (comparison) instead of = (assignment).
This is the flow you meant to implement:
if (option == 0) {
System.out.println("Please enter a temperature in degrees Fahrenheit.");
ftoc();
} else if (option == 1) {
System.out.println("Please enter a temperature in degrees Celsius.");
ctof();
} else {
System.out.println("ERROR PLEASE ENTER A F OR A C TO PROCEED!");
}
If you wish for the user to enter f or c instead to mention the degrees, you will need to use:
String option = input.next();
To get a String instead of an Integer.
And then:
if (option.equals('F') { ... }
else if(option.equals('C') { ... }
else { ... }
If you want your input to be case-insensitive take a look at toLowerCase or toUpperCase and apply it to your needs (there is an answer here that shows its use).
int is reserved key word in java you can not used it with expression to compare,you should use his value like this:
int option = input.nextInt();
if (option ==0) {
Not if (int=0) { and = is used to assign but == is used to check equality.
You first have to check the validation in your ifs statements, as the previous answers mentioned.
Also, I think that your actual conversion formulas are swapped.
Then, in order to receive either 'C' or 'c':
Read a String, instead of an int:
String option = input.next();
And then convert all the input to lowercase:
if (option.toLowerCase().equals("f"))
Here is the complete example:
public class TempCALC {
public static void main(String[] args) {
System.out.println("This Program will allow the user to calculate temperature.");
calculateTemp();
}
private static void calculateTemp() {
Scanner input = new Scanner(System.in);
System.out.println("Please enter a F to convert Fahrenheit to Celsius.");
System.out.println("Please enter a C to convert Celsius to Fahrenheit.");
String option = input.next();
if (option.toLowerCase().equals("f")){
System.out.println("Please enter a temperature in degrees Fahrenheit.");
ftoc();
}else if (option.toLowerCase().equals("c")){
System.out.println("Please enter a temperature in degrees Celsius.");
ctof();
}else{
System.out.println("ERROR PLEASE ENTER A F OR A C TO PROCEED!");
}
}
private static void ftoc() {
Scanner input = new Scanner(System.in);
Double celsius = input.nextDouble();
System.out.println(celsius + "celsius is" + ((celsius * 9 / 5.0) + 32) + "Fahrenheite");
calculatetemp();
}
private static void ctof() {
Scanner input = new Scanner(System.in);
Double Fahrenheit = input.nextDouble();
System.out.println(Fahrenheit + "Fahrenheit is" + ((Fahrenheit - 32) * (5 / 9.0)) + "Celsius");
calculatetemp();
}
private static void print(String string){
System.out.println("\n" + string);
}
private static void calculatetemp(){
System.out.println("\nInside calculateTemp");
}
}
This is a code for a foundations class that I'm working on currently. I keep getting an error, but I'm not sure what I've done incorrectly. Any help would be greatly appreciated. Thanks!
package miles;
public class Miles {
private int miles;
private int gas;
public static int mpg;
public void setMiles(int miles) {
this.miles = miles;
}
public void setGas(int gas){
this.gas = gas;
}
public int getMpg(){
mpg = miles/gas;
return mpg;
}
}
package miles;
import java.util.Scanner;
public class MilesDriver {
public static void main(String [] args) {
Miles gas = new Miles();
Scanner input = new Scanner(System.in);
System.out.println("Enter Miles: ");
int setGas = input.nextInt();
System.out.println("Enter Gas: ");
int setMiles = input.nextInt();
System.out.printf("MPG: %n%s%n", gas.getMpg() );
}
}
You actually forgot to set the values of your object :
System.out.println("Enter Miles: ");
int setMiles = input.nextInt();
gas.setMiles(setMiles);
System.out.println("Enter Gas: ");
int setGas = input.nextInt();
gas.setGas(setGas);
Try not to give a variables name the same name as a method of your object. You'll end confusing yourself and get stuck.