Learning methods, having difficulty printing out 1 to 10 and checking

Basic problem, been stuck for ages. Help greatly appreciated.

Page 1 of 1

14 Replies - 2330 Views - Last Post: 20 August 2009 - 11:26 AM Rate Topic: -----

#1 jdi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 20-August 09

Learning methods, having difficulty printing out 1 to 10 and checking

Posted 20 August 2009 - 05:45 AM

hey :)

im learning methods and want i want to do is..

-write a method that takes two integer parameters
-prints out all the numbers between them (eg parameters 1 and 10, prints 1, 2, 3, 4, 5, .. 10)
-then next to the number prints whether it is odd or even

then, call this method in the main()

i wrote this code which did this perfectly, although my instructor pointed out that i didn't follow the criteria of doing everything in the method, then calling the method in the main.

this was my original, working code:

/**

 * This program will print out numbers between two parameters, then test if they are odd or even

 * 

 */





class Exercise1c

{

	public static void main(String[] args)

	{

		for (int a = 1; a <= 10; a++)

		{

			boolean result = oddEven(a);

			System.out.println(result + " " +  a);

		}

	

	

	}





	public static boolean oddEven(int n)

	{


		

		// tests if number is odd or even

		if (n%2 == 0)

			return true;

		else

			return false;

			

		}

}




NEW CODE

/**
 * This program will print out numbers between two parameters, then test if they are odd or even
 * 
 */


class Exercise1c
{
	public static void main(String[] args)
	{

		System.out.println(oddEven(1,10));

	}



	public static boolean oddEven(int n, int m)
	{
		int a;
										 // tests if number is odd or even
		for (a = n; a <= m; a++) 		 // our two parameters in this case, are 1 and 10
		{

			System.out.println("The number is" + " " + a);

										// tests if number is odd or even
			if (n%2 == 0)
				return true;
			else
				return false;


		}

	}

}









This is my new code, the problem with it is that; if i compile it, i get the error

Exercise1c.java:71: missing return statement
		}
		^
1 error



in my compiler (using cmd to compile with javac)

I guess i kindof understand what it means, the compiler is telling me that theres no return statement, cos it runs through the for loop and doesn't store the statement, it just runs back over it.

I tried putting the test for even/odd outside of the loop but it didn't work, im not supposed to use any complicated stuff. its meant to be quite easy. we haven't learnt "break" or "continue" or anything like that.

THANKS FOR READING!! THANKS IF YOU HELP :D

Is This A Good Question/Topic? 0
  • +

Replies To: Learning methods, having difficulty printing out 1 to 10 and checking

#2 NeoTifa  Icon User is offline

  • NeoTifa Codebreaker, the Scourge of Devtester
  • member icon





Reputation: 4099
  • View blog
  • Posts: 18,184
  • Joined: 24-September 08

Re: Learning methods, having difficulty printing out 1 to 10 and checking

Posted 20 August 2009 - 05:52 AM

Don't put the else in there. Try that
Was This Post Helpful? 0
  • +
  • -

#3 jdi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 20-August 09

Re: Learning methods, having difficulty printing out 1 to 10 and checking

Posted 20 August 2009 - 05:55 AM

same error.

Exercise1c.java:67: missing return statement
}
^
1 error


this is a bit frustrating! :D
Was This Post Helpful? 0
  • +
  • -

#4 NeoTifa  Icon User is offline

  • NeoTifa Codebreaker, the Scourge of Devtester
  • member icon





Reputation: 4099
  • View blog
  • Posts: 18,184
  • Joined: 24-September 08

Re: Learning methods, having difficulty printing out 1 to 10 and checking

Posted 20 August 2009 - 05:56 AM

Try putting your boolean into a variable.
Was This Post Helpful? 0
  • +
  • -

#5 oQMr FoxQo  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 123
  • Joined: 16-August 09

Re: Learning methods, having difficulty printing out 1 to 10 and checking

Posted 20 August 2009 - 06:06 AM

try this code dude
	  public boolean oddEven(int a)
		{
				if (a%2 == 0)
					return true;
				else
					return false;
	}
		public static void main(String args[]) 
		{
			Exercise1c lc=new Exercise1c();
		   for(int i=0;i<10;i++)
		   {
			   if(lc.oddEven(i))
		   System.out.println("even: "+i);
			   else
				   System.out.println("odd: "+i);
		   
		   }

		}



