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

Page 1 of 1

14 Replies - 2352 Views - Last Post: 20 August 2009 - 11:26 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=121303&amp;s=5c8a2c7f73621b4134e58b44aadbda9c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 jdi

Reputation: 0
• 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

Is This A Good Question/Topic? 0

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

#2 NeoTifa

• NeoTifa Codebreaker, the Scourge of Devtester

Reputation: 4165
• Posts: 18,336
• 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

#3 jdi

Reputation: 0
• 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!

#4 NeoTifa

• NeoTifa Codebreaker, the Scourge of Devtester

Reputation: 4165
• Posts: 18,336
• 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.

#5 oQMr FoxQo

Reputation: 16
• 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

#6 jdi

Reputation: 0
• 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

oQMr 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.

#7 NeoTifa

• NeoTifa Codebreaker, the Scourge of Devtester

Reputation: 4165
• Posts: 18,336
• 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);

}

}

```

#8 oQMr FoxQo

Reputation: 16
• 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)
{

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

#9 jdi

Reputation: 0
• 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);

}

}

}

```

#10 oQMr FoxQo

Reputation: 16
• 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 );

}
}

```

#11 jdi

Reputation: 0
• 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

```		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 thank you everyone

oQMr 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 );

}
}

```

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

#12 Yaroslav94

• D.I.C Regular

Reputation: 5
• 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

#13 NeoTifa

• NeoTifa Codebreaker, the Scourge of Devtester

Reputation: 4165
• Posts: 18,336
• 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.

#14 Yaroslav94

• D.I.C Regular

Reputation: 5
• 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

NeoTifa, 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.

Thanks.. You really deserve to be a Moderator, not an Author!!! You helped me out a lot for the last couple of days!!!

#15 NeoTifa

• NeoTifa Codebreaker, the Scourge of Devtester

Reputation: 4165
• Posts: 18,336
• 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. ((but can I still streak and throw confetti in my own thread? ))

You should click that "This Post Was Helpful" button under my post then to show your appreciation!