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 3 of 3

Thread: Difficulty calling method in another method

  1. #1
    Junior Member
    Join Date
    Sep 2019
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Difficulty calling method in another method

    Hi, first time on here so hope i am in the right place. I have created the methods gradeTemp and getValidTemp, which i can run independently. However when i attempt to call them both in tempGrading i cannot. To be clear what I am trying to do in tempGrading method is call getValid temp to provide me with a temp for which I can then call gradeTemp to give a rating. In addition to the code below I have tried including in tempGrading: vTemp= temp; rating=gradeTemp(vTemp); but cant make work in either case.

    import java.util.*;

    public class Temperature
    {


    static void gradeTemp() {

    int temp;



    Scanner sc=new Scanner(System.in);
    System.out.println("Enter temperature");

    temp = sc.nextInt();
    System.out.print("The temp is ");
    if(temp>=70 && temp<=100) {
    System.out.println("Very Hot");
    } else if (temp>=60 && temp<=69) {
    System.out.println("Hot");
    }
    else if (temp>=50 && temp<=59) {
    System.out.println("Warm");}
    else if (temp>=40 && temp<=49) {
    System.out.println("Cool");}
    else if (temp>=0 && temp<=39) {
    System.out.println("Cold");}


    }
    public static int getValidTemp() {
    Scanner sc=new Scanner(System.in);
    int temp;
    System.out.println ("Enter a Temperature in range 0 to 100");
    mark=sc.nextInt();
    while (temp < 0 || temp > 100)
    {
    System.out.println("Invalid temperature !");
    System.out.println ("Enter a mark in range 0 to 100");
    mark=sc.nextInt();
    }
    return mark;}

    public void tempGrading() {
    Scanner sc=new Scanner(System.in);
    char response;
    int temp, vTemp;

    System.out.println("*********** Temperature Grading *********");
    System.out.println("Enter temperature to receive rating");


    do {
    String rating;
    temp = getValidTemp();
    rating= gradeTemp();
    System.out.println("Your rating is" + rating);

    System.out.print("another go (y/n)? =>");
    response = sc.next().charAt(0);
    } while (response == 'y'|| response =='Y');
    }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Difficulty calling method in another method

    when i attempt to call them both in tempGrading i cannot
    Please explain. If there are error messages, copy the full text and paste it here.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2019
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Difficulty calling method in another method

    Quote Originally Posted by Norm View Post
    Please explain. If there are error messages, copy the full text and paste it here.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    Thanks for your response, I have now resolved, my error was declaring formal parameters.

Similar Threads

  1. Finding difficulty with getResource() method to find path.
    By jerinjose61 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: August 26th, 2014, 12:35 AM
  2. Method not calling?
    By NTWolf1220 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 20th, 2013, 12:04 PM
  3. calling this method
    By antnas in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 2nd, 2012, 01:32 PM
  4. Difficulty with method headers.
    By tripline in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 30th, 2011, 10:58 AM