i ve made the loop inside the main fct i think this way will be easier for u
let me know it will be helpful
Was This Post Helpful? 0
  • +
  • -

#6 jdi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 20-August 09

Re: Learning methods, having difficulty printing out 1 to 10 and checking

Posted 20 August 2009 - 06:09 AM

View PostoQMr FoxQo, on 20 Aug, 2009 - 05:06 AM, said:

try this code dude
	  public boolean oddEven(int a)
		{
				if (a%2 == 0)
					return true;
				else
					return false;
	}
		public static void main(String args[]) 
		{
			Exercise1c lc=new Exercise1c();
		   for(int i=0;i<10;i++)
		   {
			   if(lc.oddEven(i))
		   System.out.println("even: "+i);
			   else
				   System.out.println("odd: "+i);
		   
		   }

		}



i ve made the loop inside the main fct i think this way will be easier for u
let me know it will be helpful


hmm

it all has to be done in the method, then the main function has to call the method.

i did the same thing in my first code and it worked.

thank you for your help but it has to be specific to the criteria.
Was This Post Helpful? 0
  • +
  • -

#7 NeoTifa  Icon User is offline

  • NeoTifa Codebreaker, the Scourge of Devtester
  • member icon





Reputation: 4099
  • View blog
  • Posts: 18,184
  • Joined: 24-September 08

Re: Learning methods, having difficulty printing out 1 to 10 and checking

Posted 20 August 2009 - 06:14 AM

According to what I see of your logic, you're trying to only first even number? As for just printing numbers, you could say:

public static void oddEven(int n, int m) {

int num1 = n;
int num2 = m;

for(i = num1; i <= num2; i++){

sout("The number is: " + i);

}

}



Was This Post Helpful? 0
  • +
  • -

#8 oQMr FoxQo  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 123
  • Joined: 16-August 09

Re: Learning methods, having difficulty printing out 1 to 10 and checking

Posted 20 August 2009 - 06:20 AM

Quote

hmm

it all has to be done in the method, then the main function has to call the method.

i did the same thing in my first code and it worked.

thank you for your help but it has to be specific to the criteria.

ok i will this is the same way in another way, and in the end it is called from the main fct
	  public boolean oddEven(int a)
		{
				if (a%2 == 0)
					return true;
				else
					return false;
	}
	  public static void cal(int m)
	  {

			reaader lc=new reaader();
		  lc.oddEven(m);
		  System.out.println();
		   for(int i=0;i<m;i++)
		   {
			   if(lc.oddEven(i))
		   System.out.println("even: "+i);
			   else
				   System.out.println("odd: "+i);
		   
		   }
		  
	  }
		public static void main(String args[]) 
		{

			cal(10);
		}


maybe this one ?

This post has been edited by oQMr FoxQo: 20 August 2009 - 06:22 AM

Was This Post Helpful? 0
  • +
  • -

#9 jdi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 20-August 09

Re: Learning methods, having difficulty printing out 1 to 10 and checking

Posted 20 August 2009 - 06:29 AM

okay

i rewrote the code using the considerations and ideas from mrfox and neotifa (thank you :))

this is the code but it has a problem: it ONLY prints out false, never true. i dont understand why this is happening.

/**
 * This program will print out numbers between two parameters, then test if they are odd or even
 * 
 */


class Exercise1c
{
	public static void main(String[] args)
	{

		oddEven(1,10);

	}



	public static void oddEven(int n, int m)
	{
		int a;
		boolean result;
										 // tests if number is odd or even
		for (a = n; a <= m; a++) 		 // our two parameters in this case, are 1 and 10
		{



										// tests if number is odd or even
			if (n%2 == 0)
				result = true;
			else
				result = false;

			System.out.println("The number is" + " " + a + " " + result);



		}


	}

}



Was This Post Helpful? 0
  • +
  • -

#10 oQMr FoxQo  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 123
  • Joined: 16-August 09

Re: Learning methods, having difficulty printing out 1 to 10 and checking

Posted 20 August 2009 - 06:39 AM

