+ Reply to thread
Results 1 to 5 of 5

Thread: Any Java programmers out there? Need help...

  1. #1
    Member
    Registered
    Mar 2009
    Location
    London
    Posts
    30

    Default Any Java programmers out there? Need help...

    ...for my coursework which is due in tomorrow! I have a variable scope problem which I'm not sure how to resolve.

    Okay I have code that looks like this (simplified, assume it's within a 'main' function, which is inside a class with the same name as the file)

    Code:
    if (args.length != 2) { 
    	System.out.println("bugger off");
    	return;
    }
    else {
    	String mode = args[0];
    	String sequence = args[1]; // for clearer variable names
    }
    
    if (mode="herring") {
           doSomethingUsefulWith(sequence);
    }
    Okay so it tells me it can't resolve 'mode' or 'sequence'. I think I understand why... I declared them within the else-clause, right?
    So I suppose I could fix this by declaring them (but not setting values) at the top, before the argument checking bit, then within the else-clause I just set their values?
    Well, fair enough, though I dislike the idea that I have to declare variables at the top that, depending on the argument checking bit, might not get used (indeed NOTHING further will happen unless there are just the two arguments that are required). Any alternative? Can I do it my way but stick some smart keyword in when 'mode' and 'sequence' are defined, some word that means 'KEEP MEEEE!'?

    My second problem is similar but can't be resolved the same way, how can I resolve it at all? I have a HashMap. The keys are chars, but depending on what the first argument to the program is, the values need to be either integers or strings. So after the argument-number-checking bit, the code looks like this... (the game's up, it's a bioinformatics course)

    Code:
    String aaSet = "ACDEFGHIKLMNOPQRSTUVWY";
    if (mode.equals("-count")) {
    	HashMap<Character, Integer> residues = new HashMap<Character, Integer>(22);
    	for (int i=0; i<22; i++) {
    		residues.put(aaSet.charAt(i), 0);
    	}
    }
    else {
    	HashMap<Character, String> residues = new HashMap<Character, String>(22);
    	String aaCodes = "AlaCysAspGluPheGlyHisIleLysLeuMetAsnPylProGlnArgSerThrSecValTrpTyr";
    	for (int i=0; i<22; i++) {
    		residues.put(aaSet.charAt(i), aaCodes.substring(i*3,(i+1)*3));
    	}
    }
    
    // now run through each letter of the input sequence
    for (int i=0; i<sequence.length(); i++) {
    	char res = sequence.charAt(i);
    	if (residues.containsKey(res)) {
    		// oh no you naughty boy, I don't know what the variable 'residues' means because I r dumb.
    	}
    }
    As far as I can see I can't declare the residues HashMap outside the if-else blocks as I don't yet know what type the values in the hash will be.

    Haalp.

  2. #2
    Elephant CRSP's avatar
    Registered
    Feb 2009
    Location
    Perfidious Albion
    Posts
    936

    Default Re: Any Java programmers out there? Need help...

    In the first one, you don't need the "else". The code is executed, no matter what, if the first test fails. You could also move the second "if" within the scope of the second "else".

    The second problem is harder, and the easiest method is just to duplicate the "for" loop, once inside the "if", the other inside the "else".
    Les sanglots longs des violons de l'automne blessent mon coeur
    D'une langueur Monotone

  3. #3
    Member
    Registered
    Mar 2009
    Location
    London
    Posts
    30

    Default Re: Any Java programmers out there? Need help...

    Quote Originally posted by CRSP
    In the first one, you don't need the "else". The code is executed, no matter what, if the first test fails.
    Why yes, how dumb not to have realised that. I'm usually much smarter than that when I'm writing programs for my hobby-du-minute. Funny how my brain melts when there's the prospect of being marked on my work. Yeurgh.

    The second problem is harder, and the easiest method is just to duplicate the "for" loop, once inside the "if", the other inside the "else".
    The program has to do one of two different tasks depending on that 'mode', so my first thought was indeed to have an 'if' at the start, and essentially whack two different programs inside and after the if. But though the tasks are different, they both involve looping a character at a time through the supplied sequence, and using a hash-table based on the character being read, so it struck me as a bit wasteful. On the other hand, neither task is particularly big. I suppose I'll do it that way then.

    Thanks for the suggestions!

  4. #4
    Elephant CRSP's avatar
    Registered
    Feb 2009
    Location
    Perfidious Albion
    Posts
    936

    Default Re: Any Java programmers out there? Need help...

    There is a more elegant way for the second problem. It's to create a new generic class, taking the varying type of the Hashtable as a type parameter, and to put all your Hashtable code into a method of that class. It's probably not worth it, though, for a small piece of coursework.
    Les sanglots longs des violons de l'automne blessent mon coeur
    D'une langueur Monotone

  5. #5
    Member
    Registered
    Mar 2009
    Location
    Central WA
    Posts
    18

    Default Re: Any Java programmers out there? Need help...

    Quote Originally posted by Nancarrow
    My second problem is similar but can't be resolved the same way, how can I resolve it at all? I have a HashMap. The keys are chars, but depending on what the first argument to the program is, the values need to be either integers or strings.
    You could also make use of pre-1.5 technique by making it a map of Object values (or, don't even declare the key/value types; just the "@SuppressWarning" or whatever the appropriate tag is to quiet the compiler). Depending on what you're going to do with them, it might not matter (e.g., if simply printing to System.out or somesuch, you might use nothing more than Object.toString()).

    Another aspect of this: I have to say that I'm not sure an int will auto-box directly to an Object (without an Integer along the way), though I think it likely.

+ Reply to thread

Posting rules

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts