Shannon Pitts

Greenhorn
+ Follow
since Dec 30, 2011
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
1
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Shannon Pitts

That was just the thing I needed! Thank you. I feel like parts of my code may be a bit redundant, but I can go back and clean it up after I finish making it work.
8 years ago
I'd never even heard of these concepts before, but Jason's example was amazingly clear and precise. If only more tutorials could be written so well. Thank you!
8 years ago
Okay! I got the file reading and writing working. I can create tasks and have them save to the file when I close the program. Victory!

I've run into another snag. I'm trying to build the delete functionality right now, and everything works, except I can't figure out how to prevent it from throwing an exception if the user enters a number that isn't on the list. (IE, they choose 4 when the highest number on the list is 3). Supposedly ArrayList.remove() returns true if the item is found and successfully deleted, but I can't figure out how to capture that and use it in a while statement so that if it returns false, the user is told to pick a different number. I thought about just checking to see if the object exists before attempting to delete, but I've found that it's very difficult to reference an object in an ArrayList the way you would an array. I can't just type taskList[N] because it insists on having taskList<Task> but if I stick [N] on the end of that, it gives me an error. I'm not sure how I'm supposed to reference these objects.
8 years ago
Maybe there's a better way to do this.. let me describe what I'm trying to do, and perhaps you'll know an easier way to accomplish what I'd like.

When the program is started, it looks for a file called tasklist.txt. If it finds the file, it reads the information in the file line by line, and assigns the information it finds into task objects, one for each line (I haven't worked out how to do that yet, but I'm sure it's possible). Then the user does whatever they want to do in the program, be it creating, editing or deleting task objects, as well as displaying the current list (created by iterating through the array that holds the task objects, not the text file). Then when the user finishes, the last thing the program does before quitting will be to write the task objects in the array back to the text file, one line per object.

Is it possible to open the file, read out the contents and save them into objects, then close the file, then when the program closes, reopen the file to write, write the data, then close both file and program? The only problem I can see is that if the program closes unexpectedly you'll lose any changes to the list since the last save, but I can't think of any other way for the program to have the data persist, besides databases which I'm not quite ready for yet.
8 years ago

Campbell Ritchie wrote:Are you sure it’s your back you are scratching?



Just being polite!
8 years ago

Campbell Ritchie wrote:You appear to have two independent problems: One is how to read text from a file, which is best done with a FileReader and a BufferedInputStream, which you will have read about in the tutorial.
The other is how to create an object from the data in your file; that depends on the format of the file and all its information being in the correct order and the correct positions.



You are correct, but I also want to be able to write any changes the program makes back out to the file again, which is where I think the output stream would come in.
8 years ago

Campbell Ritchie wrote:I haven’t got a link to it on this computer, but Google for Java Tutorials file input output and you should find something useful This is one of the hits from that Google search.



Thanks! I'm actually looking at that right now, though I'm running into the age-old problem of them telling me WHAT I can do, but not WHY. All I know is I want to check for a file, open it if it's there and create objects based on the lines in it, or create the file if it's not there, then when I'm finished with everything, write the new information back to the file and close it. I don't know whether I need a BufferedOutputStream or what have you. I've been writing down words I don't recognize so I can look them up, but it's slow going.
8 years ago
I'm confused-- why would you do this at all? Surely this is like reaching around your elbow to scratch your back?
8 years ago
This is my biggest frustration with every Java tutorial I've found so far-- they tell you WHAT to do, but not WHY you're doing it, so once you're working on something that is not that exact project, you have no idea whether or not you should be applying that thing in the same way.
8 years ago
Yeah, I noticed that. If I just changed the one line I needed to nextLine(), it did what you're describing, but by changing them all to nextLine() I haven't had any troubles with it.

Now I just need to learn how to deal with files!
8 years ago

Campbell Ritchie wrote:Add a line like this to display the input: System.out.printf("Input = \u201c%s\u201d%n", input); where you are calling the String in question input. You will get “” around the output.



Thanks, but I also figured out I can just use Scanner.nextLine() instead of Scanner.next() and it will go until the user hits enter.
8 years ago
After I enter in the description of the task I'm creating, I press enter and receive back "Task Added! What next?" then "Improper Command" twice in rapid succession without me pressing any more buttons, that's what makes it really strange.

And yes, "bells and whistles" is an idiom; it basically means that I'm adding fancier things to my (currently) simple program.

Thanks for the information about Eclipse! I'll mess with it a bit and see if I can get some more answers.

ETA: Yeah, I'm using Java 7. I can't imagine how else I'd be able to use the keyboard input to determine what the program does next. I guess it used to be kind of complicated?

Edit Again:

<del>Okay, upon further testing, if I use a command as the name of the task, I don't get the "Improper Command" response, so my theory is that even though the input is used inside the new case, it's also being checked against all the other cases, but the output isn't being put out until that case breaks and we go back to the beginning of the loop. I'm going to try changing names of variables to see if that fixes things.</del>

Turns out it's only grabbing the first word, not the whole sentence if you type more than one for the description! Then it's going back and using the other words as commands!
8 years ago
Hi, I literally just started programming tonight, and I've hit a bit of a snag. I'm writing a little to-do list type program, and right now I'm just running it on the command line because GUIs are for later. I've got this so far:

I tested all the cases before I started adding bells and whistles to the "new" option. Everything worked fine, program exited prettily and all was well. I added in the actual statements for the "new" command and now when I run it, I'll get all the way down to displaying "Task added! What's next?" when the program seems to run through the loop twice and spit out "Improper command. Please try again." (from the default case) twice. I can continue typing as normal and the rest of the commands work at that point, but I can't seem to figure out why I'm getting this bizarre behavior.

I'm using Eclipse IDE and I haven't quite figured out how to get it to show me my variables while the program is running, and I imagine that would help immensely. If anyone can tell me what I'm doing wrong here, I'd really appreciate it.
8 years ago