Results 1 to 6 of 6

Thread: simple java seems breaking head

  1. #1
    Join Date
    Jun 2008
    Location
    India
    Beans
    252
    Distro
    Ubuntu 10.04 Lucid Lynx

    simple java seems breaking head

    Code:
    import java.util.Scanner;
    
    
    
    class Calculation{
    
    	
    
    	private int noRange;
    
    
    
    	
    
    	public void getData(int no)
    
    	{
    
    		noRange=no;
    
    	}
    
    	public int calculateSeries()
    
    	{
    
    		int data=1;
    
    		while(data<=noRange)
    
    		{
    
    			if(data%2==0)
    
    				return -data;
    
    			else
    
    				{
    
    					return data;
    
    				}
    
    			data++;
    
    		}
    
    	}
    
    }
    
    public class Series{
    
    	public static void main(String args[])
    
    	{
    
    		Calculation calc = new Calculation();
    
    		Scanner r = new Scanner(System.in);
    
    		System.out.println("Enter the Integer No:");
    
    		calc.getData(r.nextInt());
    
    		
    
    		System.out.println("The series are:"+calc.calculateSeries());
    
    	}
    
    				
    
    }
    Here I have programmed for the output

    Enter the Integer no:6
    1-2+3-4+5-6

    likewise according to input..

    but i get error what is the problem dude
    by
    Visit me
    Click

  2. #2
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: simple java seems breaking head

    Fix this code:
    Code:
    			if(data%2==0)
    
    				return -data;
    
    			else
    
    				{
    
    					return data;
    
    				}
    
    			data++;
    Then maybe the issue with your code will be corrected.

    Btw, above, the value of 'data' is never incremented. Your function always returns 1, if the value of noRange is 1 or greater. Otherwise your function doesn't return anything... oops!

    P.S. Typically one calls their set-accessor methods with the prefix "set", not "get". Consider renaming your function getData to setData.

  3. #3
    Join Date
    Dec 2006
    Location
    USA
    Beans
    278
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: simple java seems breaking head

    while(data<=noRange)

    the condition is not guaranteed to run once so there's no guarantee that it will ever return. You need to add a return statement after the while loop.

    data++;

    is unreachable because of your return statements

  4. #4
    Join Date
    Feb 2009
    Beans
    1,469

    Re: simple java seems breaking head

    Holy whitespace, Batman!

  5. #5
    Join Date
    Jun 2008
    Location
    India
    Beans
    252
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: simple java seems breaking head

    How it will return 1 always?
    by
    Visit me
    Click

  6. #6
    Join Date
    Jul 2009
    Location
    UK
    Beans
    222
    Distro
    Ubuntu

    Re: simple java seems breaking head

    you only return a value once.. what you can do is to have a while loop where you will call the method calculateSeries, but each time u will feed to it the value returned from the previous run.. Don't know if this does makes sense to you but it is an easy solution if u want to return a value every time..

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •