Edwin Torres

Ranch Hand
+ Follow
since Mar 19, 2011
Edwin likes ...
Java Linux Python
Family man, programmer, college professor.
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
5
Received in last 30 days
0
Total given
7
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Edwin Torres

chamini-

All Java objects have a default toString() method. In your code, this method is called on Line 9 when it tries to print result. In the case of an array object, the default toString() method returns a String that is the hashcode of the array (that is what you are seeing). To print each element of the array, see Jeanne's post or use a loop to print them manually.
2 years ago
Okay. Let me post a proper response then.

Line 01: This line declares a static variable named SEPARATOR. Since it is static, you do not need an object to access it; it belongs to the class. It is private, so it is only accessible from within this class. It is final, so you cannot change its value within the code. SEPARATOR is basically a constant.

Line 02: This line declares a static method called getAllMembers. A static method belongs to the class, so you do not need an object to invoke it. Use the class name to invoke it. This method is declared as public, so it is accessible outside of this class. In other words, a user of this class can create an object and invoke this method.This method returns an ArrayList of Speaker objects. An ArrayList is a built-in Java class that is similar to an array, but it includes some helpful methods. There should be a closing parentheses (}) to match the opening parentheses on this line, but your code excerpt does not show it.

Line 03: This line declares a local (inside the method) ArrayList of Member objects named members. The part to the right of the equals sign creates an empty ArrayList of Member objects. This empty ArrayList object is assigned to the members reference. The method eventually returns members . However, the return type is an ArrayList of Speaker objects. So it appears that this code will not compile successfully. You should probably change the return type (Line 02) to: ArrayList<Member>.

Line 05, Line 23, Line 23-Line 25: These two lines define a try block. If exceptions are thrown inside this block, it is passed to the catch block (Line 23 - Line 25). The catch block prints the stack trace of the exception. The try-catch block is needed because Line 07 could throw a FileNotFoundException exception.

Line 06: This line defines a File object named file for the text file with the String name that is defined in the Resources.MEMBERS_TXT field.

Line 07: This line defines a Scanner object named fileReader to read the File object. Scanner is a built-in Java class that can scan input and parse fields.

Line 08 - Line 10: These lines declare three variables that are used inside the while loop.

Line 12: This is the start of the while loop. The hasNextLine method of the Scanner class returns true if there is another line of input. If it is true, then the while loop begins executing its statements. If there are no more lines of input, hasNextLine returns false and the while loop ends.

Line 13: This line calls the nextLine Scanner method. This method returns an entire line from input and positions the Scanner object on the next line. Since the Scanner object was created to accept input from a file (Line 07), it reads the first line of the file and positions the Scanner on the next line of the file. The nextLine method returns the line as a String object. The String split method is invoked on the String object. This method splits the String into an array of fields, delimited by the SEPARATOR on Line 01. Finally, this array of String objects is assigned to the properties array variable. So for each line in the file, it splits it into fields (separated by commas) and stores the fields in the properties array.

Line 14: This line creates a new Member object and assigns it to the member variable. It calls the Member constructor that accepts three parameters: String, int, String.

Line 15: This line passes the first String of the properties array as the first parameter of the Member constructor. Note that it first invokes the trim method on the String, which removes any leading or trailing whitespace.

Line 16: This line passes the second String of the properties array as the second parameter of the Member constructor. It invokes the trim method on it as well. Then, it takes the resulting String and passes it to Integer.parseInt. This parseInt method converts a String into its equivalent integer value. So if the String is "99", it returns the integer 99. Also note that the parseInt method throws a NumberFormatException exception if it cannot parse the String value into an integer value. The catch block would also catch this exception.

Line 17: This line passes the third String of the properties array as the third parameter of the Member constructor. It first invokes the trim method on the String, which removes any leading or trailing whitespace.

Line 18: This line invokes the setPaymentDate method of the Member class on the newly created member object. It passes the fourth String of the properties array (trimmed first) as the parameter.

Line 19: This line adds the member object to the members ArrayList.

Line 20: This is the end of the while loop.

Line 22: This line occurs after the while loop ends. It closes the fileReader object.

Line 27: This line returns the members ArrayList object and the getAllMembers method ends.

Summary: The code appears to read an input text file line by line. Each line has fields separated by commas (e.g. string1, number, string2, string3). The first three fields are used to create each Member object. The fourth field is used to set the payment date field.
5 years ago

Kin Kinov wrote:Hello everyone, I'm new to programming and java and I'm trying to understand how exactly this piece of code works and what exactly does.
I'm trying to make my first GUI application and to read some person details from a file and then put the result in a Jlist.



It's pretty obvious what it does. Do you know what the MEMBERS_TXT file looks like? Have you looked up the String split() method? Do you know what Scanner does? Do a search on the String and Scanner classes. You'll learn what they do and how to research things on your own. There's a lot of information about Java online.
5 years ago

