Help: Program not doing what it's supposed to - Printable Version +- Support Forums (https://www.supportforums.net) +-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87) +--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18) +---- Forum: Java Programming (https://www.supportforums.net/forumdisplay.php?fid=22) +---- Thread: Help: Program not doing what it's supposed to (/showthread.php?tid=3347) |
Help: Program not doing what it's supposed to - nevets04 - 12-05-2009 Code: import java.util.*; If I input 1, then I input a, it outputs "no" I want it to output "1" Does anyone see what is wrong? RE: Help: Program not doing what it's supposed to - wat - 12-09-2009 You've got to stop posting code like this... You create a new scanner object inside the if-block, why? Make your variable names descriptive. Every time you use a cryptic variable name, a baby dies. Learn what an array is. The problem is that you never change the value of 'cero', why the variable even exists, I have no idea. Remember, every undescriptive variable name leads to a dead baby.... RE: Help: Program not doing what it's supposed to - nevets04 - 12-09-2009 (12-09-2009, 03:08 PM)FarOut Wrote: You've got to stop posting code like this... When I posted this, people were telling me to shorten my variable names... Code: def a(classname,highdamageweaponname,lowdamageweaponname,ldmgname,hdmgname,yourhealth,enemyhealth,ldmgspellname,hdmgspellname): RE: Help: Program not doing what it's supposed to - 1337 - 12-09-2009 Hey ok so first off user FarOut is completely right you need no must use descriptive variable names. Otherwise even the best programmers will have no idea what your talking about and might give up trying to help you because you have inconvenienced them. To answer your question though.... You are comparing a String with == this operator can only be used on primitive data types such as booleans, doubles, ints. You need to remember even though in some instances Java treats String sort of like a primitive it is still an Object. So instead of if (ia2.substring(cero1,cero2) == "a") you should use the .equals() method of the Object superclass .equals() compares the state and vales of certain aspects of the string object. So when you say if (ia2.substring(cero1,cero2) == "a") that is really just saying if(45fa4bc45 == 783fd6709a) those two numbers(base 16 hexadecimal) are almost never going to be the same. Without going into to much detail those numbers have to do with where the object is stored in memory on the stack or RAM. thus using the .equals() method you can compare actual meaningful values so .. . if (ia2.substring(cero1,cero2).equals("a")) should return true assume all conditions are met. also be aware of .equalsIgnoreCase(String) might add some robustness to your program RE: Help: Program not doing what it's supposed to - MrD. - 12-10-2009 (12-09-2009, 10:34 PM)1337 Wrote: You are comparing a String with == this operator can only be used on primitive data types such as booleans, doubles, ints. Ergh, that caught me out when I first used Java. It was my first time meeting a high-level language that didn't have an overloaded equality/inequality operator on a string class to do a string compare (and I still find it quite stupid, why would you ever want to compare the memory address of a string literal? It will never match anything else). RE: Help: Program not doing what it's supposed to - 1337 - 12-10-2009 (12-10-2009, 03:45 AM)MrD. Wrote: Ergh, that caught me out when I first used Java. It was my first time meeting a high-level language that didn't have an overloaded equality/inequality operator on a string class to do a string compare (and I still find it quite stupid, why would you ever want to compare the memory address of a string literal? It will never match anything else). I would not consider Java as a high level language. It is more similar to C++ and other lower level languages than most people realise. The only reason some people consider it high level is because it has automated garbage collection and memory management (ie. no pointers and whatnot like C++) Most high level languages like php are String languages meaning they don't have variable types, instead they work on Strings. So that is why you can compare strings with == in high level (String languages) because they are using strings as the "primitives" of the language. Whereas Java which is a "high" low-level language. Uses true primitive data types such as doubles(2^64) and ints(2^32). Thus the language must treat anything else as an object. This seems annoying but it allows you to do more and be more creative. ie. low level languages. RE: Help: Program not doing what it's supposed to - MrD. - 12-10-2009 To be fair, C++ and C# was what I was comparing it to. So I really meant middle-level, not high-level. RE: Help: Program not doing what it's supposed to - wat - 12-10-2009 (12-10-2009, 03:45 AM)MrD. Wrote: Ergh, that caught me out when I first used Java. It was my first time meeting a high-level language that didn't have an overloaded equality/inequality operator on a string class to do a string compare (and I still find it quite stupid, why would you ever want to compare the memory address of a string literal? It will never match anything else).In java, strings are objects. So whenever you have a string literal (anything inside the "s), it's really pointing to that literal. Also, nevets, whoever told you to shorten your variable names is a retard. |