Need help please. This program is supposed to be a do while loop and convert fahrenheit to celcius and vise versa using user defined methods but as of right now it just doesn't work. I need to make a do while loop in main method too, but was just trying to link the user entered letters but that doesn't seem to be working either. Can someone point me in the right direction please,

import java.util.*;

public class p7
{
 static Scanner kb = new Scanner(System.in);

 public static void main(String[] args)

 {

  char letter, C, F, Q;
  int cel,fah, fahrenheit, celsius;


  displayMenu();

  if(letter == 'C')
     {
      celToFar();
	 }
  else if (letter == 'F')
	 {
	 farToCel();
	 }
  else (letter == 'Q');
	 {
	  System.Exit(0);
     }

  displayMenu();

 }


public static char displayMenu()
	 {
	  do
		{
 		 char letter, C, F, Q;
 		 letter = kb.next().charAt(0);
		 System.out.println("Please select one of the following: \n");
		 System.out.println("\tF - to convert Fahrnheit to Celsius ");
		 System.out.println("\tC - to convert Celsius to Fahrnheit\n");
		 System.out.println("\tQ - to Quit.");
		 System.out.printf("%nChoice:");
		}

	while(letter.toUpperCase() == 'C' || letter.toUpperCase() == 'F' || letter.toUpperCase() == 'Q');
	 }

public static int celToFar()
	{
	 do
	 {
	 System.out.println("Please enter the Celsius temperature:");
	 cel = kb.nextInt();
	 celsius = (cel) * 9 / 5 + 32;
	 System.out.println("The temperature " + cel + " Celsius is " + celsius + "Fahrenheit.");
	 }
	while(letter.toUpperCase() != 'F');
	}

public static int farToCel()
	{
	 do
 	 {
	 System.out.println("Please enter the Fahrenheit temperature:");
	 fah = kb.nextInt();
	 fahrenheit = ((fah) - 32) * 5 / 9;
	 System.out.println("The temperature " + fah + " Fahrenheit is " + fahrenheit + " Celsius.");
 	 }
	while(letter.toUpperCase() != 'C');
	}
}

Recommended Answers

All 7 Replies

When you are thinking about loop, you need to think which part of your code will be rerun over and over again. Then, you plug in a loop and move the code inside your new loop. In your case, it looks like these code lines below are executing over and over again...

displayMenu();

if(letter == 'C') {
  celToFar();
}
else if (letter == 'F') {
  farToCel();
}
else (letter == 'Q') {
  System.Exit(0);
}

However, in your if-else statement, you have 'else' statement which is the exit condition. You would just need to remove the else and use the condition checking as your do-while loop condition. That's it.

I continue to recieve errors which are listed down at the bottom. When I run the program it only displays the menu but ends after any is entered. What am I missing? Why isn't it recognizing the variable "letter"? Do I need to declare my variables outside of the loop but still within the "do"? The character dereferencing error, do I need to change the capital letters to lowercase letter? That doesn't make much sense to me from what I keep rereading... stupid book! Please remember I am new to programming so these "stupid" questions are serious questions to my simple mind :)

import java.util.*;

public class p7
{
 static Scanner kb = new Scanner(System.in);

 public static void main(String[] args)

 {

  char letter, C, F, Q;
  int cel,fah, fahrenheit, celsius;

	do
	{
	  displayMenu();

	  if(letter != 'C')
     {
      celToFar();
	 }
	  else if (letter != 'F')
	 {
	 farToCel();
	 }

	  displayMenu();
	}
	while(letter.toUpperCase() != 'C' || letter.toUpperCase() != 'F' || letter.toUpperCase() != 'Q');
 }

public static char displayMenu()
	 {
	  do
		{
		 char letter, C, F, Q;
 		 letter = kb.next().charAt(0);
		 System.out.println("Please select one of the following: \n");
		 System.out.println("\tF - to convert Fahrnheit to Celsius ");
		 System.out.println("\tC - to convert Celsius to Fahrnheit\n");
		 System.out.println("\tQ - to Quit.");
		 System.out.printf("%nChoice:");
		}

	while(letter.toUpperCase() != 'C' || letter.toUpperCase() != 'F' || letter.toUpperCase() != 'Q');
	 }

public static int celToFar()
	 {
	 int cel = 0, celsius = 0;
	 System.out.println("Please enter the Celsius temperature:");
	 cel = kb.nextInt();
	 celsius = (cel) * 9 / 5 + 32;
	 System.out.println("The temperature " + cel + " Celsius is " + celsius + "Fahrenheit.");
	 }

public static int farToCel()
	{
	 int fah = 0, fahrenheit = 0;
	 System.out.println("Please enter the Fahrenheit temperature:");
	 fah = kb.nextInt();
	 fahrenheit = ((fah) - 32) * 5 / 9;
	 System.out.println("The temperature " + fah + " Fahrenheit is " + fahrenheit + " Celsius.");
 	}
}

C:\Documents and Settings\Chalandria\Desktop\p7.java:29: char cannot be dereferenced
while(letter.toUpperCase() != 'C' || letter.toUpperCase() != 'F' || letter.toUpperCase() != 'Q');
^
C:\Documents and Settings\Chalandria\Desktop\p7.java:29: char cannot be dereferenced
while(letter.toUpperCase() != 'C' || letter.toUpperCase() != 'F' || letter.toUpperCase() != 'Q');
^
C:\Documents and Settings\Chalandria\Desktop\p7.java:29: char cannot be dereferenced
while(letter.toUpperCase() != 'C' || letter.toUpperCase() != 'F' || letter.toUpperCase() != 'Q');
^
C:\Documents and Settings\Chalandria\Desktop\p7.java:45: cannot find symbol
symbol : variable letter
location: class p7
while(letter.toUpperCase() != 'C' || letter.toUpperCase() != 'F' || letter.toUpperCase() != 'Q');
^
C:\Documents and Settings\Chalandria\Desktop\p7.java:45: cannot find symbol
symbol : variable letter
location: class p7
while(letter.toUpperCase() != 'C' || letter.toUpperCase() != 'F' || letter.toUpperCase() != 'Q');
^
C:\Documents and Settings\Chalandria\Desktop\p7.java:45: cannot find symbol
symbol : variable letter
location: class p7
while(letter.toUpperCase() != 'C' || letter.toUpperCase() != 'F' || letter.toUpperCase() != 'Q');
^
6 errors

