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

Thread: Unreachable statement problem

  1. #1
    Junior Member
    Join Date
    Jul 2018
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Unreachable statement problem

    I have been working on the following for hours only to get unreachable code for line 44 return (sum);. Need some fresh eyes asap please.

    package evenFibonaccisum;
     
    import java.io.*;
    import java.math.*;
    import java.text.*;
    import java.util.*;
    import java.util.regex.*;
     
    public class Sum {
        static int sumEvenFib(int start, int end) {
            int fn1 = start; //fn_1
            int fn2 = getNextTerm(start); //fn_2
            int fn = 0; //current term
            int sum = fn1 + fn2; //holds sum of even terms
                while (fn<=end) {
                    fn=fn1+fn2;
                    fn1=fn2;
                    fn2=fn;
                       if(fn>=end){
                           break ;
                        }
                       else{
     
                           if(fn%2==0){
     
                               sum=sum+fn;
                            }
                        }
                }
     
            int getNextTerm; 
            int x = 0; {
                int a=0;
                int b=1;
                int fib=1;
                for(int i=0; i<=x; i++){ 
                    fib=a+b;  
                    a=b;  
                    b=fib;        
                }
                return (fib);  
            } 
     
          return (sum);
        }
     
    private static int getNextTerm(int start) {
    		// TODO Auto-generated method stub
    		return 0;
    	}
     
    private static final Scanner scanner = new Scanner(System.in);
     
    public static void main(String[] args) throws IOException {
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
     
        int start = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])*"); 
     
        int end = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])*");
     
        int res = sumEvenFib(start, end);
     
        bufferedWriter.write(String.valueOf(res));
        bufferedWriter.newLine();
     
        bufferedWriter.close();
     
        scanner.close();
     
        }
       }
    Last edited by Norm; July 19th, 2018 at 05:25 AM. Reason: Added code tags

  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: HELP...I CAN'T FIGURE THIS OUT

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

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

    to get highlighting and preserve formatting.

    I've done it this time for you.

    There is a useless pair of {}s in the code starting at this line:
    int x = 0; {
    Remove that { and its pairing } and you will see two return statements immediately next to each other.
    The second return statement is unreachable code
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2018
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP...I CAN'T FIGURE THIS OUT

    Thank you. Removed the {}s. How do I correct for this unreachable code to ensure that I am able to get a result from my input?

  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: HELP...I CAN'T FIGURE THIS OUT

    How do I correct for this unreachable code
    Remove one of the return statements so there is only one. For example:
       return val1;
       return val2;    // this statement unreachable
    Removing one of the above return statements so that there is only one return statement will remove the unreachable error.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Aug 2018
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Unreachable statement problem

    Hello,

    here is in Bold the statement that is unreachable. I will intentionally not omit the [code] tags.

    static int sumEvenFib(int start, int end) {
    int fn1 = start; //fn_1
    int fn2 = getNextTerm(start); //fn_2
    int fn = 0; //current term
    int sum = fn1 + fn2; //holds sum of even terms
    while (fn<=end) {
    fn=fn1+fn2;
    fn1=fn2;
    fn2=fn;
    if(fn>=end){
    break ;
    }
    else{

    if(fn%2==0){

    sum=sum+fn;
    }
    }
    }

    int getNextTerm;
    int x = 0; {
    int a=0;
    int b=1;
    int fib=1;
    for(int i=0; i<=x; i++){
    fib=a+b;
    a=b;
    b=fib;
    }
    return (fib);
    }

    return (sum);
    }

    I believe this code needs a little bit of refactoring.

    Regards
    Alin

Similar Threads

  1. Cant Figure it out. Can someone help me?
    By EricIsaiah in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 28th, 2014, 02:17 AM
  2. I can't figure this out! Please help!
    By angyvill85 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 2nd, 2012, 03:36 AM
  3. [SOLVED] can't figure out..
    By darego in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 10th, 2010, 02:26 PM
  4. I'm new at this and can't figure it out
    By jaheh06 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 22nd, 2010, 08:44 AM

Tags for this Thread