Support Forums
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.*;
import java.lang.*;
class sub
{
    public static void main(String[] args)
    {
        String a = "1";
        String b = "2";
        String c = "3";
        String d = "4";
        String e = "5";
        String f = "6";
        String g = "7";
        Scanner sa1 = new Scanner(System.in);
        System.out.println("1) Encrypt");
        System.out.println("2) Encrypt");
        System.out.print("What do you want to do?: ");
        int ia1 = sa1.nextInt();
        if (ia1 == 1)
        {
            Scanner sa2 = new Scanner(System.in);
            System.out.print("What do you want to encrypt?: ");
            String ia2 = sa2.next();
            int ia2len = ia2.length();
            int cero = -1;            
            for (int loop = 0; loop < ia2len; loop++)
            {
                int cero1 = cero + 1;
                int cero2 = cero + 2;
                if (ia2.substring(cero1,cero2) == "a")
                    System.out.print(a);
                else if (ia2.substring(cero1,cero2) == "b")
                    System.out.print(b);
                else if (ia2.substring(cero1,cero2) == "c")
                    System.out.print(c);
                else if (ia2.substring(cero1,cero2) == "d")
                    System.out.print(d);
                else if (ia2.substring(cero1,cero2) == "e")
                    System.out.print(e);
                else if (ia2.substring(cero1,cero2) == "f")
                    System.out.print(f);
                else
                    System.out.print("no");
                System.out.println();
        
            }
        }    
    }
            
}

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

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

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):
import os
import random
os.system("clear")
monsters = ['cyclopse', 'duck', 'dragon', 'wizard', 'magician']
randommonster = random.choice(monsters)
print "Class: %s" % classname
print "1) %s (More damage, but less armor)" % highdamageweaponname
print "2) %s (Less damage, but more armor)" % lowdamageweaponname
a = int(raw_input("You are battling a %s what weapon do you want?: " % randommonster))
while enemyhealth >= -1 and yourhealth >= -1:
hdmg = [0,5,0,15,0,20,30]
ldmg = [0,5,6,7,8,9,10]
ldmgrandom = random.choice(ldmg)
hdmgrandom = random.choice(hdmg)
edmg = [ldmgrandom,hdmgrandom]
edmgrandom = random.choice(edmg)
mdmg = [1,2,3]
mdmgrandom = random.choice(mdmg)
os.system("clear")
if a == 1:
print "you have chosen a %s" % highdamageweaponname
print "Your Armor: ", yourhealth
print "Enemy Armor: ", enemyhealth
print "1) %s (High damage | High chance of missing)" % hdmgname
print "2) %s (Low damage | Low chance of missing)" % ldmgname
c = int(raw_input("Choose your attack: "))
if c == 1:
ddmg = hdmgrandom
yourhealth = yourhealth - edmgrandom
enemyhealth= enemyhealth- ddmg
print "You Did ", ddmg, "Damage"
raw_input("Press Enter To Continue")
elif c == 2:
ddmg = hdmgrandom
yourhealth = yourhealth - edmgrandom
enemyhealth= enemyhealth- ddmg
print "You Did ", ddmg, "Damage"
raw_input("Press Enter To Continue")
if a == 2:
print "you have chosen a %s" % lowdamageweaponname
print "Your Armor: ", yourhealth + 20
print "Enemy Armor: ", enemyhealth
print "1) %s (High damage | High chance of missing)" % hdmgspellname
print "2) %s (Low damage | Low chance of missing)" % ldmgspellname
c = int(raw_input("Choose your attack: "))

if c == 1:
ddmg = hdmgrandom
yourhealth = yourhealth - edmgrandom
enemyhealth= enemyhealth- (ddmg - mdmgrandom)
print "You Did ", ddmg, "Damage"
raw_input("Press Enter To Continue")
elif c == 2:
ddmg = ldmgrandom
yourhealth = yourhealth - edmgrandom
enemyhealth= enemyhealth- (ddmg - mdmgrandom)
print "You Did ", ddmg, "Damage"
raw_input("Press Enter To Continue")
if c > 2 or c < 1:
print "Invalid attack"
raw_input("Press Enter To Continue")
if yourhealth <= 0:
os.system("clear")
print "You Died..."

elif enemyhealth<= 0:
os.system("clear")
print "You Killed Him!"
def menu():
import os
import random
os.system("clear")
print "############################################"
print " City of Warscape "
print "###########################################"
print " "
print "Choose Your Class"
print "1) Warrior"
print "2) Ranger"
print "3) Mage"
x = int(raw_input("What Class What You Like To Be?: "))
if x == 1:
a('Warrior','Mace','Sword','Pound','Bash',100,100,'Slice','Dice')
if x == 2:
a('Ranger','Bow','Knives','Steady Shot','Power Shot',100,100,'Toss','Hurl')
if x == 3:
a('Mage','Staff','Wand','Poke','Slash',100,100,'Fireball','Froststrike')
menu()
the tabs got screwed up, but you can see the variable names


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.