• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Loading data from a Text File and printing it out on the console having trouble

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had to change your comments: don’t use // for several lines unless you need to comment‑out some code. Use /*...*/ And read what it says here and here about line length.

Your code looks as if you are guessing and trying different things to see whether they work. They never will (unless you are very very lucky).
You need to write down what you want to do. You have written something down, which doesn’t look like anything I would do. Then you write down how to achieve it.
Anything which requires subtraction by 17 is going to be very brittle. You will find an explanation about simply writing numbers here. How do you know 17 is correct and will remain correct? Don’t use is as a variable name; it is confusing.
 
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

Jamal Taylor wrote: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:...


I think, in order to advise you properly, we really need to know what this input file looks like. However, I will say that your code looks awfully over-engineered. For starters, you definitely don't want to hard-code numbers like 4 and 17 into it, because you'll only have to change them if your input changes.

My advice:
1. STOP CODING.
2. Write down what you're trying to do - in English.
Describe the input you're getting and the output you want to see, exactly; and then describe the steps needed to get from one to the other. In detail, and NOT in Java-ese.
AND (and this is the really hard part) don't think about the code you've already written while you're doing it; it's plainly not working, so treat it like Kleenex for the moment.
3. Now turn your computer back on, and see how you need to change your code - or perhaps better still, start a new program.

Programming is about thinking, not coding; and you'll never be able to solve a problem in Java if you can't describe it in English.

HIH

Winston

[Edit] Too slow (as usual).
 
Jamal Taylor
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my PERSONAL opinion, pseudo-code should be platform neutral - especially for a first go. for example:

1. Read in Text File by using the scanner method


What if I wanted to write this in Perl? or C++? or Assembly? "by using scanner method" tells me you are thinking about HOW to do it in Java, not WHAT to do. It would be better to start with:

1) Read file one line at a time



Once you have done this, and then gone back through and revised it several time...THEN you can start thinking "Ok, so how do I do that IN JAVA".
 
Jamal Taylor
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jamal Taylor wrote: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?


Sure; just don't write it as Character.isDigit().

Like Fred says, pseudo-code should be language neutral, which forces you to concentrate on what needs to be done rather than how you're going to do it.
So describe actions, not methods. If whatever language you end up writing your program in doesn't support it natively, you may end up having to write a method or function to achieve it, but that SHOULDN'T concern you while you're writing the pseudo-code.

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...


OK, so it looks like your file has a very specific format. What is it? Describe it in detail - and, again, in English. Use examples or pictures if you need to. One form that can be useful for describing stuff like that is Backus-Naur, but I doubt if anyone will be expecting you to know it at this stage. Some simplified version, like you often see in books for describing syntax might be an alternative, eg:
question-number : question-text
number-of-answers
answer
[answer...] // repeated as necessary for number-of-anwers

as long as it means something to you (and hopefully other people).

I then can set an instance variable to that number and the instance variable can tell to the program...


No, no, no. You're back to describing HOW again. In your pseudo code all you need to say is something like 'for each answer...'; DON'T start describing the mechanics. You may even surprise yourself and come up with a much better way of doing it when you start thinking about the code (or indeed, your file format), but you WON'T if you tie yourself down at this stage.

As you see, I have already set the placeCounter to the size of the array...So how do I tell the program to record the next iteration after the question mark?


You are allowed to use a bit of "program like" logic in pseudo-code, but again, it helps if you keep it language-neutral, so things like:
'set questionCount to 0' is better than 'questionCount = 0'
and
'increment questionCount' is better than '++questionCount' or 'questionCount = questionCount + 1'
but in general, it's best to stay away from this sort of low-level stuff at the pseudo-code stage, unless the algorithm you're describing is very complex.

Basically, stay away from "code" until you're absolutely sure you know WHAT needs to be done.

HIH

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