473,320 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Need help returning an array of int's

2
I have searched through Google for about an hour with nothing to show for it. I'm pretty sure I'm doing the actual return part right, but I'm not too sure what to do with the return once it's back in main(). I basically just need to know how to put the data from the importMarks() function to main(), the goal here is to have all the data in main() in a normal int array where I can use it like I normally would. The code is simple enough:


Expand|Select|Wrap|Line Numbers
  1.   import java.io.*;
  2. public class IcsMarkbook 
  3. {
  4.  
  5.     public static void main(String[] args, int marks[]) throws IOException
  6.     {
  7.         System.out.println("Writing 25 student marks...");
  8.         randMarks();
  9.         System.out.println("Done.");
  10.         System.out.println();
  11.  
  12.         System.out.println("Loading the marks into array...");
  13.         //int marks[] = importMarks();  (this is commented out because it wasn't working)
  14.         System.out.println("Done.");
  15.         System.out.println();
  16.  
  17.         System.out.print("Original mark order: ");
  18.  
  19.         for (int i = 0; i < marks.length; i++)
  20.         {
  21.             System.out.print(marks[i]);
  22.             if (i != marks.length-1)
  23.             {
  24.                 System.out.print(", ");
  25.             }
  26.             else
  27.             {
  28.                 System.out.print(".");
  29.             }
  30.         }
  31.  
  32.         System.out.println();
  33.         System.out.println("Arranging numbers in descending order...");
  34.         // have yet to write this part
  35.         System.out.println("Done.");
  36.     }
  37.  
  38.     public static void randMarks()
  39.     {
  40.         int rndNum; // create integer to hold random number
  41.  
  42.         PrintWriter fileOut = new PrintWriter(new FileWriter("marks.txt")); // open a file for writing
  43.  
  44.         for (int i = 0; i < 25; i++)
  45.         {
  46.             rndNum = (int)(Math.random() * 60) + 41; // generate random number between 40-60
  47.             fileOut.println(rndNum); // print that number to a new line in currently open file
  48.         }
  49.  
  50.         fileOut.close(); // close the file once we're done with it
  51.     }
  52.  
  53.     public static int importMarks()
  54.     {
  55.         BufferedReader readFile = new BufferedReader(new FileReader("marks.txt"));
  56.  
  57.         int marks [] = new int [25];
  58.  
  59.         for (int i = 0; i < marks.length; i++)
  60.         {
  61.             marks[i] = Integer.parseInt(readFile.readLine());
  62.         }
  63.  
  64.         readFile.close();
  65.         return marks;
  66.     }
  67. }
  68.  

I know that I could be doing this in a more simple way, but I am trying to do it in the way I am because of the requirements of the assignment, initially it was supposed to be 2 seperate programs, one to write a file, and another to read from it and then manipulate the data. Thanks for looking : )
Jun 6 '07 #1
6 1584
blazedaces
284 100+
I have searched through Google for about an hour with nothing to show for it. I'm pretty sure I'm doing the actual return part right, but I'm not too sure what to do with the return once it's back in main(). I basically just need to know how to put the data from the importMarks() function to main(), the goal here is to have all the data in main() in a normal int array where I can use it like I normally would. The code is simple enough:


Expand|Select|Wrap|Line Numbers
  1.   import java.io.*;
  2. public class IcsMarkbook 
  3. {
  4.  
  5.     public static void main(String[] args, int marks[]) throws IOException
  6.     {
  7.         System.out.println("Writing 25 student marks...");
  8.         randMarks();
  9.         System.out.println("Done.");
  10.         System.out.println();
  11.  
  12.         System.out.println("Loading the marks into array...");
  13.         //int marks[] = importMarks();  (this is commented out because it wasn't working)
  14.         System.out.println("Done.");
  15.         System.out.println();
  16.  
  17.         System.out.print("Original mark order: ");
  18.  
  19.         for (int i = 0; i < marks.length; i++)
  20.         {
  21.             System.out.print(marks[i]);
  22.             if (i != marks.length-1)
  23.             {
  24.                 System.out.print(", ");
  25.             }
  26.             else
  27.             {
  28.                 System.out.print(".");
  29.             }
  30.         }
  31.  
  32.         System.out.println();
  33.         System.out.println("Arranging numbers in descending order...");
  34.         // have yet to write this part
  35.         System.out.println("Done.");
  36.     }
  37.  
  38.     public static void randMarks()
  39.     {
  40.         int rndNum; // create integer to hold random number
  41.  
  42.         PrintWriter fileOut = new PrintWriter(new FileWriter("marks.txt")); // open a file for writing
  43.  
  44.         for (int i = 0; i < 25; i++)
  45.         {
  46.             rndNum = (int)(Math.random() * 60) + 41; // generate random number between 40-60
  47.             fileOut.println(rndNum); // print that number to a new line in currently open file
  48.         }
  49.  
  50.         fileOut.close(); // close the file once we're done with it
  51.     }
  52.  
  53.     public static int importMarks()
  54.     {
  55.         BufferedReader readFile = new BufferedReader(new FileReader("marks.txt"));
  56.  
  57.         int marks [] = new int [25];
  58.  
  59.         for (int i = 0; i < marks.length; i++)
  60.         {
  61.             marks[i] = Integer.parseInt(readFile.readLine());
  62.         }
  63.  
  64.         readFile.close();
  65.         return marks;
  66.     }
  67. }
  68.  

