• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Need help understanding a piece of code.

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch! Thanks for using code tags.

In general, you'll get better responses if you show that you've put some effort into solving your problem. You may have done this, but we don't know it. So...

Do you have a specific question? What have you tried? What do you think the program does?
 
Ranch Hand
Posts: 57
Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Knute Snortum
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Edwin Torres wrote:It's pretty obvious what it does.



Not to someone new to Java.
 
Marshal
Posts: 80756
487
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, it isn't obvious. You should be using nextInt rather than String#split and Integer#parseInt.
It is also not obvious from the code that a Member is a Speaker. Presumably they are subclasses of each other. In which case the return type is incorrect; a List<Member> IS‑NOT‑A List<Speaker>, so that code will probably fail to compile.

If that method is not in the Member class, it violates the single responsibility principle. A class should take care of itself, and this appears to be getting another class to do the work. There ought to be a method in the Member class like valueOf(String). Then you can call it like this:-All the bits about nextInt belong in the valueOf method. That is a factory method, and you usually make factory methods static.Line 6: Note lots of documentation comments. You should use them to work out what that factory method does. If you suffer an Exception it will probably be an InputMismatchException, in which case you may have to check whether your file is corrupted.
Line 12: You can create a Scanner with a String. It can take the String above and take is apart into tokens. Try the block of code in the following post. Then you can see how it works, and also how you can use those tokens in the Member constructor/setDate method. \u201c and \u201d are posh quote marks.
Line 13: A Scanner Line uses whitespace as a default to separate tokens. That String represents any amount of whitespace followed by a single comma followed by any amount of whitespace. \s means whitespace and * means any amount, permitting 0 instances. That is a regular expression which you can read about here if you have the time.

I pushed the wrong button and posted this post before it was complete. So I deleted it, edited it, and shall restore it from what I deleted.

The code above has nothing to do with GUIs, so you should make sure your entire application works at the command line before you even think about GUIs.

And welcome again
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:Not to someone new to Java.


Indeed.

@Edwin: Be nice. Knowledge can be a curse, you know.

Winston
 
Campbell Ritchie
Marshal
Posts: 80756
487
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this code to demonstrate that Scanner working[edit]Corrected code formatting and starting number: use 0 not 1[/edit]
 
Campbell Ritchie
Marshal
Posts: 80756
487
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A few minutes ago, I wrote: . . . Line 13: A Scanner Line uses whitespace . . .
And welcome again

Mistake in first part. Sorry. It should read

A Scanner uses whitespace . . .

No mistake in second part
 
Campbell Ritchie
Marshal
Posts: 80756
487
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have mistakenly deleted a line while doing all that editing. Sorry. The valueOf method should read like this:-Add line 17.
 
Edwin Torres
Ranch Hand
Posts: 57
Python Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Edwin Torres wrote:Okay. Let me post a proper response then.


Now that's more like it.

Winston
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic