Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Printing output arrray
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
QuantumPhysics




PostPosted: 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

code:

/**
 Written by: CodeMaeTrix
 Teacher: Dowlut
 Date Written: 23/10/12
 Header: Arrays - Question 5
 **/

import java.util.*;
public class arrays5 {
        public static void main (String args[]){
                Scanner input = new Scanner(System.in);
                String indentName, response;
                int pass[] = new int[10];
                int fail[] = new int[10];
                int mark[] = new int[10];
                String passName[] = new String[10];
                String failName[] = new String[10];
                String lastnFirstn[] = new String[10];
               
                for (int i = 0; i <= 10; ++i){
                        System.out.println("Please enter 10 lastname(s) followed by (period) firstname(s) [e.g. MaeTrix.Code]: ");
                        lastnFirstn[i] = input.next();
                        indentName = lastnFirstn[i].replace(".", " ").trim();
                        System.out.println("Please enter your mark: ");
                        mark[i] = input.nextInt();
                        if (mark[i] >= 50){
                                mark[i] = pass[i];
                                indentName = passName[i];
                        }
                        else if (mark[i] < 50){
                                mark[i] = fail[i];
                                indentName = failName[i];
                        }
                }
                System.out.println("Would you like to see all pass' or all fails: ");
                response = input.next();
                if (response == "pass"){
                        for (int i = 0; i <= mark.length; ++i){
                                System.out.print(passName[i]);
                                System.out.print(pass[i]);
                        }
                }
                else if (response == "fail"){
                        for (int i = 0; i <= mark.length; ++i){
                                System.out.print(failName[i]);
                                System.out.print(fail[i]);
                        }
                }
        }
}


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?"
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: 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.
QuantumPhysics




PostPosted: 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?
QuantumPhysics




PostPosted: 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.
DemonWasp




PostPosted: 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.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: