returning more than one integer
This code has no errors but only prints one number below 10, i wanted the output to be;
"The numbers that are below 100 are 69, 67, 2".
I learnt yesterday the below method cannot return more than one number and then i was told that an array might work.
do i need an array to store all the numbers below 10? do i need to make an empty array? i'm a little confused!
class ReturnNumbersBelowTen
{
publicstaticvoid main(String[] args)
{
int num1 = 69, num2 = 67, num3 = 488, num4 = 2;
System.out.println("The first number is " +num1);
System.out.println("The second number is " +num2);
System.out.println("The third number is " +num3);
System.out.println("The fourth number is " +num4);
System.out.println("The numbers that are below 100 are " +below(num1, num2, num3, num4));
}
publicstaticint below(int x1,int x2,int x3,int x4)//this part of the code is causing the problems
{
if (x1 < 100)return x1;
if (x2 < 100)return x2;
if (x3 < 100)return x3;
if (x4 < 100)return x4;
return 0;
}
}
OUTPUT IS
the numbers that are below 100 are 69//which is not what i want!
[2272 byte] By [
mark_8206a] at [2007-11-26 18:09:40]

i saiddo i need an array to store all the numbers below 10? i meantdo i need an array to store all the numbers below 100?sorry
1. go to the kitchen.2. return.3. return.4. return.do you see the problem now?
yeah, i know now that code cannot do what i want, i just don't know the solution
public static String below(int x1, int x2, int x3, int x4){
String numbers = "";
if (x1 < 100) numbers = numbers + x1+ " ";
if (x2 < 100) numbers = numbers + x2 + " ";
if (x3 < 100) numbers = numbers + x3 + " ";
if (x4 < 100) numbers = numbers + x4;
return numbers;
}
this is not the best way of doing it, just a quick eg
don't return from the method, print from therethat is, if (x1 < 100) System.out.print(x1 + ", ");and so on
this is a better way of doing it my way:
public static String below(int x1, int x2, int x3, int x4) {
String numbers = "";
if (x1 < 100) numbers = x1;
if (x2 < 100) {
if numbers.length() > 0 {
numbers = numbers +", " + x2;
}else{
numbers = x2;
}
}
if (x2 < 100) {
if numbers.length() > 0 {
numbers = numbers +", " + x3;
}else{
numbers = x3;
}
}
if (x2 < 100) {
if numbers.length() > 0 {
numbers = numbers +", " + x4;
}else{
numbers = x4;
}
}
return numbers;
}
it seems to be working a little better now, still a couple of little problems though!
sjs, i'll go try that, thanks
mark,
Think about the problem... it's not too hard... you want to return a list of things from a function.... and how do you "hold" lists of things in java? There are actually lots & lots & lot of ways, but often the simplest is the best... and that's an array... which is just another name for a "list of the same stuffs" - don't get picky guys!
Your "below" function might look like this:public static int[] below(int boundary, int[] nums) {
... some code ...
}
it takes two arguments
1. "boundary" is the number below which one shall not go... ie we won't return any numbers which are below this value.
2. "nums" is array of however many numbers.
The "below" function would have to compare each number in the "nums" array with the "boundary" number... and if it's less than the boundary then it would copy that number into a new array.... once it's compared all the numbers it would return the new array to the caller.
jsalonen's suggestion will work, but "I don't like it" on soooo many levels. It's a gluggy design.
keith.
Message was edited by: corlettk
kludgy design for a kludgy assignment; in a real program things should be done differently
At the moment i have this code below. thanks 4 all the help by the way
Since i'm a bit of a newbie, can anyone give me a little explanation of what's happening with it? Is it just a string that gets numbers added or rejected from it? i never even knew you could put integers into a string.
if (x1 < 100) numbers = numbers + x1+ " ";
this line is saying if the number is below 100 add it to the numbers string?
class ReturnNumbersBelowTen
{
public static void main(String[] args)
{
int num1 = 69, num2 = 67, num3 = 488, num4 = 2;
System.out.println("The first number is " +num1);
System.out.println("The second number is " +num2);
System.out.println("The third number is " +num3);
System.out.println("The fourth number is " +num4);
System.out.println("The numbers that are below 100 are " +below(num1, num2, num3, num4));
}
public static String below(int x1, int x2, int x3, int x4)
{
String numbers = "";
if (x1 < 100) numbers = numbers + x1+ " ";
if (x2 < 100) numbers = numbers + x2+ " ";
if (x3 < 100) numbers = numbers + x3+ " ";
if (x4 < 100) numbers = numbers + x4;
return numbers;
}
}
dont use my old method, it doesnt work nicely, use the newer one that i made.
x1 - It looks at the number and if it is less than 100 it adds it to a blank string
x2, x3, x4 - It looks at the number and if it is less than 100 and there are no other numbers less than 100 it adds it to a blank string IF there are other numbers it attatches it to the end of the string
public static void main(String[] args){
int num1 = 69, num2 = 67, num3 = 488, num4 = 2;
System.out.println("The first number is " +num1);
System.out.println("The second number is " +num2);
System.out.println("The third number is " +num3);
System.out.println("The fourth number is " +num4);
System.out.println("The numbers that are below 100 are " + below(num1, num2, num3, num4));
}
it will print this:
The numbers that are below 100 are 69, 67, 2
ok sjs thanks, i think i know what you did
Mark,
Ok, so this is probably just showing off now, since you are happy with your kludge, but...public class UnderHundreds
{
public static final int MAXIMUM = 100;
public static void main(String[] args) {
int[] nums = {69, 67, 488, 2};
System.out.println("The numbers are ("+join(", ", nums)+")");
System.out.println("The numbers below "+MAXIMUM+" are ("+join(", ", below(MAXIMUM,nums))+")");
}
public static int[] below(int boundary, int[] nums) {
int[] results = new int[nums.length];
int cnt = 0;
for (int x : nums) {
if (x < boundary) results[cnt++] = x;
}
return results;
}
//
// STATIC HELPER METHODS
//
public static String join(String FS, int[] nums) {
StringBuffer sb = new StringBuffer();
for (int i=0; i<nums.length; i++) {
if (i>0) sb.append(FS);
sb.append(nums[i]);
}
return sb.toString();
}
}
I really like things to be "a bit flexible".
So... I've got a few exercises for you.
1. compile and run the above code (henceforth called "my code")
2. 100 isn't big enough any more, now we need to allow numbers up to 150!
2a. change "my code" to allow numbers up to 150.
2b. change your code to allow numbers up to 150.
* How many changes did you have to make to each?
* How hard was it to figure out what you needed to change in each?
* How many times did you compile and test each? (be honest)
3. We now need to deal with eight numbers!
3a. change "my code" to use 8 numbers instead of 4.
3b. change your own code to do the same.
* Again, which code was easier to change?
4... I'll give you 10 dukes if you get this one...
4a. change "my code" to take the list of however many numbers from the command line. The first argument will be the MAXIMUM, the following arguments (if any) should be printed only if they are less than the first number.
* Hint: the command line arguments are those "String[] args" thingummies which are passed into main method by "the system" by the command "java myProgram arg0 arg1 arg2 ..."
* Hint: [url=http://java.sun.com/javase/6/docs/api/java/lang/Integer.html#Integer(java.lang.String)]Integer(String s)[/url] Constructs a new Integer from the given String... and lookout for those NumberFormatException's
So what's the point? Why am I going on like this? The point, in a nutshell is "flexibility", which really just means that a well designed and written computer program is easier to change... so it can be adapted to changes in "the requirements".
It's usually a bit harder to "be flexible", and it always requires a bit more fore-thought but (ask any professional programmer) it's an investment which will save you untold headaches down the road.
keith.
Message was edited by: corlettk -fuddle it!
corlettk, yeah you're a bit of a show off! but seriously thanks 4 all the help. i would say that even if the code i'm happy with is not all that good or flexible as u put it, it works and i understand it reasonably well. With your code, (which i'm sure is much better) for me it's like trying to understand chinese, it's a little too advanced for me at the moment. maybe one day i'll figure it out!