I know that I could be doing this in a more simple way, but I am trying to do it in the way I am because of the requirements of the assignment, initially it was supposed to be 2 seperate programs, one to write a file, and another to read from it and then manipulate the data. Thanks for looking : )
First of all, you should print the error that shows up, it's always helpful. Secondly, you didn't get an error when trying to do this:

Expand|Select|Wrap|Line Numbers
  1. int marks [] = new int [25];
???

Do you notice the String[] args in main? (Oh, Btw, you shouldn't input anything into main except String[] args, though I think it's possible you are certainly not using it that way). Well the brackets ( [ ] ) need to be in front of int and before marks. That's the first problem.

It should look like this:
Expand|Select|Wrap|Line Numbers
  1. int[] marks = new int[25];
Try that out and tell us what errors come up.

Hope this helped,

-blazed
Jun 6 '07 #2
jeffbroodwar
118 100+
yup blazedaces is right, you should always check the errors your getting. without doing it, your like programming in the dark. use try catch and use exception.getMessage(); / exceaption.printStackTrace(); or exception.toString();
that should help out... ^^
Jun 6 '07 #3
r035198x
13,262 8TB
I have searched through Google for about an hour with nothing to show for it. I'm pretty sure I'm doing the actual return part right, but I'm not too sure what to do with the return once it's back in main(). I basically just need to know how to put the data from the importMarks() function to main(), the goal here is to have all the data in main() in a normal int array where I can use it like I normally would. The code is simple enough:


Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. public class IcsMarkbook 
  3. {
  4.  
  5. public static void main(String[] args, int marks[]) throws IOException
  6. {
  7.     System.out.println("Writing 25 student marks...");
  8.     randMarks();
  9.     System.out.println("Done.");
  10.     System.out.println();
  11.  
  12.     System.out.println("Loading the marks into array...");
  13.     //int marks[] = importMarks(); (this is commented out because it wasn't working)
  14.     System.out.println("Done.");
  15.     System.out.println();
  16.  
  17.     System.out.print("Original mark order: ");
  18.  
  19.     for (int i = 0; i < marks.length; i++)
  20.     {
  21.         System.out.print(marks[i]);
  22.         if (i != marks.length-1)
  23.         {
  24.             System.out.print(", ");
  25.         }
  26.         else
  27.         {
  28.             System.out.print(".");
  29.         }
  30.     }
  31.  
  32.     System.out.println();
  33.     System.out.println("Arranging numbers in descending order...");
  34.     // have yet to write this part
  35.     System.out.println("Done.");
  36. }
  37.  
  38. public static void randMarks()
  39. {
  40.     int rndNum; // create integer to hold random number
  41.  
  42.     PrintWriter fileOut = new PrintWriter(new FileWriter("marks.txt")); // open a file for writing
  43.  
  44.     for (int i = 0; i < 25; i++)
  45.     {
  46.         rndNum = (int)(Math.random() * 60) + 41; // generate random number between 40-60
  47.         fileOut.println(rndNum); // print that number to a new line in currently open file
  48.     }
  49.  
  50.     fileOut.close(); // close the file once we're done with it
  51. }
  52.  
  53. public static int importMarks()
  54. {
  55.     BufferedReader readFile = new BufferedReader(new FileReader("marks.txt"));
  56.  
  57.     int marks [] = new int [25];
  58.  
  59.     for (int i = 0; i < marks.length; i++)
  60.     {
  61.         marks[i] = Integer.parseInt(readFile.readLine());
  62.     }
  63.  
  64.     readFile.close();
  65.     return marks;
  66. }
  67. }
  68.  

