Tim Holloway wrote:This is kind of gauche to me: "memset(left, '\0', 50)". If, as I presume, you mean for '\0' to stand for the octal byte value \000, I'd recommend that you follow common practice and write all 3 digits.
Although in actual fact, I'd simply code "memset(left, 0, 50)", as "0" is the universal nothing in C, signifying ASCII NUL, floating-point 0.0, and null pointer value (which, interestingly, on some systems is not actually address 0)..
Then again, I think that there may be a standard memset-equivalent that's explicitly intended to deal with character arrays. A character, after all, isn't always exactly one byte in the wider world.
As to getting a binary 1 for pass 1, my first instinct would be to see if the loop index was somehow getting pushed into a character.
But I think your code would probably be clearer (and easier to debug!) if you considered doing that central loop block using a switch statement.
Campbell Ritchie wrote:Have you verified where that \001 is in you file? Have you verified that all the characters in that file are ≤ 0xff? What is the encoding for your file? If you have any characters ≥ 0x100, or anything non‑ASCII, there is the possibility of an unexpected pairing.
Ron McLeod wrote:
Adrian Meneses wrote:Does not print even though it compiles and runs just fine. I can enter lines of text but it will not print how many lines have been entered. What is wrong?
The code is going to stay ion the while loop until there is no more input - how are you terminating your input? From the Linux command line, you would normally use Control-D:
You could also test by redirecting standard input from a file:
Adrian Meneses wrote:Hello all,
I'm trying to print the contents of a queue using nodes but I get duplicates for some reason and I don't understand why. Here is my code:
The unbounded interface just overrides a queue interface where the enqueue, dequeue, size, and toString methods are. The console prints the last item added to the queue. For example, if I add Mary with ID 10, Jon with ID 20, and Adrian with ID 30, the program will print:
Student name: Adrian and the ID is: 30
Student name: Adrian and the ID is: 30
Student name: Adrian and the ID is: 30
Please help me by pointing where the error/bug is, tell me in a conceptual way so I see what's wrong and to code it.![]()
Campbell Ritchie wrote:Two more things:
1: Before popping something it will be necessary to null out the location in the array, otherwise you are risking a hard reference to an object no longer required. 2: As an alternative to throwing an exception when the array is full, it is possible to copy the entire contents into a larger array.
Junilu Lacar wrote:A couple of other things:
1. Your push() logic looks faulty to me.Seems like you would get an ArrayIndexOutOfBoundsException on the very first call to push() a Book unto a new StackNever mind, I see you did the increment and use of the index in two different statements. That's not how it's normally coded though. You wouldn't initialize the index of a new stack to -1 but 0. Then do the post increment at the same time you're accessing your stack array.
2. You declared the myBookStack variable as Stack not StackInterface. What's the point of declaring and implementing StackInterface if you're not going to use that abstraction?
Campbell Ritchie wrote:That looks as though you need a way of enumerating languages. How much do you know about enumerated types? There is a lot in the Java® Language Specification about them, too.
Knute Snortum wrote:You don't need parameterization to do what you're describing, just three instances of MyStack. If you wanted to -- and it might be a good idea -- you could parameterize MyStack so that it could hold Books or Clocks or Chickens or whatever, but you don't need to do that to have a stack of English, Spanish, and Other language Books.
Knute Snortum wrote:Why is the class Book parameterized? What would T be for a Book? Do you use T in the Book Class? What does BookInterface look like? Why do you need to implement it in MyStack?