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: What is the issue with my code

  1. #1
    Junior Member
    Join Date
    Jun 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default What is the issue with my code

    Hello,

    I am trying to learn Java and ran into a problem with my code that I can seem to figure out. On line 17 it says this method must return a result type int. any guides would be appreciated.

     import java.util.Scanner;
     
    public class Sticker {
     
    	public static void main(String[] args) {
    		Scanner scan = new Scanner(System.in);
    		//Variables
    		System.out.print("Enter a value for wc: ");
    		int wc = scan.nextInt();
    		System.out.print("Enter a value for hc: ");
    		int hc = scan.nextInt();
    		System.out.print("Enter a value for ws: ");
    		int ws = scan.nextInt();
    		System.out.print("Enter a value for hs: ");
    		int hs = scan.nextInt();
    	}
    		public static int solve(int wc, int hc, int ws, int hs) {
    			if (wc -ws < 2)
    				return(1);
    		else 
    			if (hc - hs <=2)
    				return(0);
     
     
     
    		}
    }
    Last edited by Mr_anon; June 6th, 2021 at 08:46 PM.

  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: What is the issue with my code

    The compiler requires that the value method return an int value for ALL possible conditions. The current code only returns values inside of if statements.
    What if those if statements are not true?
    There needs to be a return statement for when the if statements are not true.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What is the issue with my code

    Thanks for your reply Norm, I was able to get past all my errors. Now i can seem to figure out why i'm not outputting my results? Any Ideas?

    import java.util.Scanner;
     
    public class Sticker {
     
    	public static void main(String[] args) {
    		Scanner scan = new Scanner(System.in);
    		//Variables
    		System.out.print("Enter a value for wc: ");
    		int wc = scan.nextInt();
    		System.out.print("Enter a value for hc: ");
    		int hc = scan.nextInt();
    		System.out.print("Enter a value for ws: ");
    		int ws = scan.nextInt();
    		System.out.print("Enter a value for hs: ");
    		int hs = scan.nextInt();
    	}
    		public static void solve(int wc, int hc, int ws, int hs) {
    			if (wc - ws > 2) {
    				System.out.println(1);			
    		}else if 
    			(hc - hs < 2) {
    			System.out.println(0);
     
    			}
     
    		}
    }

  4. #4
    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: What is the issue with my code

    why i'm not outputting my results? Any Ideas?
    Same problem as before. There is a way for none of the if statements to be true. If they are all false, nothing is printed.
    Add a print statement at the end of the method that prints out the values being tested in the if statements so you can see why the if statements are not true,
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Niggling Issue in code
    By keepStriving in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 3rd, 2013, 05:13 PM
  2. Help With Flowchart (Non-Code Issue)
    By Sephyncloud in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 27th, 2013, 02:03 PM
  3. issue with jsp code
    By zulu1900 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 20th, 2013, 01:08 PM
  4. Issue in a code
    By ayyappad in forum What's Wrong With My Code?
    Replies: 6
    Last Post: August 3rd, 2013, 07:17 PM
  5. I am having an issue with my code
    By gmamagoodwin in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 9th, 2012, 11:36 PM

Tags for this Thread