Computer Science Canada Printing output arrray |
Author: | QuantumPhysics [ Tue Oct 23, 2012 6:05 pm ] | ||
Post subject: | Printing output arrray | ||
In Computer Science class our teacher gave us about 400 little assignments we had to complete throughout the school year. I rushed ahead and completed all of them 3 months early. This is the only one that has been a pain for me. I cannot understand why the program terminates at final output after the response was given. It does not output the results, it just terminates in my console. I remade the same program in C and it worked flawlessly. I don't understand why it doesn't work here. Would someone care to explain? Thank-you ~ QP
RE: Even though the answer may be in front of my eyes, I still cannot figure it out... This is harder than the polymorphism and multi-threading assignments we had to do... Am I not outputting it correctly, or what? My teacher could not even get it (obviously) ~ She says "I will take it home and look at it" or "You are disrupting me" or "Did you try and run the program and see if that works." <- I laughed so hard at the last one .... "Could that really be the answer?" |
Author: | DemonWasp [ Wed Oct 24, 2012 12:09 am ] |
Post subject: | RE:Printing output arrray |
Because == doesn't compare string contents, it compares String objects. Use .equals() or .equalsIgnoreCase() in your if / else-if conditions. |
Author: | QuantumPhysics [ Wed Oct 24, 2012 1:20 am ] |
Post subject: | RE:Printing output arrray |
Seriously, hmm, let me try that. But how come it works in C++ and not java? Because java is weird in that manner? RE: Alright, I changed it but now after I type pass for example it outputs: null0null0null0null0null0null0null0null0null0null0 Meaning somewhere in my program values are not assigned to the arrays i chose to output, correct? |
Author: | QuantumPhysics [ Wed Oct 24, 2012 8:17 am ] |
Post subject: | RE:Printing output arrray |
Oh! I see the error. Okay nevermind, I just parsed indentName. And changed the assignment order of the mark[i]=pass[i] and so on. |
Author: | DemonWasp [ Wed Oct 24, 2012 9:20 am ] |
Post subject: | RE:Printing output arrray |
The reason that "works" in C++ and not Java is because in C++, std::string overloads operator== to do comparisons using the contents of a string. Similarly, it overloads operator<=, operator+, operator+=, and several other operators. Java doesn't permit operator overloading in general, with exactly one exception: you can concatenate strings with +. Java's behaviour is closer to what you would get if you used C-style strings: if you compare the variables themselves, you get the wrong result, because the variables are pointers (safe pointers in Java's case) to the contents of the string. You have to call a special comparison function (strcmp, .equals(), .equalsIgnoreCase() ) to compare the guts. This is true of all reference-typed objects (class instances) in Java. To compare for equality, use .equals if it's an object, == if it is primitive (byte, short, int, long, float, double, char, boolean). Use Array.equals() for arrays, whether primitive or not. |