take this code dude you ve done a mistake instad of (n%2 == 0) it should be (a%2 == 0) and i fixed some other stuffs
	  public static void main(String[] args)
		{
			oddEven(1,10);
		}
		public static void oddEven(int n, int m)
		{
			

											 // tests if number is odd or even
			for (int a = n; a <= m; a++)		  // our two parameters in this case, are 1 and 10
			{
									  // tests if number is odd or even
				if (a%2 == 0)
					System.out.println("The number is even" + " " + a);
				else
					System.out.println("The number is odd" + " " + a );
				
			}
		}



hope it is helpful
Was This Post Helpful? 1
  • +
  • -

#11 jdi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 20-August 09

Re: Learning methods, having difficulty printing out 1 to 10 and checking

Posted 20 August 2009 - 06:42 AM

HAHA YES!!

I FIXED IT :D

it was this line

		for (a = n; a <= m; a++) 		 // our two parameters in this case, are 1 and 10
		{

										// tests if number is odd or even
			if (n%2 == 0) <-- HERE HERE HERE
				result = "and it is Even";
			else
				result = "and it is Odd";



			System.out.println("The number is" + " " + a + " " + result);




i said if (n%2 == 0)

when i should've said if (a%2 == 0)

excellent.

:D:D thank you everyone

View PostoQMr FoxQo, on 20 Aug, 2009 - 05:39 AM, said:

take this code dude you ve done a mistake instad of (n%2 == 0) it should be (a%2 == 0) and i fixed some other stuffs
	  public static void main(String[] args)
		{
			oddEven(1,10);
		}
		public static void oddEven(int n, int m)
		{
			

											 // tests if number is odd or even
			for (int a = n; a <= m; a++)		  // our two parameters in this case, are 1 and 10
			{
									  // tests if number is odd or even
				if (a%2 == 0)
					System.out.println("The number is even" + " " + a);
				else
					System.out.println("The number is odd" + " " + a );
				
			}
		}



hope it is helpful



thank you very much :)

this is very helpful. you posted just before me hehe thank you :) the code is much nicer. i appreciate it alot

thank you :)
Was This Post Helpful? 0
  • +
  • -

#12 Yaroslav94  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 294
  • Joined: 15-July 09

Re: Learning methods, having difficulty printing out 1 to 10 and checking

Posted 20 August 2009 - 08:36 AM

Quick Question - what is if (n%2 == 0)??

Thank You, and u shld also get a free java/compliler program
I recommend jGRASP, or Eclipse (I use jGRASP)
http://jgrasp.org

:P
Was This Post Helpful? 0
  • +
  • -

#13 NeoTifa  Icon User is offline

  • NeoTifa Codebreaker, the Scourge of Devtester
  • member icon





Reputation: 4099
  • View blog
  • Posts: 18,184
  • Joined: 24-September 08

Re: Learning methods, having difficulty printing out 1 to 10 and checking

Posted 20 August 2009 - 09:22 AM

ERmmm.... its checking for even numbers. its saying if the remainder of n / 2 == 0 then.... so if their is no remainder then it is even.
Was This Post Helpful? 0
  • +
  • -

#14 Yaroslav94  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 294
  • Joined: 15-July 09

Re: Learning methods, having difficulty printing out 1 to 10 and checking

Posted 20 August 2009 - 10:34 AM

View PostNeoTifa, on 20 Aug, 2009 - 08:22 AM, said:

ERmmm.... its checking for even numbers. its saying if the remainder of n / 2 == 0 then.... so if their is no remainder then it is even.

:genius: :genius: :genius: :genius: :genius: :genius: :genius: :genius: :genius: :genius:
Thanks.. You really deserve to be a Moderator, not an Author!!! You helped me out a lot for the last couple of days!!! :w00t:
Was This Post Helpful? 0
  • +
  • -

#15 NeoTifa  Icon User is offline

  • NeoTifa Codebreaker, the Scourge of Devtester
  • member icon





Reputation: 4099
  • View blog
  • Posts: 18,184
  • Joined: 24-September 08

Re: Learning methods, having difficulty printing out 1 to 10 and checking

Posted 20 August 2009 - 11:26 AM

Lol thanks. Take that up with them then. :P ((but can I still streak and throw confetti in my own thread? :P))

You should click that "This Post Was Helpful" button under my post then to show your appreciation!
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1