You do not need do-while in every single method you are working on. You need only one in your main.

The teacher wants a do while loop in main and then one in diplayMenu(). sadly that is what is making this hard.

The reason you are getting cannot dereference error is because you are trying to call a method (toUppercase()) on a primitive type.

You can try doing it like this.

public static void main(String[] args)

 {

  char letter= ' '; //initialize this variable
  char C, F, Q;
  int cel,fah, fahrenheit, celsius;
  Character c = null; //initialize this variable
  

	do
	{
	  displayMenu();

	  if(letter != 'C')
     {
      celToFar();
	 }
	  else if (letter != 'F')
	 {
	 farToCel();
	 }

	  displayMenu();
	}
//c is of type Character (a non primitive type of char). This has a function .uppercase that takes in a char //parameter
	while(c.toUpperCase(letter) != 'C' || c.toUpperCase(letter) != 'F' || c.toUpperCase(letter) != 'Q');
 }

In addition to that, your functions are done wrong.

Here is your code:

public static char displayMenu() /*public static char displayMenu. This header tells the app that when this method is used, it RETURNS a value of type char. Yours doesn't.

I don't know if you want to return anything. If you do at the end of the method put. return [value].

If not, change the char to void.*/
	 {
	  do
		{
		 char letter, C, F, Q;
 		 letter = kb.next().charAt(0);
		 System.out.println("Please select one of the following: \n");
		 System.out.println("\tF - to convert Fahrnheit to Celsius ");
		 System.out.println("\tC - to convert Celsius to Fahrnheit\n");
		 System.out.println("\tQ - to Quit.");
		 System.out.printf("%nChoice:");
		}

	while(letter.toUpperCase() != 'C' || letter.toUpperCase() != 'F' || letter.toUpperCase() != 'Q');
	 }

I made the changes and got the error that the varible c cannot be found(See below for exact error message). I added the varibles inside the loop hoping that might fix it but it is still giving me the same error. Thank you by the way for explaining my previous mistakes.

I also think that my do while loop for main is still messed up. When I run the program it gives the menu where the user can enter thier choice but when enter is hit the program closes "Press any key to continue..." (Closes).

import java.util.*;

public class p7
{
 static Scanner kb = new Scanner(System.in);

 public static void main(String[] args)

 {

  char letter = ' ';
  char C, F, Q;
  int cel,fah, fahrenheit, celsius;
  Character c = null;

	do
	{
	  displayMenu();

	  if(letter != 'C')
     {
      celToFar();
	 }
	  else if (letter != 'F')
	 {
	  farToCel();
	 }

	  displayMenu();
	}
	while(letter != 'Q'); System.exit(0);
 }

public static void displayMenu()
	 {
	  do
		{
		 char letter = ' ';
		 char C, F, Q;
		 Character c = null;
 		 letter = kb.next().charAt(0);
		 System.out.println("Please select one of the following: \n");
		 System.out.println("\tF - to convert Fahrnheit to Celsius ");
		 System.out.println("\tC - to convert Celsius to Fahrnheit\n");
		 System.out.println("\tQ - to Quit.");
		 System.out.printf("%nChoice:");
		}

     while( c.toUpperCase() != 'C' || c.toUpperCase() != 'F' || c.toUpperCase() != 'Q');
	 }

public static void celToFar()
	 {
	 int cel = 0, celsius = 0;
	 System.out.println("Please enter the Celsius temperature:");
	 cel = kb.nextInt();
	 celsius = (cel) * 9 / 5 + 32;
	 System.out.println("The temperature " + cel + " Celsius is " + celsius + "Fahrenheit.");
	 }

public static void farToCel()
	{
	 int fah = 0, fahrenheit = 0;
	 System.out.println("Please enter the Fahrenheit temperature:");
	 fah = kb.nextInt();
	 fahrenheit = ((fah) - 32) * 5 / 9;
	 System.out.println("The temperature " + fah + " Fahrenheit is " + fahrenheit + " Celsius.");
 	}
}

C:\Documents and Settings\Chalandria\Desktop\p7.java:49: cannot find symbol
symbol : variable c
location: class p7
while( c.toUpperCase() != 'C' || c.toUpperCase() != 'F' || c.toUpperCase() != 'Q');
^
C:\Documents and Settings\Chalandria\Desktop\p7.java:49: cannot find symbol
symbol : variable c
location: class p7
while( c.toUpperCase() != 'C' || c.toUpperCase() != 'F' || c.toUpperCase() != 'Q');
^
C:\Documents and Settings\Chalandria\Desktop\p7.java:49: cannot find symbol
symbol : variable c
location: class p7
while( c.toUpperCase() != 'C' || c.toUpperCase() != 'F' || c.toUpperCase() != 'Q');
^
3 errors

The 'c' you are looking for should be 'letter' variable...

I can understand your teacher purpose of using 2 loops. One loop in displayMenu() is to ensure that the return letter is one of 'C', 'F', or 'Q'. If the input is not one of those, loop it to ask the user to reenter again. The loop in the main() is to check which letter the user has selected. If it is not 'Q', then loop it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.