yet more questions...


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 14 of 14

Thread: yet more questions...

  1. #1
    Join Date
    Sep 2004
    Posts
    18

    yet more questions...

    sorry bout asking so many question but an assignments due so I need to try to figure out a lot of stuff in a small amount of time.

    well here goes.


    How can you make a record that is permanently saved so it stays stored even when the java program is exited.?

    I want to create a large selection of words (about 50)... one of these will be chosen at random...
    what would be the best way to go about this?

    the only solution I came up with was a switch but I'm sure there would be better ways

    and lastly, is there anyway for a string to be searched...?
    say search a string for a vowel or a certain letter?

  2. #2
    Join Date
    Mar 2003
    Posts
    834
    1. How can you make a record that is permanently saved so it stays stored even when the java program is exited.?

    There are different ways of doing this. You can: (a) serialize objects to disk (b) write data out to a regular text file (c) write data out to a database.

    Which of these you choose depends upon how complicated you want your solution to be.

    2. I want to create a large selection of words (about 50)... one of these will be chosen at random...
    what would be the best way to go about this?

    the only solution I came up with was a switch but I'm sure there would be better ways

    What's the problem? Generate a random number between 1 and 50 and then pick that String (the Strings could be stored in memory or in a file).

    3. and lastly, is there anyway for a string to be searched...? say search a string for a vowel or a certain letter?

    Sure. Check the .indexOf(...) method.
    ArchAngel.
    O:-)

  3. #3
    Join Date
    Sep 2004
    Posts
    18
    this may sound incredibly stupid but how do I get this working...

    Code:
           private String selectWord ()
           {
               int random = Random();
               
               String selectedWord = " ";
               
               String word1 = "ant";
               String word2 = "bet";
               String word3 = "pip";
               String word4 = "pot";
               String word5 = "cut";
               
               selectedWord = (word[random]);
               
               return selectedWord;
           }
    its telling me that word isn't a variable, so how do I get random onto the end of it so it is a variable?

    I've tried, but that didn't work.

    random is just a randomly generated number from 1-5.

  4. #4
    Join Date
    Mar 2003
    Posts
    834
    You need to use an array. At the moment, you've just declaring a whole load of String variables, rather than an array of Strings:
    Code:
      String[] word = new String[]{"ant", "bet", "pip"};
    ArchAngel.
    O:-)

  5. #5
    Join Date
    Sep 2004
    Posts
    18
    those....

    I've used an array twice and not even known it. See I use things but don't know what they're for.

    I'll try using that then, but I have found a way using switch so if I can get an array to work I will, if I can't I won't bother.


    well thanks so far... but how do you use indexOf...?

    I looked it up on the sun website and got really really confused...

  6. #6
    Join Date
    Mar 2003
    Posts
    834
    I assume you mean String's .indexOf(...) method. What's confusing? You pass in a String and it will return the index of where that String appears in your object, or -1 if it doesn't appear.
    ArchAngel.
    O:-)

  7. #7
    Join Date
    Sep 2004
    Posts
    18
    yeah String's indexOf method...

    I don't know how to use it...

    or what you include in the brackets for it...

    the whole thing confuses me...

  8. #8
    Join Date
    Mar 2003
    Posts
    834
    I think in that case it might be worth your while doing some study of Java first. Below is a link to a free on-line book called "Thinking In Java" - it's excellent.

    http://www.mindview.net/Books/TIJ/
    ArchAngel.
    O:-)

  9. #9
    Join Date
    Sep 2004
    Posts
    18
    well I've found a site that told me how to use IndexOf()..

    so I should be able to use it now. Although I will take a look at that book.

    I have a problem where if I see some code and how it works I can generally adapt it to what I want even If I have no idea how it really works or even what its called.

    See I don't even know what a method actually is, but I constantly use them... same with arrays, but I don't really actually know how they work, I still use them though.

    I need to get a better understanding of the terms pretty much.

    If you showed me some code and said this is how you do it... I can easily understand. But if you said you need to invoke a method or something... I would just be completely lost.

    So anyway in conclusion, thanks.

  10. #10
    Join Date
    Mar 2003
    Posts
    834

    Lightbulb

    Glad you worked it out.

    I really strongly suggest you use the book to at least cover the basics. Once you understand classes and methods you suddenly have the entire Java libraries written by Sun available to you! Until then, you arein danger of just staggering on, never really understanding the behaviour or the consequences of what you're writing.

    Happy reading!
    ArchAngel.
    O:-)

  11. #11
    Join Date
    Feb 2004
    Posts
    808
    Originally posted by ArchAngel
    2. I want to create a large selection of words (about 50)... one of these will be chosen at random...
    what would be the best way to go about this?

    the only solution I came up with was a switch but I'm sure there would be better ways
    selecting all the text on a web page, dumping it into a good text editor and find/replacing out all the \n and \r, then dump the resulting single-line text into a string variable.. then myString.split(" ")ting it when you star thte program and keeping the array returned, using it like:
    myArray[Math.random()*myArray.length]

    would possibly make things a lot easier for you
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  12. #12
    Join Date
    Feb 2004
    Posts
    808
    Originally posted by nothing
    I have a problem where if I see some code and how it works I can generally adapt it to what I want even If I have no idea how it really works or even what its called.
    but that's never the best teacher..

    See I don't even know what a method actually is, but I constantly use them... same with arrays, but I don't really actually know how they work, I still use them though.
    heres a quick run down:

    programs are like recipes. they are list of well defineed instructions
    programs are broken down into modules (in object orientation) or classes (as we call them in java)
    modules are broken down into methods (or functions as we call them in vb/c)
    a method is a list of instructions with a particular purpose. the name of the method gives some indication as to purpose

    now suppose we are reading a recipe and is says "beat eggs" - we know how to do that, we know to get a whisk, stick it in the eggs, move it with our arm.. so we can call this process beating eggs. its a method. no recipe book in the world will write "get whisk, stick in eggs, move arm" over and over again.. thye jsut write "beat eggs"
    so if you find yourself pasting the same thing over and over into a program, you can make a load of instructions into a method that will save time and effort, and also mean you only have to change one place if you want to make a change. so a method defines a common set of instructions, but what if we want to change the behaviour.. we use arguments.
    a recipe book might say "beat eggs ferociously, beat eggs to within an inch of their life, beat eggs for 5 minutes, beat eggs until light and fluffy, beat eggs until a smooth yellow is produced"
    the same process of beating eggs, but you have some additional argument that further refine how you will carry out the instructions

    methods are the same.. System.out.println() is a method. it ALWAYS prints something to the dos box. but WHAT it prints depends on what you supply as an argument.

    indexOf ALWAYS looks in a string, for the occurrence of some letter (or another string).. but you have to tell it what string to look for
    myName = "Matt"
    myName.indexOf("t");//returns 2 (M=0, a=1, t=2..)
    myName.indexOf("m");//returns -1 (not found, case sensitive you see)

    methods might have a return value as you see above.. if your mum tells you to beat eggs, youre going to tell her when it's done, or maybe if you failed to manage it

    just like this:

    int x = 2+2;
    2+2 is evaluated and spits out 4, which goes into x and is stored there, the same applies for methods that return a value:

    int positionOfT = myName.indexOf("t");
    myName.indexOf("t") spits out 2, so our line of code becomes I(like algebra in maths, concept of substitution)
    int positionOfT = 2;

    and lo, out positionOfT variable now holds 2

    we dont have to use the return value.. sometimes a method does something, and so long as it does it, we dont care for the return value so we dont have to catch it. in indexOf's case, this is pretty dumb not to, because it is the result we want. you may come across enumerations in the future.. they are used to pull results out of databases and things like that. in a typical enumaration, you call a method called nextElement() which moves a pointer on by one,a nd returns what it is pointing to
    suppsoe we had 5 elements A B C D E
    initially the pointer is positioned before A, so calling next() moves it to A and A is returned..

    but suppose we actually only wanted C.. we want to ignore A and B that get returned:
    next() //returns a
    next() //returns b
    Object theWantedElement = next() //returns C, and stores it

    in the first 2, we are solely using next() for its ability to move the pointer, in the third, we capture what it returns too.. so sometimes you might want to ignore a return for good reason (usually becaus eit is not of interest)


    -----------

    arrays, well, you know what a variable is. its aname you can use to refer toa value. arrays are no different really.. they are just a variable but the name has one important fact; it can be changed programmatically

    using your example:

    word1 = "hello"
    word2 = "world"
    word3 = "this"
    word4 = "rocks"

    these are all separate variables, and you CANT do this:

    for i = 1 to 4 do{
    print( word+i )
    }

    (the loop is written in pseudocode for easy reading; you dont write java loops like that)
    why? word resolves to.. well.. nothing
    and i resolves to a number.. but java doesnt resolve i to a number, then stick it on the end of word (e.g. word1) and then go off and look for word1

    instead, you need to define an array, which has a 2 part name:

    String[] word = new word[4];

    now, our array itself is called word, and each element within that array is referred to by word[a number]
    you cant change the WORD part of the name programmatically.. but you can change the number part of the name
    word[0], word[1], word[2]

    making our loop like this:


    for i = 1 to 4 do{
    print( word[i] )
    }

    java WILL resolve i to a number, and stick it in the square brackets.. and then look up which string applies.

    it often helps to think of arrays as a load of buckets tied together. they have a common name, e.g. "theBuckets" "theWords" and to get the contents of a particular bucket you say the name of the whole lot, followed by the number of the one you want.. like an address, you say the name of the whole street, plus the number of the house you want

    each position in our array refers to a thing in memory somewhere.. things in memory usually have names, like:

    String word1 = "banana"
    word1 is the name

    with arrays the name of the array, plus the index, is actually the whole name of a value:

    words[27] = "banana"
    words[27] is the name of the variable holding banana
    words is the name of the array..


    theres different ways of looking at these things, but i find it helps to consider arrayName[index] as jsut the complete name of some variable.. with the knowledge that i can modify that name programmatically, just like i would add a number to a string:

    print(" the number is " + 17372)
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  13. #13
    Join Date
    Sep 2004
    Posts
    18
    thanks alot for your help, I'm actually getting it now. Just one more annoying question, that should be easier to explain to me now.

    For a program in the assignment I'm comparing a char variable with a string from a textfield. It says they are incompatible. For numbers from text fields, I've just parsed it (I don't really know how parsing works though, I just adapted other code again.) But I'm having trouble with dealing with a character. At the moment you can enter anything in the text field but I'm going to make it so that only one character can be entered.

  14. #14
    Join Date
    Feb 2004
    Posts
    541
    Well you can't compare them for precisely the reason it says, they are incompatible. A string is an object much like any other, a char is a primitive type variable (like an int or a boolean). What you need to do is convert the string to a char array (the String class has a method for doing this), then you can access the individual chars in the string.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center