Dolphin, the GameCube and Wii emulator - Forums
Programming Discussion Thread - Printable Version

+- Dolphin, the GameCube and Wii emulator - Forums (https://forums.dolphin-emu.org)
+-- Forum: Offtopic (https://forums.dolphin-emu.org/Forum-offtopic)
+--- Forum: Delfino Plaza (https://forums.dolphin-emu.org/Forum-delfino-plaza)
+--- Thread: Programming Discussion Thread (/Thread-programming-discussion-thread)



RE: Programming Discussion Thread - Shonumi - 11-12-2012

Have a look at this mostly psuedo-code. Similar to what NV suggested, but you don't check each character 52 times :p Look up the documentation to hash maps and look at some examples if you're still confused. This one is pretty cut and dry.

Code:
Mapz = new HashMap();


For loop to iterate over ever character you want to check - Grab one at a time (let's call it Character)

    If Character is a letter (use isLetter()) AND Character already exists in the HashMap (use containsKey()) - Then
        Some Integer = Mapz(Character); //Grab current value from hashmap
        Some Integer++;
        Mapz.put(Character, Some Integer); //Replace old value from hashmap with updated value

    Else If Character is a letter AND Character does NOT already exist in Hashmap - Then
        Mapz.put(Character, new Integer(1)); //Add new entry into hashmap, value of 1 since it's first time we encountered the letter

End For loop

Write HashMap to CSV file

Note, this will include upper and lower cases. If you wanted just characters in general, just convert all the characters to one case e.g. using toLower() or toUpper() before checking the values in the hashmap.

EDIT: Got ninja'd while posting. Basically it's like shuffle said, but it's got a bit of Java for you. Haven't programmed in that language for years, but you'll get the idea.


RE: Programming Discussion Thread - AnyOldName3 - 11-12-2012

That's not ideal for me to do, as the text may have lowercase characters which I need to skip (It's to help me decrypt ceaser shift and affine shift text, and the convention is that plaintext is in lowercase. There may be a situation where I lose my frequency table having already solved several letters, so won't want frequencies of these letters, as this could get confusing.)


RE: Programming Discussion Thread - Shonumi - 11-12-2012

(11-12-2012, 11:44 AM)AnyOldName3 Wrote: That's not ideal for me to do, as the text may have lowercase characters which I need to skip (It's to help me decrypt ceaser shift and affine shift text, and the convention is that plaintext is in lowercase. There may be a situation where I lose my frequency table having already solved several letters, so won't want frequencies of these letters, as this could get confusing.)

So you want to skip lowercases altogether? Just change the two conditional statements:

Code:
If Character is a letter (use isLetter()) AND Character already exists in the HashMap (use containsKey()) AND the character is Uppercase (use isUpper()) - Then
        ...
        ...

    Else If Character is a letter AND Character does NOT already exist in Hashmap AND the character is Uppercase (use isUpper()) - Then
        ...
        ...

Or do you mean you need to skip specific lower case characters? For that, just check that the characters aren't the ones you don't want. The conditional statement could get quite long, nothing wrong with that though (unless it's a ridiculous amount...)

Code:
If Character is a letter (use isLetter()) AND Character already exists in the HashMap (use containsKey()) AND the character is not blacklisted (e.g. != "a" or != "b")
        ...
        ...

    Else If Character is a letter AND Character does NOT already exist in Hashmap AND the character is Uppercase (use isUpper()) AND the character is not blacklisted (e.g. != "a" or != "b") - Then
        ...
        ...

You mentioned having trouble grabbing just one character from a string to do comparisons, right? Use the charAt() function:

Code:
String many_letterz = "words are words";

if(many_letterz.charAt(0) == "w") { System.out.println("Yeah, 1st character is w"); }
else { System.out.println("Nope, not w"); }



RE: Programming Discussion Thread - AnyOldName3 - 11-12-2012

That'll probably be enough for me to get something working, thanks. I'll try and write it tomorrow.


RE: Programming Discussion Thread - shuffle2 - 11-12-2012

http://stackoverflow.com/questions/4363665/hashmap-implementation-to-count-the-occurences-of-each-character

I came up with this: http://ideone.com/edaq3X
(Note that I haven't used Java for a long time, and I hate it's guts...this exercise reminded me just how much it sucks)
Code:
import java.util.*;
import java.lang.*;

class Main
{
    public static void main (String[] args) throws java.lang.Exception
    {
        Map hitcounts = new HashMap();
        String uppercase_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        
        for (char uppercase_char : uppercase_alphabet.toCharArray())
        {
            hitcounts.put(uppercase_char, 0);
        }
        
        String input_string = "Make This WhatEveR input You Want";
        
        for (char character : input_string.toCharArray())
        {
            if (hitcounts.containsKey(character))
            {
                hitcounts.put(character, (int)hitcounts.get(character) + 1);
            }
        }
        
        System.out.println(hitcounts);
    }
}

You don't need to initialize a hashmap, but in this case it doesn't really matter.

Edit: Here is a version that doesn't initialize the hashmap: http://ideone.com/k88VPa
Code:
import java.util.*;
import java.lang.*;

class Main
{
    public static void main (String[] args) throws java.lang.Exception
    {
        Map hitcounts = new HashMap();
        String input_string = "Make This WhatEveR input You Want";
        
        for (char character : input_string.toCharArray())
        {
            if (Character.isUpperCase(character))
            {
                int hitcount = 0;
                if (hitcounts.containsKey(character))
                {
                    hitcount = (int)hitcounts.get(character);
                }
                
                hitcounts.put(character, hitcount + 1);
            }
        }
        
        System.out.println(hitcounts);
    }
}

Note that this time the hashmap will not contain keys with a value of 0. You would need to account for this when accessing the hashmap, or insert the keys explicitly.


RE: Programming Discussion Thread - AnyOldName3 - 11-15-2012

Two things:

a) Now I've made the code into a single method in a larger program, I don't know where to put the
Code:
java.lang.Exception
.

b) I can't work out exactly how to use .get() to make the value of a string (yes it must me a string, at least eventually) the value of a data item under a specific key. Basically, it won't compile when I use:

Code:
    public void formatFrequencies()
    {
        //Formats the data in the HashMap into a .csv file Excell can open.
        frequencyTable = frequencyTable+newLine;
        String currentFrequency = "0";
                for(char currentLetter : alphabet.toCharArray())
        {
            currentFrequency = frequencyMap.get(currentLetter);
            system.out.println(currentFrequency);
        }
    }


This code isn't quite complete yet, but I don't want to add more until I can get this bit working.


RE: Programming Discussion Thread - AnyOldName3 - 11-15-2012

I've changed system to System, and got rid of an error from that. Now Javac only lists one error.

Added a ""+ and got rid of the error. Let's see if it runs.


RE: Programming Discussion Thread - AnyOldName3 - 11-15-2012

i THINK IT MAY BE FINISHED!!!


RE: Programming Discussion Thread - ExtremeDude2 - 11-15-2012

I sense caps lock :3


RE: Programming Discussion Thread - Zee530 - 11-16-2012

Whats the best language to use if i have a set of data that i need to be entered into a program and saved to a database?