java!! help please!

Discussion in 'Computer Science & Culture' started by tQySmA, Nov 18, 2003.

Thread Status:
Not open for further replies.
  1. tQySmA Registered Senior Member

    Messages:
    36
    halu guys.. uhm, just wanna ask if u have a method that can read each letter in a string if so.. please post it here.. i just badly need it.. example : halu
    it can return. h - if i want to return this letter
    or a - if this is what i want...
    please help me.. tnx so much....
     
  2. Google AdSense Guest Advertisement



    to hide all adverts.
  3. Redrover Registered Senior Member

    Messages:
    234
    Isn't a string an array of characters? Doesn't each character have it's own adress?
     
  4. Google AdSense Guest Advertisement



    to hide all adverts.
  5. AntonK Technomage Registered Senior Member

    Messages:
    1,083
    In Java, a string is not simple an array of characters it is actually an object. To reach each character there is a method called charAt(int). You can call it as follows
    Code:
    char first = myString.charAt(0);
    char second = myString.charAt(1);
    etc.

    -AntonK
     
  6. Google AdSense Guest Advertisement



    to hide all adverts.
  7. tQySmA Registered Senior Member

    Messages:
    36
    so u mean.. if i input a value.. the first one has owedi the address of 0? or do i hav 2 set it??
     
  8. Dapthar Gone for Good. Registered Senior Member

    Messages:
    203
    You know, I used to ask a lot of brief questions like this myself, but then I started writing short, 5-10 line programs to test these sort of questions, and I find that it has helped me greatly in the long run.

    Just to get you started on this question, here's the outline of a program that will answer your query:

    Create a program with a string variable declared, like "Hello", and run Anton's suggested code on it, after that, just System.out.println the two variables (first and second) and see what you get.

    Alternatively, you could have answered your question by looking at the API documentation, located here: http://java.sun.com/j2se/1.4.2/docs/api/index.html, then click inside the bottom left window, scroll down to "String", and after clicking on that, scrolling down inside the rightmost frame until you see the description for the charAt(int index) method. It describes the "addressing system" of the method there, along with some other useful information.
     
  9. Stryder Keeper of "good" ideas. Valued Senior Member

    Messages:
    13,105
    Something I still use, but it might or might not be found in Java is the string.split() method in Javascript.

    What it does is allow a character (or characters) to be assigned to define when a string should be split and it's components placed into their own array. It's usually used when dealing with cookies, however I found it useful for storeing multiple arrays of information.

    (Note: I'm not saying this works with Java)

    A Javascript example would either of these, the long way first and the short way second.

    Code:
    
    #########--1st--###########
    var str=new Array()
    str[0]="";
    str[1]="blank.gif";
    str[2]="click%20to%20view";
    str[3]="180";
    str[4]="135";
    str[5]="TOP";
    str[6]="";
    ########################
    
    or with the .split()
    
    #########--2nd--###########
    var string="@blank.gif@click%20to%20view@180@135@TOP@";
    var str=string.split("@");
    ########################
    
    Also another ones that might cross over to Java are:
    string.indexOf() and string.lastIndexOf()
    These are both used for getting the location number of a character, indexOf() reads for left to right, while lastIndexOf() reads from right to left.

    if the string was the word "relativity"
    string.indexOf("a") would give you the number "3" as an answer
    while string.lastIndexOf("a") would give you the number "6"
     
    Last edited: Nov 18, 2003
  10. tQySmA Registered Senior Member

    Messages:
    36
    ok.. tnx guys.. so much.. i do have another inquiry.. what does this mean???
    Note: C:\BACKUP FROM d\CoLLeGE FiLes\3rd yr- 2nd sem\cs 241\prelims\projs\ConversionApplet.java uses or overrides a deprecated API.

    what do i have to do with this? should install a software or something?

    sori for being "makulit".. i just don't know java that much...
     
  11. testify Look, a puppy! Registered Senior Member

    Messages:
    508
    When you compile there should be a -depreciation flag in javac to set. It should work if your use that flag when compiling. Does it by any chance say what class is depreciated? I might be able to find a newer one that does the same thing so you don't have to keep using the depreciation flag all the time.
     
  12. tQySmA Registered Senior Member

    Messages:
    36
    o yeah.. just dscovered it a while ago.. ehehehe.. nwai, uhm.. i have another problem..uhm, my prgrom won't execute.. error: "Exception in thread "main" java.lang.NoClassDefFoundError: "

    what does this mean??
     
  13. AntonK Technomage Registered Senior Member

    Messages:
    1,083
    You didnt execute it properly. To execute a java program, say your java file is named myprog.java, you'll write
    Code:
    java myprog
    Thats it. Easy. Make sure you don't type
    Code:
    java myprog.class
    or
    Code:
    java prog.java
    , those won't work.

    -AntonK
     
  14. tQySmA Registered Senior Member

    Messages:
    36
    ah ok.. guys! can u do me a favor? can u debug my program.. coz i really don't know wats wrong with it.. nwai, f its ok... can u give me ur add coz i cant post it here.. it's so embarassing e...tnx n advance...
     
  15. Redrover Registered Senior Member

    Messages:
    234
    If you want us to debug your program, you should past it here so that the errors and, more importantly, the solutions can be shared with everybody.
     
  16. AntonK Technomage Registered Senior Member

    Messages:
    1,083
    No offense meant, but I refuse from now on to help or even respond to anyone that types like this. A few acronyms here and there are acceptable, but what you posted was barely even language. Please spell properly and use proper English grammar. No one here is going to tear you apart for a misspelled word, but seriously what you typed there was horrible.

    -AntonK
     
  17. Executor Registered Senior Member

    Messages:
    104
    i think the .split() method for javascript talked about above is similar to StringTokenizer() in java?
     
Thread Status:
Not open for further replies.

Share This Page