PDA

View Full Version : FUCKING PROGRAMMING



infiniteeverlasting
Jun 10, 2014, 04:38 PM
=.="
damn my damn stupid programming teacher doesn't teach shit, and she's wondering why no one gets the shit she's trying to teach? goddamn.
can someone help me with this shit... ANYONE?
how do arrays work?
how do you search through an array with the goddamn loops?
wtf is printwriter?
how the hell do i syntax this shit?
How do i write an array into a txt file?
how do i make an array spit back these values?
O jesus i dont know any of this shit.

this is soooo off topic but man am i just pissed.
AND I GOT FINALS TOMORROW SOS PEOPLE READY TO FAIL.

Randomness
Jun 11, 2014, 12:00 AM
Arrays are a sequential set of identically-sized chunks of memory. They may also have a section that saves the size of the array (depending on language. Java has Array.length, C has nothing so you have to count). The actual implementation of arrays in memory is (or should be) beyond the scope of an introductory programming course (and matters mainly in lower level languages like C, when you're doing direct manipulation of things in memory... which is well beyond introductory programming!). The size of an array is set on creation and is fixed. It cannot be changed, so know how much you need when you create it... or have logic to move things to a bigger one if needed (in Java, check the Arrays class for array utility methods like that... or just use an ArrayList, though I don't know if you've gone over data structures like the Collections classes).

The simplest way to process an array is to just iterate every element. If your language supports for loops, use them (they're basically the loop of choice for iteration). Pseudocode for array processing:
int i=0;
while(i<array length) {
do stuff
i++;
}

If you actually mean searching... well that's a far more complicated question. Presumably the array elements are ordered. If not you need to first sort (I'm not going into sort optimization. Find an implementation of quicksort or mergesort, or just write bubblesort since you aren't working with anything large). Once sorted, your common searches can be run via binary searching - start in the middle, if the element there is larger than your target you check the lower half, else the upper. Repeat until you find the exact item.

PrintWriter... if this is the Java class you're in luck since that's the language I work with most. It supports output to both files and output streams (the main output streams are System.out and System.error)... but usually you use System.out.print(); to write to standard out. For file output I normally use FileWriter (wrapping in BufferedWriter for the utility methods it provides).

In short, a Java Writer class is used to send characters to an output stream. They support sending multiple data types (int, byte, and chars and arrays of them, as well as String), and provide methods to flush the stream (which causes it to actually write the characters) and close it safely (which involves among other things, flushing it). Essentially, they're the usual class for text outputs (check the API for method signatures and descriptions: http://docs.oracle.com/javase/7/docs/api/java/io/Writer.html - note this is for Java 7, while I think it's the same as 6 here it might differ).

If you want to write text to a file, the normal syntax would be:
PrintWriter writer = new PrintWriter(new File(path));
writer.write(something);
...
writer.close();

To write everything in an array to a file in Java:
PrintWriter writer = new PrintWriter(new File(path));
for(int i=0;i<arrayname.length;i++) {
writer.write(arrayname[i]);
//writer.write("\n"); if you want a new line - I use BufferedWriter for its newLine() method
}
writer.close();

Note that once you call writer.close(), you cannot use that writer to write AT ALL anymore. Trying to do so will throw an exception. Opening a new writer to a file will overwrite the file (in general - FileWriter can be set up to append instead), so you don't want to call close until you're 100% done.

To make the values in an array spit back to screen just use System.out.println(arrayname[i]);

Almost every major language uses the syntax arrayname[x] to reference array elements, and standard programming courses tend to start with Hello World, which should tell you how to write stuff back out to the screen... if you didn't cover Hello World look it up for the language you're using (it's just the standard first exercise when learning a new language).


This took a little bit to regurgitate - it's been a while since I actually cared about how arrays worked exactly (I more often use lists). Feel free to fire off more questions... though I don't know that I'll be able to get back to them in time for your final. That said, I'm sure there's other programmers on the board (though activity isn't what it once was...).

infiniteeverlasting
Jun 12, 2014, 08:00 PM
Arrays are a sequential set of identically-sized chunks of memory. They may also have a section that saves the size of the array (depending on language. Java has Array.length, C has nothing so you have to count). The actual implementation of arrays in memory is (or should be) beyond the scope of an introductory programming course (and matters mainly in lower level languages like C, when you're doing direct manipulation of things in memory... which is well beyond introductory programming!). The size of an array is set on creation and is fixed. It cannot be changed, so know how much you need when you create it... or have logic to move things to a bigger one if needed (in Java, check the Arrays class for array utility methods like that... or just use an ArrayList, though I don't know if you've gone over data structures like the Collections classes).

The simplest way to process an array is to just iterate every element. If your language supports for loops, use them (they're basically the loop of choice for iteration). Pseudocode for array processing:
int i=0;
while(i<array length) {
do stuff
i++;
}

If you actually mean searching... well that's a far more complicated question. Presumably the array elements are ordered. If not you need to first sort (I'm not going into sort optimization. Find an implementation of quicksort or mergesort, or just write bubblesort since you aren't working with anything large). Once sorted, your common searches can be run via binary searching - start in the middle, if the element there is larger than your target you check the lower half, else the upper. Repeat until you find the exact item.

PrintWriter... if this is the Java class you're in luck since that's the language I work with most. It supports output to both files and output streams (the main output streams are System.out and System.error)... but usually you use System.out.print(); to write to standard out. For file output I normally use FileWriter (wrapping in BufferedWriter for the utility methods it provides).

In short, a Java Writer class is used to send characters to an output stream. They support sending multiple data types (int, byte, and chars and arrays of them, as well as String), and provide methods to flush the stream (which causes it to actually write the characters) and close it safely (which involves among other things, flushing it). Essentially, they're the usual class for text outputs (check the API for method signatures and descriptions: http://docs.oracle.com/javase/7/docs/api/java/io/Writer.html - note this is for Java 7, while I think it's the same as 6 here it might differ).

If you want to write text to a file, the normal syntax would be:
PrintWriter writer = new PrintWriter(new File(path));
writer.write(something);
...
writer.close();

To write everything in an array to a file in Java:
PrintWriter writer = new PrintWriter(new File(path));
for(int i=0;i<arrayname.length;i++) {
writer.write(arrayname[i]);
//writer.write("\n"); if you want a new line - I use BufferedWriter for its newLine() method
}
writer.close();

Note that once you call writer.close(), you cannot use that writer to write AT ALL anymore. Trying to do so will throw an exception. Opening a new writer to a file will overwrite the file (in general - FileWriter can be set up to append instead), so you don't want to call close until you're 100% done.

To make the values in an array spit back to screen just use System.out.println(arrayname[i]);

Almost every major language uses the syntax arrayname[x] to reference array elements, and standard programming courses tend to start with Hello World, which should tell you how to write stuff back out to the screen... if you didn't cover Hello World look it up for the language you're using (it's just the standard first exercise when learning a new language).


This took a little bit to regurgitate - it's been a while since I actually cared about how arrays worked exactly (I more often use lists). Feel free to fire off more questions... though I don't know that I'll be able to get back to them in time for your final. That said, I'm sure there's other programmers on the board (though activity isn't what it once was...).

yo i didnt actually think i'd get a legit response! thanks man this helped.
hope i dont fail. we're only learning very simple core basics with 1 dimensional arrays. I believe that on my final, the teacher told us that she will have us read a file that she premade for us with a list of integers listed on its own line, which we will hafto use printwriter to read the file and then take out those integers and put them into an int array and find a specific integer in the array by using some type of loop to find the specific int in a specific element, then output the results and then copy the array into our own txt file with printwriter... i'm not sure how the syntax will work out in this. I dont htink the teacher cares about format (probably the reason why she hasnt taught us anything about the buffered writer).

and this a 2 sided "study guide" she gave out as an example program, this may explain a few things of what we're learning, half my class doesnt know whats going on so asking my classmates to make sense of the program was no very effective:

[SPOILER-BOX]https://fbcdn-sphotos-h-a.akamaihd.net/hphotos-ak-xpf1/v/t34.0-12/10462673_1444788509112025_4438834112775606812_n.jp g?oh=acfb767ec650bcd5c353541105e55db2&oe=539CD579&__gda__=1402788594_49987650010b48991f7ed972520d3d0 a
https://fbcdn-sphotos-h-a.akamaihd.net/hphotos-ak-xpf1/v/t34.0-12/10343509_1444788725778670_6302209647532174993_n.jp g?oh=87ed52813859d777fd9deb3ebcc54968&oe=539C5E7B&__gda__=1402774896_1b934ba05e9a6cac8e318a5e271bdb9 8[/SPOILER-BOX]

Gardios
Jun 12, 2014, 08:33 PM
Man, I should work on my own assignment instead... :V

I'll just go through the code. What your teacher told you will be on the final is basically what you just got, except with some extra stuff in the beginning.

16: Since you're working with IO, if there's an error when you run the code, the program will throw an IOException.

20: Scanners are simple "text readers". System.in is your console input, meaning the Scanner can read what you write in it.

22: The Scanner reads a number you type in the console and saves it in score.

24: PrintWriter is a text-output. It will write into the scores.txt file.

26: Loop. As long as the scanner doesn't read -1, you repeat the stuff in there.

28, 29: Write the read number into the file and read the next number. println() means that you use a seperate line for each number.

32: Closes the PrintWriter. That's necessary to release system resources associated with it.

34: Creates a file called scores.txt. Since it already exists, you basically link to it.
35: Scanner, this time you read the file.

37: Set a variable.

39-43: Loop. As long as there are lines in the file, you increase count by 1. Basically, since there's a number on each line, you count the number of numbers.

45: Allocate an array with as many slots as numbers you've just counted.

47: Read the file once more. That's so you start from the beginning again.

49: Loop. It loops as long as i is smaller than the length of the array, meaning you repeat the loop for every number you've counted.

51: Places the number from the line in the file into the array.

52: Move to the next line.

57: Set the variable search to a number you input in the console.

59, 60: Variables for use in the upcoming loop.

62: It loops as long as both of the following conditions are fulfilled:
found is set to false.
You haven't gone through the entire array (it's like the for loop from line 49)

64: If the number in the array at position j is the same as the number you#re looking for, set found to true. That means that in the next iteration of the loop, you will break out of it. If you're wondering why there aren't brackets for this if statement, it's because if you only need one instruction, you can leave the brackets out.
66: Increase j manually since you don't use a for loop.

Write in a new file called yourResults.txt

If you've found the number (meaning found is true), then you write that sentence in the file. Otherwise, you use the println in the else path.

Finally, close the PrintWriter to release the resources.

infiniteeverlasting
Jun 12, 2014, 08:57 PM
Man, I should work on my own assignment instead... :V

I'll just go through the code. What your teacher told you will be on the final is basically what you just got, except with some extra stuff in the beginning.

16: Since you're working with IO, if there's an error when you run the code, the program will throw an IOException.

20: Scanners are simple "text readers". System.in is your console input, meaning the Scanner can read what you write in it.

22: The Scanner reads a number you type in the console and saves it in score.

24: PrintWriter is a text-output. It will write into the scores.txt file.

26: Loop. As long as the scanner doesn't read -1, you repeat the stuff in there.

28, 29: Write the read number into the file and read the next number. println() means that you use a seperate line for each number.

32: Closes the PrintWriter. That's necessary to release system resources associated with it.

34: Creates a file called scores.txt. Since it already exists, you basically link to it.
35: Scanner, this time you read the file.

37: Set a variable.

39-43: Loop. As long as there are lines in the file, you increase count by 1. Basically, since there's a number on each line, you count the number of numbers.

45: Allocate an array with as many slots as numbers you've just counted.

47: Read the file once more. That's so you start from the beginning again.

49: Loop. It loops as long as i is smaller than the length of the array, meaning you repeat the loop for every number you've counted.

51: Places the number from the line in the file into the array.

52: Move to the next line.

57: Set the variable search to a number you input in the console.

59, 60: Variables for use in the upcoming loop.

62: It loops as long as both of the following conditions are fulfilled:
found is set to false.
You haven't gone through the entire array (it's like the for loop from line 49)