Jack Lickman wrote:So I guess there was like a thousands of these topics, but I hope you will help me out anyway...



There are a lot of free online resources for learning Java. I write about them in my blog (shameless plug below). You can start by looking at the tutorials at Oracle. The Getting Started trail is a good one. But if you are new to programming, you might want to learn some basic programming concepts first. Codecademy has a good interactive, web-based learning tool. The Python tutorial teaches basic programming concepts (input, output, decisions, loops, etc.). You'll learn a little Python too. Learning the basic concepts will help you learn any programming language.

Then, and most important, practice writing lots of programs. You will learn something from every program you write, no matter how small. Malcolm Gladwell said it takes 10,000 hours of practice to become an expert at something. I can tell you that when I hit that mark in programming, my understanding was far above it was in the first hour. Practice a lot.
5 years ago

Noah Osornio wrote:nevermind, it works either way. However, I am experiencing a problem in game where when I roll a number. The numbers don't add up, and if I hold the number will hold, but when I roll its says that if I hold I gain as many numbers as I had in storage. Is there a reason for this?



In your latest code, you never update humanTotal or computerTotal. For starters...
5 years ago

Noah Osornio wrote:I thought so based on the last statement. Now I wrote the new code, but for some reason the compiler says my else statement is wrong? I checked my previous one and performed the same steps, but I am still getting this error for some reason.





The error is here. You cannot place a statement in between the IF and ELSE clauses of an IF-THEN-ELSE statement. You probably meant to put that inside the previous IF clause.
5 years ago

Noah Osornio wrote:So, if it goes to the winner by default if the limit is reached. One more question if you don't mind. Do I need any booleans to make this work or can I get by with only int? Also, what does "<>" mean?



Whether you use boolean variables or not is up to you. I don't think you need them based on my pseudocode. And "<>" is another way of saying "not equal to". In Java, that's: !=
5 years ago

Noah Osornio wrote:okay, thank you for your help. The way you organized it also helps because I didn't know how to place in the code for both the human and the computer when they are less then 100, but placing it in the first while loop makes sense. Does that mean there should be another while loop for those who reach over one hundred or is that nested inside?


In the pseudocode that I provided, the outer (main) WHILE loop ends when one or both scores are greater than or equal to 100. When you break out of that loop, you just have to calculate the winner by comparing the two scores. There is no need for additional loops.
5 years ago

Noah Osornio wrote:seems to be the case. Now I understand that the while loops are actually the end of do-while loop which explains somethings.


Yes. There are two types of WHILE loops. WHILE (checks the condition first) and DO-WHILE (checks the condition at the end).
5 years ago

Noah Osornio wrote:yes, the human always goes first. Also, I never thought of having it in English form (it sure does make more sense). Thanks, I will try making a code with that in mind.


Using pseudocode let's you workout and even test your solution without having to worry about the coding details. I tell my students to solve the problem first, then code. That being said, I think the pseudocode I provided is good, or pretty close to what you need.
5 years ago

Noah Osornio wrote:I am creating a pig game (dice game) for my CS 140 class. ...When the game runs, it only runs through the human turn and the AI's turn, but then stops after that. My question is how do I keep the game going? Any help will be great.



It might help if you work out your logic using pseudocode (plain English). Get a clear understanding of the solution first, then implement it in code. For example:




I'm assuming that the human always rolls first, and that the computer keeps rolling until it sees a 1 or already gained 20 points.



5 years ago

Omkant Parashetty wrote:
Can some one please explain why we are not able to read second string in this program.


Your first next() method returns the next token that is delimited by whitespace (default). If your first input is "A B C<cr>", then s1 is assigned "A" and "B C<cr>" is still in the input buffer.

When nextLine() executes, it takes the remaining line (up to the line separator) from the input buffer and assigns it to s2. So s2 has " B C". The nextLine() method additionally consumes the trailing line separator and advances the position to the beginning of the next line. Note that the user does not have the opportunity to input a value for s2 because the Scanner must consume the leftover characters in the input buffer first.

When the next next() method executes, it again returns the next token delimited by whitespace. There are no characters in the input buffer and the position is on the next line. If your input is again "A B C<cr>", "A" is assigned to s3. "B C<cr>" is again left in the input buffer.

The final next() statement assigns the next token "B" to decider. "C<cr>" is left in the buffer.

Hope this helps.

5 years ago

Madu Biradar wrote:I want to get SCJA SE 7 (India )so what i need to do? how to get voucher and certification?


Register for your exam at Oracle: register.
Find a testing center near you: locate a center
Rock and roll.
5 years ago
Hi everyone,

I just wanted to post a link to my blog: java Friendly http://javafriendly.blogspot.com/.

It's a blog for new Java programmers. Comments, suggestions, future topics welcome! ;-)

Thanks.
5 years ago