Can someone help me to make a program in java:
1. Generate 2 rows of 50 random numbers (int) from 0 to 99.
2. Search the numbers that appear in the first row, but not in the second, and numbers that appear in the second row but not in the first. Print the result.
Help someone, please?

Recommended Answers

All 18 Replies

>Can someone help me to make a program in java
Yes we can help you, please post the code you have so far (read this) and tell us where exactly you're having problems with.

Um...well this is for the random numbers from 1 to 99. is it ok?

import java.util.Random;

/** Generate random integers in a certain range. */
public final class RandomRange {
  
  public static final void main(String... aArgs){
    log("Generating random integers in the range 0..99.");
    
    int START = 0;
    int END = 99;
    Random random = new Random();
    for (int idx = 0; idx <= 99; ++idx){
      showRandomInteger(START, END, random);
    }
    
    log("Done.");
  }
  
  private static void showRandomInteger(int aStart, int aEnd, Random aRandom){
    if ( aStart > aEnd ) {
      throw new IllegalArgumentException("Start cannot exceed End.");
    }
    //get the range, casting to long to avoid overflow problems
    long range = (long)aEnd - (long)aStart + 1;
    // compute a fraction of the range, 0 <= frac < range
    long fraction = (long)(range * aRandom.nextDouble());
    int randomNumber =  (int)(fraction + aStart);    
    log("Generated : " + randomNumber);
  }
  
  private static void log(String aMessage){
    System.out.println(aMessage);
  }
}

and for the second part i have no idea. Any suggestions?

I think you should set up a 2-D array of random integers (2 rows, 50 columns) and store them as you generate them. Right now you are displaying them, but then they are gone, so you won't be able to display them later, nor will you be able to search for them.

I think you may be complicating the generation of the random integers.

random.nextInt (100)

will generate an integer from 0 to 99.

http://java.sun.com/javase/6/docs/api/java/util/Random.html#nextInt(int)

So you can fill the ith row, jth column like this:

randomnumbers[i][j] = random.nextInt (100);

Doing it this way will make the second part easier. And you'll need a function to display the 2-D array. Do it with a nested loop.

for (int i = 0; i < 2; i++)
{
  for (int j = 0; j < 50; j++)
  {
    // display row i, column j
  }
  System.out.println ("");
}

Ok I've made it something like this:

import java.util.Random;

public class RandomNum {
	int i, j;
	int AllNumbers[][] = new int [2][50];
	AllNumbers[50] = new int[50];
	AllNumbers[50] = new int[50];
	for (i=0; i<=50; i++){
		for (j=0; j<=50; j++){
			AllNumbers[i][j] = RandomNum.nextInt (100);
			System.out.println(AllNumbers[i][j] + " ");
			}
		}
	}
}

but it says that nextInt is undefined. What can I do? Also can someone help me with the search thing, because I've never worked with 2-D arrays before.
Thanks VernonDozier!

You have to create a new Object of type Random before you can use it to generate the random numbers. If you go to the link Vernon gave you, you will notice that the class is called Random. So to use a method of this class, you need to create a new Object of this class using the 'new' operator, then you need to use whateverYouCalledThatObject.nextInt(100).... In Vernon's example, he called his Object 'random', hence his code says random.nextInt(100)...

Something like this?

import java.util.Random;

public class RandomNum {
	int i, j;
	int AllNumbers[][] = new int [2][50];
	AllNumbers[50] = new int[50];
	AllNumbers[50] = new int[50];
	Random1 = new Random1;
	for (i=0; i<=50; i++){
		for (j=0; j<=50; j++){
			AllNumbers[i][j] = Random1.nextInt (100);
			System.out.println(AllNumbers[i][j] + " ");
			}
		}
	}
}

Sorry for the stupid questions, but I'm beginner in Java...

Sorry for the stupid questions, but I'm beginner in Java...

There's nothing wrong with being a beginner, and by the way: questions aren't stupid, as long as you learn something of it :)

Ok I've made something else too:

import java.util.Random;

public class RandomNum {
	int i, j;
	int AllNumbers[][] = new int [2][50];
	AllNumbers[50] = new int[50];
	ResultNum[50] = new int[50];
	Random1 = new Random1;
	for (i=0; i<=50; i++){
		for (j=0; j<=50; j++){
			AllNumbers[i][j] = Random1.nextInt (100);
			System.out.println(AllNumbers[i][j] + " ");
			}
	for(int i=0; i<=50; i++)
		for(int j=i+1; j<=50; j++)
	          if(AllNumbers[i]==AllNumbers[j]) 
	        	  ResultNum[i]=AllNumbers[i];
	for(i=0; i<7; i++)
	if(AllNumbers[i]!=ResultNum[i])      
	System.out.println(""+AllNumbers[i]);
	}
}
}

Is it ok and help with the errors?
Thanks thux4life, I'm really trying to learn java.

>Is it ok and help with the errors?
Does it compile? Then it's syntactically correct.
Does it run and give the expected output? Then you're pretty sure that you haven't made a mistake :)

:) nope thux4life,it doesn't compile, that's why I'm asking for help

My program has comipled, but again not the result I wanted, what should I write into nextInt?:

import java.util.Random;

public class RandomNum {
public static void main(String args[])
{
    int i, j;
    int AllNumbers[][] = new int [2][50];
    for (i=0; i<2; i++){
        for (j=0; j<50; j++){
            AllNumbers[i][j] = RandomNum.nextInt (100);

            }
        }
    for (i=0; i<2; i++)
        for (j=0; j<50; j++)
            System.out.println(AllNumbers[i][j] + " ");
    }

private static int nextInt(int k) {
	
	return 0;
}

}

