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: -
import java.io.*;
-
public class IcsMarkbook
-
{
-
-
public static void main(String[] args, int marks[]) throws IOException
-
{
-
System.out.println("Writing 25 student marks...");
-
randMarks();
-
System.out.println("Done.");
-
System.out.println();
-
-
System.out.println("Loading the marks into array...");
-
//int marks[] = importMarks(); (this is commented out because it wasn't working)
-
System.out.println("Done.");
-
System.out.println();
-
-
System.out.print("Original mark order: ");
-
-
for (int i = 0; i < marks.length; i++)
-
{
-
System.out.print(marks[i]);
-
if (i != marks.length-1)
-
{
-
System.out.print(", ");
-
}
-
else
-
{
-
System.out.print(".");
-
}
-
}
-
-
System.out.println();
-
System.out.println("Arranging numbers in descending order...");
-
// have yet to write this part
-
System.out.println("Done.");
-
}
-
-
public static void randMarks()
-
{
-
int rndNum; // create integer to hold random number
-
-
PrintWriter fileOut = new PrintWriter(new FileWriter("marks.txt")); // open a file for writing
-
-
for (int i = 0; i < 25; i++)
-
{
-
rndNum = (int)(Math.random() * 60) + 41; // generate random number between 40-60
-
fileOut.println(rndNum); // print that number to a new line in currently open file
-
}
-
-
fileOut.close(); // close the file once we're done with it
-
}
-
-
public static int importMarks()
-
{
-
BufferedReader readFile = new BufferedReader(new FileReader("marks.txt"));
-
-
int marks [] = new int [25];
-
-
for (int i = 0; i < marks.length; i++)
-
{
-
marks[i] = Integer.parseInt(readFile.readLine());
-
}
-
-
readFile.close();
-
return marks;
-
}
-
}
-
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 : )
6 1584
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: -
import java.io.*;
-
public class IcsMarkbook
-
{
-
-
public static void main(String[] args, int marks[]) throws IOException
-
{
-
System.out.println("Writing 25 student marks...");
-
randMarks();
-
System.out.println("Done.");
-
System.out.println();
-
-
System.out.println("Loading the marks into array...");
-
//int marks[] = importMarks(); (this is commented out because it wasn't working)
-
System.out.println("Done.");
-
System.out.println();
-
-
System.out.print("Original mark order: ");
-
-
for (int i = 0; i < marks.length; i++)
-
{
-
System.out.print(marks[i]);
-
if (i != marks.length-1)
-
{
-
System.out.print(", ");
-
}
-
else
-
{
-
System.out.print(".");
-
}
-
}
-
-
System.out.println();
-
System.out.println("Arranging numbers in descending order...");
-
// have yet to write this part
-
System.out.println("Done.");
-
}
-
-
public static void randMarks()
-
{
-
int rndNum; // create integer to hold random number
-
-
PrintWriter fileOut = new PrintWriter(new FileWriter("marks.txt")); // open a file for writing
-
-
for (int i = 0; i < 25; i++)
-
{
-
rndNum = (int)(Math.random() * 60) + 41; // generate random number between 40-60
-
fileOut.println(rndNum); // print that number to a new line in currently open file
-
}
-
-
fileOut.close(); // close the file once we're done with it
-
}
-
-
public static int importMarks()
-
{
-
BufferedReader readFile = new BufferedReader(new FileReader("marks.txt"));
-
-
int marks [] = new int [25];
-
-
for (int i = 0; i < marks.length; i++)
-
{
-
marks[i] = Integer.parseInt(readFile.readLine());
-
}
-
-
readFile.close();
-
return marks;
-
}
-
}
-
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: - 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: - int[] marks = new int[25];
Try that out and tell us what errors come up.
Hope this helped,
-blazed
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... ^^
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: -
import java.io.*;
-
public class IcsMarkbook
-
{
-
-
public static void main(String[] args, int marks[]) throws IOException
-
{
-
System.out.println("Writing 25 student marks...");
-
randMarks();
-
System.out.println("Done.");
-
System.out.println();
-
-
System.out.println("Loading the marks into array...");
-
//int marks[] = importMarks(); (this is commented out because it wasn't working)
-
System.out.println("Done.");
-
System.out.println();
-
-
System.out.print("Original mark order: ");
-
-
for (int i = 0; i < marks.length; i++)
-
{
-
System.out.print(marks[i]);
-
if (i != marks.length-1)
-
{
-
System.out.print(", ");
-
}
-
else
-
{
-
System.out.print(".");
-
}
-
}
-
-
System.out.println();
-
System.out.println("Arranging numbers in descending order...");
-
// have yet to write this part
-
System.out.println("Done.");
-
}
-
-
public static void randMarks()
-
{
-
int rndNum; // create integer to hold random number
-
-
PrintWriter fileOut = new PrintWriter(new FileWriter("marks.txt")); // open a file for writing
-
-
for (int i = 0; i < 25; i++)
-
{
-
rndNum = (int)(Math.random() * 60) + 41; // generate random number between 40-60
-
fileOut.println(rndNum); // print that number to a new line in currently open file
-
}
-
-
fileOut.close(); // close the file once we're done with it
-
}
-
-
public static int importMarks()
-
{
-
BufferedReader readFile = new BufferedReader(new FileReader("marks.txt"));
-
-
int marks [] = new int [25];
-
-
for (int i = 0; i < marks.length; i++)
-
{
-
marks[i] = Integer.parseInt(readFile.readLine());
-
}
-
-
readFile.close();
-
return marks;
-
}
-
}
-
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 - public static int importMarks()
Don't you want to return an int[] instead of an int? In which case you should have - public static int[] importMarks()
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 - public static int importMarks()
to - 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 - int marks [] = new int [25];
should be written as - 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 : )
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 - public static int importMarks()
to - 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 - int marks [] = new int [25];
should be written as - 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 - int a[] = new int[2];
-
int []b = new int[2];
-
int []c[] = new int[2][2];
-
int []d[] = new int[2][2];
-
//etc
-
Both are accepted by the compiler
Have a look at - int a[] = new int[2];
-
int []b = new int[2];
-
int []c[] = new int[2][2];
-
int []d[] = new int[2][2];
-
//etc
-
... Um ... cool. I didn't know that. I should have tested it myself. Good to know, thanks man.
-blazed
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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. ...
|
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...
|
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:
...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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...
| |