I know that I could be doing this in a more simple way, but I am trying to do it in the way I am because of the requirements of the assignment, initially it was supposed to be 2 seperate programs, one to write a file, and another to read from it and then manipulate the data. Thanks for looking : )
You have
Expand|Select|Wrap|Line Numbers
  1.  public static int importMarks()
Don't you want to return an int[] instead of an int? In which case you should have
Expand|Select|Wrap|Line Numbers
  1. public static int[] importMarks()
Jun 6 '07 #4
xansar
2
Thanks for the quick replies! I totally forgot to post the error I was getting, but nonetheless I got an answer that worked. Just to clarify the extra stuff in main() was yet more code I forgot to remove after desperately trying anything in hopes it would work, the error I was getting before was "'.class' expected" on line 71 (the line with the return statement).

What actually helped my problem was changing

Expand|Select|Wrap|Line Numbers
  1. public static int importMarks()
to

Expand|Select|Wrap|Line Numbers
  1. public static int[] importMarks()
Somehow I didn't put two and two together and realize that I needed to state the array, and not just the array type before the function name. Thanks to everyone that posted, I didn't expect an answer so fast!

There's another thing though, blazed mentioned that
Expand|Select|Wrap|Line Numbers
  1. int marks [] = new int [25];
should be written as
Expand|Select|Wrap|Line Numbers
  1. int[] marks = new int [25];
The teacher I have has told us to write it the first way, and it has always worked for me. I didn't know there's a difference, is it better to do it the second?
Thanks again : )
Jun 6 '07 #5
r035198x
13,262 8TB
Thanks for the quick replies! I totally forgot to post the error I was getting, but nonetheless I got an answer that worked. Just to clarify the extra stuff in main() was yet more code I forgot to remove after desperately trying anything in hopes it would work, the error I was getting before was "'.class' expected" on line 71 (the line with the return statement).

What actually helped my problem was changing

Expand|Select|Wrap|Line Numbers
  1. public static int importMarks()
to

Expand|Select|Wrap|Line Numbers
  1. public static int[] importMarks()
Somehow I didn't put two and two together and realize that I needed to state the array, and not just the array type before the function name. Thanks to everyone that posted, I didn't expect an answer so fast!

There's another thing though, blazed mentioned that
Expand|Select|Wrap|Line Numbers
  1. int marks [] = new int [25];
should be written as
Expand|Select|Wrap|Line Numbers
  1. int[] marks = new int [25];
The teacher I have has told us to write it the first way, and it has always worked for me. I didn't know there's a difference, is it better to do it the second?
Thanks again : )
Both are accepted by the compiler
Have a look at

Expand|Select|Wrap|Line Numbers
  1.  int a[] = new int[2];
  2.  int []b = new int[2]; 
  3. int []c[] = new int[2][2]; 
  4.  int []d[] = new int[2][2];
  5. //etc
  6.  
Jun 6 '07 #6
blazedaces
284 100+
Both are accepted by the compiler
Have a look at

Expand|Select|Wrap|Line Numbers
  1.  int a[] = new int[2];
  2.  int []b = new int[2]; 
  3. int []c[] = new int[2][2]; 
  4.  int []d[] = new int[2][2];
  5. //etc
  6.  
... Um ... cool. I didn't know that. I should have tested it myself. Good to know, thanks man.

-blazed
Jun 6 '07 #7

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

Similar topics

5
by: Gent | last post by:
I have two questions which are very similar: Is it possible to return an object in C++. Below is part of my code for reference however I am more concerned about the concept. It seems like the...
10
by: Fraser Ross | last post by:
I need to know the syntax for writing a reference of an array. I haven't seen it done often. I have a class with a member array and I want a member function to return an reference to it. ...
8
by: Mike Nolan | last post by:
As far as I can tell, Postgres has no equivalent to greatest and least functions in Oracle. Yes, you can do the same thing with a case statement, but at the expense of writing MUCH longer SQL...
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
5
by: ctj951 | last post by:
I have a very specific question about a language issue that I was hoping to get an answer to. If you allocate a structure that contains an array as a local variable inside a function and return...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.