Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Problem with adapting a C++ program to Java

  1. #1
    Junior Member
    Join Date
    Apr 2020
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with adapting a C++ program to Java

    This program inputs temperature readings. Works easy in C++ but it's more difficult in Java. The current error I'm getting is the compiler is telling me "ch cannot be resolved to a variable." How can I fix this? The user enters 'y' telling the program he wants to enter another temperature reading, or else he enters 'n' which should break the loop and end the program and calculate the results.

    import java.util.Scanner;
     
    public class TemperatureReadings
    {
    	public static void main(String args[])
    	{
    		Scanner myScanner = new Scanner(System.in);
    		int count = 0;
    		double temperature = 0.0;
    		double average = 0.0;
     
    		do
    		{
    			System.out.print("Enter a temperature reading: ");
    			temperature = myScanner.nextDouble();
    			average += temperature;
    			count++;
     
    			System.out.print("Do you want to enter another? (y/n)");
    			char ch = myScanner.next().charAt(0); // right here compiler error
    		} while(ch == 'y');	
     
    		average /= count;
    		System.out.println("The average temperature reading is " + average);
    	}
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem with adapting a C++ program to Java

    Your error location is on the wrong line. It should be on the while statement.
    The while statement requires that variables being tested be declared outside of the while loop.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2020
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with adapting a C++ program to Java

    Thank You. I got it going now. All I need to do now is adjust the output so my program only shows one or two decimal places after the decimal point. I'll do some research.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem with adapting a C++ program to Java

    adjust the output so my program only shows one or two decimal places after the decimal point.
    Look at the printf method and the Formatter class for how to write formatting text.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Problem with Java Program
    By dhmhgr in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 5th, 2020, 06:59 AM
  2. Problem with Grading calculator in java program
    By dwnhiler in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 27th, 2014, 04:10 AM
  3. Java - Problem getting my program functions
    By maths94 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 4th, 2014, 02:42 AM
  4. Java Program String Arrays Problem
    By incxx in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 12th, 2013, 11:57 PM
  5. Problem with java program and CPU usage
    By Bahramudin in forum Java Theory & Questions
    Replies: 3
    Last Post: July 25th, 2012, 09:26 AM