• Post Reply Bookmark Topic Watch Topic
  • New Topic

Stuck on java assignment with minimal direction(file io)  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So,I've been working on this program with my girlfriend for a java class, and with as much progress as we've made, the professor has left us to our own devices and we're stuck. We're supposed to make a program consisting of 3 classes that opens a file at the user's discretion(just a text file that contains a list of random 'customers') and then takes a customer number from a follow-up input and displays the customer's information(customer number, first name, last name, balance). As far as functionality goes, it literally just opens a text file we've made and reads the variables we've set. I think we have the bulk of the code done, we just don't have the java knowledge to make the final pieces work, nor do we even know if the existing code functions properly. There's also some odd errors in CustomerList saying that ";" is expected, but there's already a semicolon.

Sorry for the wall of text, if possible I'd like to be able to get on a chat with someone that could help walk us through what is wrong and what is missing; not to discourage forum input :p

edit: Some imports are repeated because the instructor wants each class in a different .java file.

 
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nicholas Granat wrote:We're supposed to make a program consisting of 3 classes that opens a file

What's the format of the file?

Nicholas Granat wrote:I think we have the bulk of the code done, we just don't have the java knowledge to make the final pieces work, nor do we even know if the existing code functions properly.

Think about WHAT you want to do instead of HOW are you going to do this.
Useful links:
StopCoding
WhatNotHow

Nicholas Granat wrote:There's also some odd errors in CustomerList saying that ";" is expected, but there's already a semicolon.

What exact error? At which line?
Tip: How do you refer to a field in an array in java?

Nicholas Granat wrote:Some imports are repeated because the instructor wants each class in a different .java file.

That's normal. Every public class must be in separate .java file. What you can do is to edit your post to put every class/file in separate code tag pair.
 
Nicholas Granat
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. File format is supposed to be 100 lines of "n firstName lastName randomBalance": i.e. 82 Ted Danson 102.63.

2. We've been trying that, but lack of java prowess and syntax knowledge kind of gets in the way. Our prof is very practice-oriented rather than instilling textbook fundamentals. I've had multiple instances on class programs where I had no idea how I made something work, but got commended on making it work. My File knowledge is limited to a few powerpoint slides at most.

3. Lines 55-58. 55 gives an error saying "}" is expected, and the other 3 say that ";" is expected, but they're already present >.>

4. I thought it abnormal to not have all classes in the same .java file, though I know they both work.

I'm going to give the WHAT not HOW approach another go and see if I can make headway before the program is due. Thanks much for the feedback!
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nicholas Granat wrote:3. Lines 55-58. 55 gives an error saying "}" is expected, and the other 3 say that ";" is expected, but they're already present >.>

Sometimes the error is in another place that it seems. The message say that ";" is missing. The reason is you use incorrect syntax regarding your array.
To get an element of index i from array data you need to write data[i] instead of just [i].
Also, be careful. I see the NullPointerException is lurking and ready to strike ;).
 
Nicholas Granat
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ugh, I feel dumb for missing something that obvious lol. I'll hopefully have time to work on it more after work tonight. What would be a good angle of attack for my main method? I have no idea if that Scanner block will even work, I just tried Googling until I found something kind of close to what I thought I needed.
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nicholas Granat wrote:, I just tried Googling until I found something kind of close to what I thought I needed.

That path is really going to doom you to failure in the long term.

Did you read the links Pawel showed you?

Good coders usually start by NOT writing code. They probably spend a lot more time thinking about things than writing things. So, start with thought exercises. What kinds of things does your program need to do?

You need to (in no particular order):
open a file
read lines from a file
get a file name from a user
get a customer number from a user
break a string apart into pieces
print something to a screen

now you start looking at those, seeing what you know how to do and what you don't. You also notice that "getting file name from user" is really the same thing as "getting customer number from user" - both are "get input from user".

You realize that opening a file has NOTHING to do with getting the file name. It doesn't matter if the file name comes from the user, from a database call, is hard-coded...once you HAVE the name - regardless of how you got it - opening the file is done the exact same way.

once you break your code down into simple pieces, you start writing those pieces one at a time. you make sure the file open part works 100% before you try and get the user input of the file name. Then you get the file name from the user, and don't worry about opening the file until you are SURE you can get the name.

Then you wire those two pieces together.

It looks like your trying to do too much too fast. Never write more than 2-3 lines of code before you re-compile and test.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nicholas Granat wrote:Ugh, I feel dumb for missing something that obvious lol.

Don't worry. We've all been there.

What would be a good angle of attack for my main method?

Well, first would be to keep it as small as possible. Experienced programmers rarely put more than a few lines in it; and that's usually to instantiate an object which actually does the work.

But getting back to what Pawel said about WhatNotHow (←click): He's absolutely right.

And how do I know that? Because you've created a CustomerRecord class, instead of a Customer class - ie, you're obsessing about how to get the data from the record in your file, rather than creating a 'Customer' object that does what you need it to.

In your case, a Customer object should know how to instantiate itself from a text record, so I would expect it to have a constructor - or possibly even better, a factory method - like:
Customer(String record)
or:
Customer(Scanner input)
(or indeed, possibly both)

I've created a page called FirstClasses that describes a pattern for creating 'beginner' classes that you can "cookie-cutter", and that covers this topic (among others) in detail. However, it's still something of a work-in-progress, and it's quite long; so you may want to wait until you have time to read it. You may find some useful pointers though.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!