64: If the number in the array at position j is the same as the number you#re looking for, set found to true. That means that in the next iteration of the loop, you will break out of it. If you're wondering why there aren't brackets for this if statement, it's because if you only need one instruction, you can leave the brackets out.
66: Increase j manually since you don't use a for loop.

Write in a new file called yourResults.txt

If you've found the number (meaning found is true), then you write that sentence in the file. Otherwise, you use the println in the else path.

Finally, close the PrintWriter to release the resources.

man this is really comprehensive, i really appreciate the help. but yea, i'm only having problems with printwriter and arrays, all the scanner and system.out.println stuff i'm familiar with. Arrays and file/printwriter stuff is the new stuff that no one quite understands in my class.

so everytime you want to use a scanner input for a txt file you must state a new scanner specifically for it??

and it seems that for ever new file i create i must also create a new scanner along with it. this confuses me, can't i just use the scanner i stated at the beginning of the code " new Scanner(System.in);" ???

and the thing that i absolutely dont understand is the "hasnext()" part of the whole program. what is the purpose of it besides reading down the lines? how does it work?

Gardios
Jun 12, 2014, 10:15 PM
Nah. You use two scanners because you make use of the keyboard input again in 57.

hasNext() just checks if there's another token after the current one and returns true if it does. You just use it to determine if you're done or not.

infiniteeverlasting
Jun 12, 2014, 10:34 PM
Nah. You use two scanners because you make use of the keyboard input again in 57.

hasNext() just checks if there's another token after the current one and returns true if it does. You just use it to determine if you're done or not.

ah okay i get it! thanks for the quick response

Zorafim
Aug 11, 2014, 02:35 PM
As a quick tip (several months late), if you have a question, you're most of the way through the problem. If you can formulate the question, you can google the question, and find someone with the exact same problem. The most important thing you learn in computer science is how to google.
It may seem like cheating at first, but eventually you get good at isolating exact problems. In the end, you might be working on a completely different project from someone else, but still suffer the same problem. You learn from them, and your original program works flawlessly.

Stuff like arrays and incrementing is something you can easily learn online, once you learn how to (which I admit, is a skill in itself). Once you can learn that, a teacher is superfluous.