Jamal Taylor

Greenhorn
+ Follow
since Sep 06, 2012
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
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Jamal Taylor

Is it at all possible,to convert my number to a string and insert it in the file. After that read it back in as a String and convert it as a integer, so then I can treat it as number. I have tried this using the scanner method and it throws me an exception. Please respond back.
this is what I thought of:


Thanks
5 years ago
Yea, I am going to use the ArrayList and do as you said. It's 2:30 in the morning here so I will try it in the morning. Thanks for your help.
5 years ago

Jeff Verdegan wrote:

Jamal Taylor wrote:Sorry, I will try to be more clear. I just want to append the end of file one time, and every time after that I would like to overwrite the data in the same exact spot.



And will the new data be exactly the same length as the data it's overwriting? If so, RAF may be an option, but I wouldn't use it for a line-oriented text file anyway. (Do you understand why it matters if it's the same size?)

If I wanted to replace the last line in that text file, assuming it's not a terribly long file, I would read the whole file into memory, close the file, reopen it with a BufferedWriter or PrintWriter, and then write out the whole file, with the last line replaced by the new last line.



Can you point me in the right direction how to read the data into the memory and edit like that. I am unsure on how to do this, but yet it sounds very intriguing and promising. Do you mean store the data in an Array and then edit the last index or did I miss something?
5 years ago
HOWEVER, I still wouldn't do that. I'd do the read-then-write approach I outlined above.


Thank you I will try the read then write approach. Give it a try, and hopefully it works.
5 years ago
Here is an example:
Question4 when was the year the gold standard first ended in America
1933
1922
1910
1900
"2"

The number 2 should indicate that user got the question right two times, and if the program ran again it should show
Question4 when was the year the gold standard first ended in America
1933
1922
1910
1900
"3"

When i used the Bufferedwriter class it would write:
Question4 when was the year the gold standard first ended in America
1933
1922
1910
1900
"3","3","3"
The randomAccessFile seemed like a gem, because of the seek method. The problem I had with that was the whole byte problem when writing numbers.


And I don't know why the length matters. Thanks for helping me man. It's the last part of this program. I am not enrolled in the school, I just try to do the homework so I can learn Java by myself. I don't have a TA to help me.
5 years ago
Sorry, I will try to be more clear. I just want to append the end of file one time, and every time after that I would like to overwrite the data in the same exact spot. See I have a bunch of questions written in a text file. I need to record at the end of file how the user scores on the text file. After all the the questions I have a spot for the user score to be placed, so I can track his or her progress over time. It's suppose to be cumulative. I have tried the bufferWriter and it worked but the problem was it kept appending the data, I just needed it appended once single time and thereafter I need to rewrite in that location. Does that make more sense.
5 years ago

Jeff Verdegan wrote:

Jamal Taylor wrote:I am afraid to use other I/O classes because they delete preexisting data.



Not if you use them correctly, as I suggested twice.


Can someone help me out.



I've been trying to, but you've been ignoring my advice. Use the proper class for the job. Since you're dealing with characters, not bytes, that class is a Writer. BufferedWriter or PrintWriter most likely, wrapped around a FileWriter.



Yes, I understand that my problem with using the BufferedWiter Class is the following:
It keeps appending the data at the end of file.
For example, if I have the class write 1 at the end of the file, and the next time it runs it will write 1 besides the previous 1. I need it to write over 1 and not append it anymore. Does that make sense?
5 years ago

Jeff Verdegan wrote:

Jamal Taylor wrote:
The reason why I am using ReadAccessFile is if i use the BufferedWriter it deletes my preexisting data.



Then you didn't use new FileWriter(file, true).


I am trying to append my file. ReadAccessFile class is amazing,



It's also the wrong class for creating or appending to a line-based text file.



See my problem with the BufferedWriter Class is that use it does not delete my preexisting data, but I need to be able to rewrite in the same position. I want to read the variable and edit it and for that to happen it only should append the file once. The BufferedWriter class keeps apending my file, and if I set the constructor to false poof all my data is done. So I thought why not use the RandomAccessFile class.

My problem with RandomAccessFile is writing integers is a pain in the butt. It seems to only be capable of writing it in Byte form and that does me no good. So I am back to square one. I begin to see how something so simple can become so complex with Java. All I want to do is write a bloody integer at the eof one time and be able to continue to write at that location, and the BufferedWriter Class falls short and the RandomAccessFile class would work but it writes Integers.

I don't want to be a pest but I am almost finished with the program, but I am stuck and don't know where to turn for resources. So come to JavaRanch for guidance.
5 years ago
Alright, let me try to explain my problem in more detail. I am asking a bunch of random questions and I am suppose to record the response the amount of times the person has tried and the amount the person got it correct. I am afraid to use other I/O classes because they delete preexisting data. So I decided to use RandomAccessFile class it works i thought. Here is the part of the code that is relevent


It prints the this on the end of my file " " weird characters that I cant get to correctly display on this screen. Very frustrated. Can someone help me out. I am new to coding. I started during the summer and now I am hitting a brickwall going 100 mph in a Ferrari.
5 years ago

Paul Clapham wrote:It's also true that Notepad won't treat the \n (new-line) character as a new line. Officially the way to end a line of text in Windows is with the two characters \n\r (new-line, carriage-return) but Notepad is the only text editor that I know of which actually enforces that rule. Even Wordpad will treat \n as if it means the end of a line.



Open the file in a different editor it works perfectly. Is there a way to get it to appear like that in notepad?

Thanks
5 years ago

Jeff Verdegan wrote:

Jamal Taylor wrote:
When I read the data back to me it's all on one line.



Of course it is. If you read the docs for the methods you're using, you'll see that none of them says anything about writing newline characters.

Why are you using RAF anyway? If you just want to append text to a file, use the FileWriter constructor that takes an append argument. If this is just a text file, then use BufferedWriter or PrintWriter so you can write characters rather than bytes.



The reason why I am using ReadAccessFile is if i use the BufferedWriter it deletes my preexisting data. I am trying to append my file. ReadAccessFile class is amazing, I just need to know how to get it to write to next line.
5 years ago
I have been struggling for sometime trying to get randomaccessfile class to write a newline. I know how to append the file using the length method, but it will just result in writing the code at the end of the file. I need a new line at the end of the file. I tried all the simple ways with no luck here is one way I tried


When I read the data back to me it's all on one line.
5 years ago
Thank you, and I was in my mind still thinking about solving the problem in terms of Java.

I have a question. While I am writing my pseudocode is it at all possible to create a check to see if a character is a numeral instead of a letter? I have a text file which is just a few questions written in a multiple choice format. What I want to do is after every question mark write a number let's say 4 for example. I then can set an instance variable to that number and the instance variable can tell to the program how many new lines to create. The new lines would represent each choice the user has to pick for the right answer for the multiple choice question. I wrote this down in my pseudocode it appears that the logic will work, but I am wondering if it is at all possible. I was thinking of something like this:
I do not want to hard code anything in, because all the questions have different lengths, but each question must have a question mark. So I am trying to use that to my advantage. As you see, I have already set the placeCounter to the size of the array, so in the future I can control how big the array is. I want to be able to record the number right after the question mark. I then can add quiz questions in the future, and just for each quiz question put a number after the ? to tell the program to write four new lines or 5 new lines. It was easy with Question1 because thats static, but the number after the question mark is not static and will change with every given question. So how do I tell the program to record the next iteration after the question mark?

I was thinking if the next mark after the question mark equals a numeral then set it to newlines. I just don't know how to do that.

Thanks in advance.
5 years ago
Thank you so much for your responses. You're 100 percent correct, I did not take the time out to correctly create a pseuocode. I want to dive right in and start coding away. It was a hard urge to resist. I erased the prior code, and started with the pseudocode. I really want to develop good habits that will pay dividends in the future. I looked up instructions on how to do pseudocode and then I attempted to do my own example. Please do not hesitate to criticize me. I have thick skin and I want to learn to become a good programmer.

1. Read in Text File by using the scanner method

2. While there is a next iteration
store data into a variable
Use the variable with stored text iteration and add it to the Arraylist

3. Display data into a Quiz like format
For loop's increment equals "Question 1" the data will be printed on a singe line.
For Loop's increment equals "?" the next iteration will be stored as a variable of type int.
Print the amount of corresponding lines according to the variable. When completed, newline.


Does this look like I am on the right track with my Pseudocode?
5 years ago
I am doing an assignment for school. I actually don't attend the school. I am doing the homework so I can get the education without paying the tuition. The problem with this is I don't have a TA to answer my questions. So I come to the experts.

Okay my question, is when I print data from an ArrayList is there a methodology of controlling your output? See, I want to be able to read in data from a Text file, then store it in the Arraylist(This part was easy), and then print the data out in a quiz like format. Here is what the output should look like:

"How many licks does it take to get to the tootsie roll center of a tootsie pop?
4
one
two
three
four "
Now I have done that it almost is perfect but for some reason I am getting erroneous information. My output is:
How many licks does it take to get to the tootsie roll center of a tootsie pop? " 4 0:4"
1 one
2:two
3:three
4:four

I am unsure on why I am getting that bit of extra info on my output.


Now doing this, I foresee me having trouble with adding another question, because the loop initial loop will continue to print my data out and make it sloppy. Any suggestions on how about reading data from an ArrayList then printing it out and at some point in the ArrayList adding a newLine so it can be in a format like multiple choice questions?
5 years ago