473,543 Members | 3,135 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Printing out a Random Array

60 New Member
import java.util;

public class Compact
{
public static int[] randomArray (int n)
{
int[] a = new int[n];
for (int i = 0; i < a.length; i++)
{
a[i] = Math.random();
}
return a;
}

public static double[] printArray (double[] a)
{
for (int i = 0; i<a.length; i++)
{
return a[i];
}
}

public static void main(String args[])
{
System.out.prin tln("Test for printing " + printArray(a));
}
}


In the above code, what I am trying to do is create an array with random integers. The size of the array is 20. I don't know how to call the array...
Dec 6 '07 #1
47 3965
JosAH
11,448 Recognized Expert MVP
Have you tried to compile it and read what the compiler had to say about it? btw,
you can't call an array, you might yell 'yoohoo' at it but I'm sure it won't react nor
respond. Arrays are kind of stupid.

kind regards,

Jos
Dec 6 '07 #2
Energizer100
60 New Member
Have you tried to compile it and read what the compiler had to say about it? btw,
you can't call an array, you might yell 'yoohoo' at it but I'm sure it won't react nor
respond. Arrays are kind of stupid.

kind regards,

Jos

import java.util;

public class Compact
{
public static void main(String[] args)
{
public static int[] randomArray (int n)
{
int[] a = new int[n];
for (int i = 0; i < a.length; i++)
{
a[i] = Math.random();
}
System.out.prin tln (a);
}
}
}


this doesn't work for some reason, it says illegal start of expression at the randomArray method. and a semicolon is expected at the second to last bracket
Dec 6 '07 #3
BigDaddyLH
1,216 Recognized Expert Top Contributor
When your syntax is way off the mark, the resulting compiler messages may not be helpful, because the compiler is so confused. That is the case, here.

You nested the defintion of method randomArray inside method main. In other words, you wrote:

Expand|Select|Wrap|Line Numbers
  1. void f() {
  2.     void g() {
  3.  
  4.     }
  5. }
  6.  
instead of

Expand|Select|Wrap|Line Numbers
  1. void f() {
  2.  
  3. }
  4.  
  5. void g() {
  6.  
  7. }
  8.  
Dec 6 '07 #4
Energizer100
60 New Member
When your syntax is way off the mark, the resulting compiler messages may not be helpful, because the compiler is so confused. That is the case, here.

You nested the defintion of method randomArray inside method main. In other words, you wrote:

Expand|Select|Wrap|Line Numbers
  1. void f() {
  2.     void g() {
  3.  
  4.     }
  5. }
  6.  
instead of

Expand|Select|Wrap|Line Numbers
  1. void f() {
  2.  
  3. }
  4.  
  5. void g() {
  6.  
  7. }
  8.  
okay, one problem was fixed. But it still says "illegal start of expression" for the random array method.
Dec 7 '07 #5
BigDaddyLH
1,216 Recognized Expert Top Contributor
okay, one problem was fixed. But it still says "illegal start of expression" for the random array method.
I can only guess what your code looks like now. I suggest that when you are asking a question about a syntax error, you should post the relevant code.
Dec 7 '07 #6
Energizer100
60 New Member
I can only guess what your code looks like now. I suggest that when you are asking a question about a syntax error, you should post the relevant code.
srry

import java.util;

public class Compact
{
public static void main(String[] args)
{
public static int[] randomArray(int n)
{
int[] a = new int[n];
for (int i = 0; i < a.length; i++)
{
a[i] = Math.random();
}
}
System.out.prin tln (a);
}
}
Dec 7 '07 #7
JosAH
11,448 Recognized Expert MVP
Your first line is wrong: you can't import a package, you import classes instead;
as in:

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2.  
Compilers can be real nitpickers (which is AGT (A Good Thing (tm)))

kind regards,

Jos
Dec 7 '07 #8
BigDaddyLH
1,216 Recognized Expert Top Contributor
You are still nesting method randomArray inside method main. See reply #4.

ps. If you use code tags, your code will be more readable in this forum.
Dec 7 '07 #9
Energizer100
60 New Member
Expand|Select|Wrap|Line Numbers
  1. import java.util.Arrays;
  2.  
  3. public class Compact 
  4. {
  5.     public static void main(String[] args)
  6.     {
  7.         public static int[] randomArray(int n)
  8.         {
  9.             int[] a = new int[n];
  10.             for (int i = 0; i < a.length; i++)
  11.             {
  12.             a[i] = Math.random();
  13.             }
  14.         }
  15.             System.out.println (a);
  16.     }
  17. }
  18.  
Still doesn,t work, same error
Dec 7 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

10
2487
by: Virus | last post by:
Ok well what I am trying to do is have 1.) the background color to change randomly with 5 different colors.(change on page load) 2,) 10 different quotes randomly fadeing in and out in random spots on the webpage. with a delay timer on them, so they keep changing as the page is open. Not random each time the page is loaded. If anyone...
16
4189
by: Jason | last post by:
Hi, I need a way to use random numbers in c++. In my c++ project, when using the mingw compiler I used a mersenne twister that is publicly available and this did its job well. Now I have shelled out on VC++ 6.0 compiling that same code is proving difficult. I am not too worried how I generate random numbers in c++, as long as it is...
10
5953
by: Johnny Snead | last post by:
Hey guys, Need help with this random sort algorithm private void cmdQuestion_Click(object sender, System.EventArgs e) { Random rnd = new Random(); //initialize rnd to new random object System.Random iRnd = new System.Random(); string theNum = iRnd.Next(0,8).ToString(); lblAnswer.Text = iRnd.Next(0,8).ToString();
4
9193
by: Arif | last post by:
I C# code prints very slow as compared to a third party barcode printing software. That software prints approximately 10 labels in 2 seconds while my C# code prints 10 labels in 5 to 6 seconds. And this differences increases with the increase number of labels. The code is as follwods: Here rdr = OleDbDataReader Font is Times New Roman, 12pt
5
2070
by: jar13861 | last post by:
I'm confused on how to write a random array that will only generate 9 different numbers from 1-9. Here is what I have, but its only writing one number.... holder = new Array ( 9 ); var flag = true; var rannum = Math.floor( 1 + Math.random() * 9 ); for (var j = 0; j < 9; j++) {
6
4075
by: Siv | last post by:
Hi, I am getting into printing with VB.NET 2005 and want to implement the usual capability that a user can select a selection of pages. I have a report that is generated by my application that if the user wants all pages will produce 3 pages. I want to offer the user the ability to select via the print dialog that only pages 1 and 2 of it...
12
3197
by: Pascal | last post by:
hello and soory for my english here is the query :"how to split a string in a random way" I try my first shot in vb 2005 express and would like to split a number in several pieces in a random way without success. for example if the number is 123 456 : i would like to have some random strings like theese : (12 * 10 000) + (345 * 10) + (6*1)...
6
2117
by: Pao | last post by:
My code works in this way: I declared a static array in a class (public static int GVetRandom = new int;) that in a for cycle I fill with random numbers. The array gets cleared (clear method) and refilled at each turn of cycle. On my developing pc, the same sequence of random numbers was repeated from on turn of cycle to the other; I put...
11
3000
TTCEric
by: TTCEric | last post by:
This will be original. I promise. I cannot get the random number generator to work. I tried seeding with Date.Now.Milliseconds, it still results in the same values. What I have are arrays of values. I get a random index value for each array so I can pull the data from them.
0
7402
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7347
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7344
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5264
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4890
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3388
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3391
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
963
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
633
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.