:) nope thux4life,it doesn't compile, that's why I'm asking for help

If it doesn't compile, you should also paste the whole error message your compiler is giving in this thread, by the way, it isn't thux4life, it's tux4life (correct and it will save you 1 keystroke as well :P)

oh...sorry tux4life, it has compiled, but I don't like the result which is only 1 row with 50 zeros.
oh and one more question, can this:

for(int i=0; i<=50; i++)
   
     for(int j=i+1; j<=50; j++)
          if(AllNumbers[i]==AllNumbers[j])
               ResultNum[i]=AllNumbers[i];

          for(i=0; i<=50; i++)
               if(AllNumbers[i]!=ResultNum[i])    
                   System.out.println(""+AllNumbers[i]);

be modified so that I can make the second part of my program?

Get the FIRST part of the program to work perfectly, THEN tackle the second part. You should NOT have a nextInt function in your class. It's already been written for you and comes with the Java installation. Use their Random class and use their nextInt function. Instantiate an object of the Random class, not your class (RandomNum). You had this earlier in your posts, but you took it out. See the code you posted in post 3.

Random random = new Random ();

That code is correct. Keep it. Get rid of the nextInt function you wrote. Use the program below as a model and change it to your needs. You are on the right track in many ways. This is your code with some changes. You had just about everything correct in at least one post, but you kept changing things as you were experimenting.

import java.util.Random;

public class RandomNum
{
    public static void main(String args[])
    {
        Random random = new Random ();
	int i, j;
	int AllNumbers[][] = new int [2][50];
//	AllNumbers[50] = new int[50];
//	AllNumbers[50] = new int[50];
	for (i=0; i < 2; i++){
		for (j=0; j< 50; j++){
			AllNumbers[i][j] = random.nextInt (100);
			System.out.println(AllNumbers[i][j] + " ");
		}
	}
    }
}

See lines 7 and 14 above. Notice both the names ("Random") and the lower versus uppercase ("Random" versus "random"). Notice that it is "Random", not "RandomNum". You can change the word "random" to something else, but you CANNOT change the word "Random" to something else. "Random" is the class. "random" is an instance of the "Random" class. nextInt is a function from the "Random" class. You do not write the nextInt function or the Random class. It comes with Java. You import it on line 1.

Lines 10 and 11 have been commented out. You don't want or need them. I changed lines 12 and 13. You had them correct previously, then changed them.

Ok I think I've got it:
This is:

import java.util.Random;
import java.util.ArrayList;

public class RandomNum {
    private int i, j;
    private Random random;
    private int allNumbers[][];

    public RandomNum() {
        random = new Random();
        allNumbers = new int[2][50];
    }

    public void putNumbersIn(){
        for (i=0; i<=1; i++){
            for (j=0; j<=49; j++){
                allNumbers[i][j] = random.nextInt (100);
                //System.out.println(allNumbers[i][j] + " ");
            }
        }
    }

    public void comesInOneNotTwo(){
        ArrayList<Integer> result = new ArrayList<Integer>();
        boolean iFoundOne;

        for (i=0; i<=49; i++){
            iFoundOne = false;
            for (j=0; j<=49; j++){
                if(allNumbers[1][j] == allNumbers[0][i]){
                    iFoundOne = true;
                };
            }

            if(iFoundOne ==false){
                result.add(allNumbers[0][i]);
            }
        }

        System.out.println("are in row 1, but not in 2: " +result);
        System.out.println("#found: " + result.size());

    }

    public void comesInTwoNotOne(){
        ArrayList<Integer> result = new ArrayList<Integer>();
        boolean iFoundOne;

        for (i=0; i<=49; i++){
            iFoundOne = false;
            for (j=0; j<=49; j++){
                if(allNumbers[0][j] == allNumbers[1][i]){
                    iFoundOne = true;
                };
            }

            if(iFoundOne == false){
                result.add(allNumbers[1][i]);
            }
        }
        
        System.out.println("are in row 2, but not in 1: " +result);
        System.out.println("#found: " + result.size());

    }

}

and the Main:

public class Main {
    public static void main(String[] args) {
        RandomNum rand = new RandomNum();
        rand.putNumbersIn();
        rand.comesInOneNotTwo();
        rand.comesInTwoNotOne();
    }
}

But, my question now is: How to make the two rows to be separated, because now everything is ok just the numbers are in one column so it doesn't show which one is the first and which one is the second. Any idea?

Ok I think I've got it:

But, my question now is: How to make the two rows to be separated, because now everything is ok just the numbers are in one column so it doesn't show which one is the first and which one is the second. Any idea?

println does just that. It prints a line. You want to use print, not println in the inner loop. See my post # 4. I have a println in the outer loop, but not the inner loop.

The inner loop should have a print, not a println. That will put fifty numbers in the row. They may not line up very nicely (they almost definitely WILL NOT line up unless you do something to MAKE them line up), but there will be 50 numbers per line.

Yes it works!!! Thanks A LOT VernonDozier for your help!!!
I guess I can call it solved:) !

Can someone help me to make a program in java:
1. Generate 2 rows of 50 random numbers (int) from 0 to 99.
2. Search the numbers that appear in the first row, but not in the second, and numbers that appear in the second row but not in the first. Print the result.
Help someone, please?

1. Use integer array to hold values.
2. Use == to compare values.

commented: The problem was already solved 24 hours AGO !!! -1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.