tux4life 2,072 Postaholic

The first problem is, I can't get the read values in the paint method in my appleTree subclass; I tried printing it out and whatever I do it says all values are 0s and "null"s.

Trees trees = new Trees();
int t = trees.getX();

This code is taken from your previous post, take a close look at it.
Where do you set values for the fields of your Tree?
Not in your constructor, and you don't call any setter, so it gets initialized to a default value, which is null in case of reference types, and 0 in case of integral types.

I'm not sure if this is what you mean but t also gave "0" back when i tried printing it out.

You have to call getX() and getY() in the paint() method of the Tree object that you initialized with the data from your file.

I can use appleTree.paint(g); but that only draws out one tree, I guess I should call that paint() method on every appleTree

Yes.

I just don't see how and where.

In your class that reads the file you can write a method that returns a Collection of Tree objects. In your GUI code you call that method, and a Collection<Tree> is returned.
Iterate over that collection and call paint() for every Tree.

BTW, throughout my post I refer to your Trees as Tree. Why exactly did you call it that …

tux4life 2,072 Postaholic

how do I draw that exact apple tree i specified in my txt file?

What exactly did you do in your AppleTree's paint method?

Should I put them in a Map here?

Nope.

public String color;
public int ID;

Change to private and make getter methods for these. In your subclasses' paint method you can call the getters to get the info you need to draw your tree.

tux4life 2,072 Postaholic

I understand your concern about sharing the same currentRoom. But when you think about it... it doesn't make alot of sense to keep a reference to the currentRoom in both Game and Player.

I'd like to point out some more things though:

  1. Players know their location, so they have a field that references the currentRoom they are in. This is now the responsability of a Player. So it does not make alot sense anymore to keep currentRoom in the Game class. (Think about extensibility: multiple Players in the same Game!)
  2. Players know their Inventory, it is their responsability. Not the responsability of the Game class. So, you can take this field out of the Game class as well.
  3. Similarly Players know where they have been, so the roomsVisited field belongs in the Player class.
  4. What's the purpose of the nextRoom field? From a quick look at the code I would think that this one better had been a local variable in a method.
  5. Once you got your undo stack roomsVisited, do you really need to keep a reference to previousRoom? Won't it be the top stack element of roomsVisited?
  6. You use class Stack, this is a legacy datastructure in Java, and it extends from Vector (another legacy datastructure - still used in Swing however), my personal recommendation would be not to use it. Prefer something more recent like ArrayDeque.

    On an interesting note:

    • "This class is likely to be faster than Stack when used …

tux4life 2,072 Postaholic

Take a look at your Character class:

public class Character
{
    private Room currentRoom;

    ...

    // the currentRoom is passed in via the Constructor
    public Character(Room room, String name)
    {
        this.name = name;
        currentRoom = room; // here currentRoom is set
        Room currentRoom = new Room(); // no need for this line, remove it
    }

    ...
}

Now take a look at your Player class:

public class Player extends Character // inheritance!
{
    private Inventory playerInventory;
    private Room currentRoom;

    public Player(Room room, String name, Inventory playerInventory)
    {
        super(room, name);
        playerInventory = this.playerInventory;
    }

    ...
}

Now you've got two options:

  1. Remove the currentRoom field from your Player class and create a protected getter method for the currentRoom field in the Character class,
    and call it from Player everytime you need the currentRoom.
    (You lose some encapsulation here though)

  2. Add the line this.currentRoom = room; to the Player constructor.

Also notice that the following line in your Player constructor won't have the intented effect: playerInventory = this.playerInventory;
It should be: this.playerInventory = playerInventory; instead.
The explanation goes as follows: whenever you have a parameter variable in your method whose identifier (= name) is the same as the identifier (= name) of one of the attributes in your class, <identifier> will refer to the method parameter, and this.<identifier> will refer to the instance variable of your class. In other words, you are assigning the value of your instance variable to the method parameter, …

tux4life 2,072 Postaholic
currentRoom = room;
Room currentRoom = new Room(); // here is the problem

Why creating a new room there, can't you just use the one passed to the constructor?

Also, could you clarify the following, not sure if I understand it:

My problem is that Character is an Abstract class, so the player and (enemy or whatever) is gonna use the current room
plus, I will have the same problem for all the other classes, and it would seem like a bad programming practice to have
two methods for every task I want (one in Game, and the other in Character).
i ran the program without initializing Room in character, and when i typed command look, this is the code from Player

To get a better idea it might be helpful if you could also post your other code.

tux4life 2,072 Postaholic

couldnt figure out why the output line always appears twice! like:
you have added computer to your inventory
you have added computer to your inventory

My first guess would be the following code from takeItem():

     playerInventory.addItemToInventory(itemToBePicked); // line 16
     if (playerInventory.addItemToInventory(itemToBePicked)) // line 17

Notice that addItemToInventory() is called twice in a row here.

tux4life 2,072 Postaholic

@fallen21468:
In addition for when you get more experience with Java, you might want to keep this link bookmarked: http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice

tux4life 2,072 Postaholic

Yes I'm able to see tables too...but still this exception appear after running project.

Hmm, that's odd, because I managed to get it to work (although I had to change the persistence provider).
Have you tried (re)generating the persistence.xml file using the NetBeans wizard?

tux4life 2,072 Postaholic

Have you tried connecting to your database using the Netbeans Services tab? This should work before you proceed.

tux4life 2,072 Postaholic

Hello ... I think all of these are O(n)

That's not true, take a look at (B) and (D), where the index is divided by / multiplied by two. These are O(log(n)).

This is O(n):

int index = 1;
while (index < n) 
{
    ...
    index++ // index is incremented
}

This is O(log(n)):

int index = 1;
while (index < n) 
{
    ...
    index *= 2; // index is doubled
}
tux4life 2,072 Postaholic

This might be of interest too: http://joda-time.sourceforge.net/ .

tux4life 2,072 Postaholic

while(plaintext.length() > 0) Strings are immutable, and the reference is never set to anything else. This has the potential to cause an infinite loop. The reason why it doesn't at runtime is because you keep incrementing counter, and accessing characters from that String by invoking plaintext.charAt(counter), eventually it will lead to a java.lang.StringIndexOutOfBoundsException, and since it is never caught, will terminate your program right away.

Another thing you should pay attention to is the scope in which you define your variables, take a look at these lines taken from the code you posted:

while (plaintext.length() > 0)
{
    ...
    ArrayList mylist = new ArrayList();
    mylist.add(text);
    ...
}

There are several things wrong about this, first off the scope: each loop iteration you'll create a new ArrayList instance, and it will only have one element in it. Next iteration the reference to the list goes out of scope, and the ArrayList you instantiated becomes eligible for Garbage Collection. The obvious fix is to create one ArrayList instance and keep adding characters to that list. You achieve this by moving your ArrayList mylist = new ArrayList();-line out of the loop. Like this:

ArrayList<Character> mylist = new ArrayList<Character>();
while (plaintext.length() > 0)
{
    ...
    mylist.add(text);
    ...
}

Also notice that I've added the type argument Character between angle brackets. If you don't, your compiler will generate a warning. However, you shouldn't ignore warnings!
The difference between ArrayList mylist = new ArrayList(); and ArrayList<Character> mylist = new …

tux4life 2,072 Postaholic

I'm not sure why you see put as a problem, but if you want a mutable integer value there's no need to write a class. You could use an AtomicInteger, or just use a HashMap<String, int[]> with a 1-element int array as the value and keep incrementing its zero'th element

I pointed that out because it is possible to avoid the overhead of put() each time a count needs to be updated (except for the first time that a word is counted).
For the remainder I agree with the rest of your post though.

tux4life 2,072 Postaholic

Compiler output:

FriedmanRPSgame.java:3: error: package hsa does not exist
import hsa.Console;
          ^
FriedmanRPSgame.java:7: error: cannot find symbol
    static Console c;           // The output console
           ^
  symbol:   class Console
  location: class FriedmanRPSgame
FriedmanRPSgame.java:11: error: cannot find symbol
 c = new Console ();
         ^
  symbol:   class Console
  location: class FriedmanRPSgame
FriedmanRPSgame.java:21: error: possible loss of precision
  int computer = Math.floor((Math.random()*3)+1);
                           ^
  required: int
  found:    double
FriedmanRPSgame.java:24: error: cannot find symbol
 if (Computer == 1 && choice == 2 ||Computer == 2 && choice == 3 || computer ==
3 && choice == 1)
     ^
  symbol:   variable Computer
  location: class FriedmanRPSgame
FriedmanRPSgame.java:24: error: cannot find symbol
 if (Computer == 1 && choice == 2 ||Computer == 2 && choice == 3 || computer ==
3 && choice == 1)
                                    ^
  symbol:   variable Computer
  location: class FriedmanRPSgame
6 errors

In addition to what has been said:

  • Math.floor() returns a double, and you are assigning it to a primitive of type int, what you want is a cast.
  • hsa.Console? From the context I assume that this class is used for outputting things. I'm just wondering... What is the great benefit of using this class as opposed to using something that comes with the Java libraries? I suggest you to take a look here. You can access an instance of PrintStream using the static out reference variable of the System class. E.g. System.out.println("You win!!!").
  • Next time it would be great if you could point out the issue that …
tux4life 2,072 Postaholic

The HashMap is a good suggestion, although I want to add something to it.
Personally I wouldn't recommend putting an Integer in it, since Integer is immutable, and you'll have to issue a put() everytime you want to update the count.
I suggest you create a Counter class that wraps around an int field and provides an increment() method. Something like this:

public class Counter
{
    private int count;

    public Counter()
    {
        this(0);
    }

    public Counter(int seed)
    {
        this.count = seed;
    }

    public void reset()
    {
        this.count = 0;
    }

    public void increment()
    {
        this.count++;
    }

    public int getCount()
    {
        return this.count;
    }
}

Everytime you fetch a word do the following:

Counter c = countsByWords.get(word);
if (c == null)
{
    // First time we encounter this word, create a counter for it
    // and put it in the Map.
    countsByWords.put(word, new Counter(1));
}
else
    c.increment(); // increment the count for this word

EDIT #0: Can anyone please link me to the post formatting stuff? I've been away for quite some time, and pretty much alot of things seem to have changed. I'd like some decent code tag formatting for the language I intend the code example for, and somehow the editor assumes I want non-decent code formatting.

EDIT #1: Just refreshed the page, and code highlighting is in place... Is the code highlighting handled automatically now?

tux4life 2,072 Postaholic

Ever heard of "Google" the mighty search machine?
Let me introduce you to what you can find with it: http://stackoverflow.com/questions/142016/c-c-structure-offset

tux4life 2,072 Postaholic

i am read java how to program
are you know other good books .?
>

Read the ``Starting Java' sticky thread, it contains plenty of useful resources for every programmer learning or using Java.

tux4life 2,072 Postaholic

Add the following line: private static final Random randomNumbers = new Random(); under:
// create random number generator for use in method rollDice int die2 = 1 + randomNumbers.next(6); must change to: int die2 = 1 + randomNumbers.next[B]Int[/B](6); Then run using the following class:

public class CrapsTest 
{
   public static void main( String[] args )
   {
      Craps game = new Craps();
      game.play(); // play one game of craps
   } // end main
} // end class CrapsTest

BTW, instead of manually keying in all the code examples from your book, you could as well download them from the website accompanying your book. This code is guaranteed to run. And if it doesn't, then you know that there's something wrong with your configuration.
Here's the download link for your book's code examples:
http://media.pearsoncmg.com/ph/esm/deitel/javahtp_8/code_examples_8e.html

tux4life 2,072 Postaholic

ups the brackets again "[" code "]" here your code "[" /code "]" like i said, the brackets without quotations :)

You can use the noparse tag( [noparse][noparse][/noparse][/noparse] ) to make it more clear.

Writing:
[noparse]

[code]

// Your code here

[/code] [/noparse]

Will yield:

[code]

// Your code here

[/code]

tux4life 2,072 Postaholic

In case you're still encountering problems with it, after applying the suggestions already given, then post the smallest piece of code that is able to reproduce your problem.

jon.kiparsky commented: Thanks for "smallest piece of code"... +1
tux4life 2,072 Postaholic

>>Which yes, it works, but I was wondering (First chapter of the book), is there any way that you think they meant to do it?

Since it is the first chapter of the book, I think they mean to do it as follows:

public class JavaPattern
{
  public static void main(String[] args)
  {
    /* Print newline before displaying the pattern. */
    System.out.println();
    
    /* Print the pattern. */
    System.out.println("    J    A     V     V    A");
    System.out.println("    J   A A     V   V    A A");
    System.out.println("J   J  AAAAA     V V    AAAAA");
    System.out.println(" JJ   A     A     V    A     A");
    
    /* Print newline after displaying the pattern. */
    System.out.println();
  }
}
tux4life 2,072 Postaholic

>>share me a lick please.. so i can do it.

Good thing that you wrote this post, because I'm going to refer to it anytime you do a request that involves doing it for you. So now, if you could provide us with some evidence that shows your effort, you've got a great chance of getting help. However if you don't, and continue ignoring replies, then other people at this forum will interpret it as follows: 1) You don't want to do any effort. 2) You don't want to be helped.

tux4life 2,072 Postaholic

Are you sure that you typed it in as import java.util.[B][U]S[/U][/B]canner; ? ('Scanner' starts with a capital letter).

tux4life 2,072 Postaholic
do {
      System.out.println("Enter number of years for the loan an in integer:  ");
      numberOfYears = input.nextDouble();
  
      System.out.println("Enter the loan amount :  ");
      loanAmount = input.nextDouble();
      } while (loanAmount <= 0 || numberOfYears <= 0) ;

I do not find it logical to get two user inputs in this context (by 'context' I mean: in the same loop, right after each other). Assume the user gives one correct input, and one wrong input. Then the following will happen: Both values will be discarded, and the user will be requested to re-enter values for both input requests. Personally I don't find it very user-friendly if a correct input gets discarded, just because another input request (from inside the same loop) got some invalid user-input. I suggest that each input request be put in its own loop. Different looping alternatives have already been suggested. Or even better: create input routines, one for reading the number of years, and one for reading the loan amount. Both routines should be written in such a way that they only return on valid input.

tux4life 2,072 Postaholic

>>That printed the correct total? That's impossible unless you called the price method.
True.

>>Or am I missing something here?
Unless I'm missing it too I can confirm that you're not missing anything.

tux4life 2,072 Postaholic

>>i found it from a book.
>>is it cheating to learn from any body who knows well?

I don't believe that code comes from a book. If that person ``who knows well' writes such code, full with magic numbers, and no comments at all - just to give a few examples of bad practice - and then publishes this rubbish, then I wouldn't dare to say that this person ``knows well'.

tux4life 2,072 Postaholic

>>I have no idea how you have put up with him this far javaAddict. He clearly does not want to learn and clearly did not write that code himself.

That's the proof that he's worth his Featured Poster-badge :)

tux4life 2,072 Postaholic

>>can u help me, if u can't use "if" statement in a sequential structure? what do we use? . . .

I didn't quite get that, can you illustrate with a concrete code example? (illustrate with the code you may not use, and keep it as short as possible)

>>can u giv me a download link 4 jdk1.6.0_20, tnx . . .

What?! You're to write a Java application and the JDK even isn't on your system?
Anyhow, here's a link: http://www.oracle.com/technetwork/java/javase/downloads/index.html
(click on the 'Download JDK'-button if you want the JDK)

tux4life 2,072 Postaholic

>>. . . kinda find the answer in the internet, but anyway, thank u 4 the help . . . can u giv me an example of a source code with a sequential structure, i'm kinda confuse . . .

No, I'm not going to do it for you, I believe that I've already helped you enough by providing you pseudocode for an algorithm that will do the job. Perhaps you should actually go re-reading Peter's post instead of ignoring the link he posted. (But this time read it with the -GimmeThaCodez flag turned off.) BTW, if my suggestion is ``kinda the answer in the internet', then why did you come here in first place? I'm sure you'll be able to ``kinda find' some ready bake code you can cheat with. Just keep in mind that - so far - you've already spend more time on avoiding your assignment, than by just in fact using the grey thing in your head to solve it.

tux4life 2,072 Postaholic

Here's an approach that works for integers of any length. (By length of an integer I mean: the amount of digits that integer consists of).

Pseudocode for the algorithm

While integer > 0:
  Extract last digit from integer**
  Add extracted digit to Sum
  Divide integer by 10

**Hint: Use modulo 10 (i.e. % 10).

Eventually you can maintain a counter variable that counts how many digits there are in the integer, in case you want to put a limit on the integer length.

tux4life 2,072 Postaholic

>>what if, i have many input characters??

You want to compare two arrays? Check this:

// In this code example I assume that firstArray and secondArray are primitive array
// reference variables, and that and import java.util.Arrays; has been done.
if (Arrays.equals(firstArray, secondArray))
  System.out.println("The arrays are equal.");
else
  System.out.println("The arrays are not equal.");

However, if you're not allowed to use java.util.Arrays class, then you'll have to manually loop through both arrays as suggested by javaAddict.

tux4life 2,072 Postaholic

@neworder:
Could you please post your whole program (that is everything you need to compile it and let it run). I prefer something that I can compile-test-debug-and-play-with.

tux4life 2,072 Postaholic

@NormR1:
>>how do you use an assertion to test if an object is null and change the code path?
After looking a bit deeper in the problem I understand why you're replying this. I was actually a little too fast in replying, and suggested something with limited usefulness (in this context). It's better to just stick with the if as you suggested.
>>What about when a read returns a null? How would you use an assertion with that?
Well, technically it is possible, but it is perhaps a little tedious and a bad practice.

tux4life 2,072 Postaholic

The basic way to prevent NPE is to make sure all the object references you use are NOT null. Either assign them all some non-null value or if there is a possibility that a reference could be null, test if it is null: if(objRef != null) before using it.

Personally I would prefer using assertions for that purpose.

tux4life 2,072 Postaholic

>>Hmm, What exactly is that testing though? Is it testing if the arrays are equal in size? contents are equal? or if they are the same type of array? I've never seen this before

From the Java API - Arrays.equals(char[] a, char[] a2):

``Returns true if the two specified arrays of chars are equal to one another. Two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal. In other words, two arrays are equal if they contain the same elements in the same order. Also, two array references are considered equal if both are null.'

tux4life 2,072 Postaholic

The suggestion you said to me makes my program more complicated....why don't you just help me how to solve it by editing my code...

I don't follow your reasoning. If he edits your code, by applying his suggestion(s) to it, then your code will be the same in complexity as if you'd done the edit yourself. At Daniweb we provide paths to the solution, but it's still your job to make a solution out of the hints given to you.

tux4life 2,072 Postaholic

Just to make sure you know this: that approach doesn't compare the whole array against another. It just compares two first elements of two arrays.

tux4life 2,072 Postaholic

>>Hmm, actually it's not working but it's taking shape.
>>The algorithm to determine BMI isn't functioning properly when data is entered into the program.

Well, that's probably because of the integer division that I talked about in this post. However, no worries, if you read this quote, then you'll be able to fix that:

[NOTE: this only applies if variables weight and height are of type int]

In this line: double BMI = weight/(height*height); you're effectively assigning an integer value to a variable of type double, this is because an integer division occurs. The result of an integer division is simply: another integer. Probably this is not your intent, and you might want to put in a cast to double to prevent an integer division from occuring, doing a floating point division instead. You can do this by changing that line to: double BMI = [B](double)[/B]weight/(height*height);

tux4life 2,072 Postaholic

>>Is import java.util.Scanner; similar to Scanner scan = new Scanner(System.in); ?

No, it's just another way to let the Java compiler know what Scanner class you mean. That is: there are many programmers around, and if a programmer decides to write a class and call it Scanner, then this is possible. But in the Java API there's also a class named Scanner, however it's in a package: java.util. Actually the full name of the Scanner class in this example is java.util.Scanner. Now you've two options if you want to use that class:

Option one -- explicitly typing 'java.util.Scanner', every time you want to use the class
java.util.Scanner.

Option two: -- importing java.util.Scanner, so you do not have to type it every time again, and so you can just refer to java.util.Scanner by typing 'Scanner'. This is the most common way, and it saves you a whole lot of typing.

tux4life 2,072 Postaholic
  • You forgot the import I was talking about in my previous post. No worries, just add the following line to the top of your file: import java.util.Scanner;
  • If variables height and weight are of type int, then this won't work: weight = scan.nextDouble(); You either need to make both variables of type double, or you read an integer value instead of a double value, by writing weight = [B]scan.nextInt();[/B] instead.
  • [NOTE: this only applies if variables weight and height are of type int]

    In this line: double BMI = weight/(height*height); you're effectively assigning an integer value to a variable of type double, this is because an integer division occurs. The result of an integer division is simply: another integer. Probably this is not your intent, and you might want to put in a cast to double to prevent an integer division from occuring, doing a floating point division instead. You can do this by changing that line to: double BMI = [B](double)[/B]weight/(height*height);

tux4life 2,072 Postaholic

>how can i compare in if statement, two Characters??
To compare the first two characters entered, then you'll need to change your if-statement (line 16) to: if (arrayinput1[0] == arrayinput2[0]) >>You need to loop the arrays and compare their values individually.
Not necessarily, if he's allowed to use the java.util.Arrays class, then he can do the comparison using the equals() method.
Example:

// In this code example I assume that firstArray and secondArray are primitive array
// reference variables, and that and import java.util.Arrays; has been done.
if (Arrays.equals(firstArray, secondArray))
  System.out.println("The arrays are equal.");
else
  System.out.println("The arrays are not equal.");
tux4life 2,072 Postaholic

>>it says. NOT EQUAL.
That's because you're comparing two reference variables. You're not actually comparing the values entered by the user.

tux4life 2,072 Postaholic

If you want to take input like: hello world, and want this to be stored in a character array when the user presses the enter-key, then you can do something like this (other ways exist):

  • Use the Scanner class to read a line from the input. (a reference to a String object containing that line will be returned).
  • Convert the String to a character array, by calling its toCharArray() method, and assigning the returned reference to a char[] reference variable.
tux4life 2,072 Postaholic

You actually want to read a string from the keyboard, and store it in a character array? Do you want to do that?

tux4life 2,072 Postaholic
  • Add import java.util.Scanner; to the top of your program (outside your class).
  • Remove lines 10 and 11.
  • On line 23, change int to double.
  • Line 25 needs some modification, change it from:

System.out.println("A person with weight "+weight+" lbs and height "+height+" inches has bmi = "+bmi+"); ) to: System.out.println("A person with weight "+weight+" lbs and height "+height+" inches has bmi = "+BMI);

tux4life 2,072 Postaholic

But then, this should not work too

Short a=34;

But it does. There are as many cast/box conversions here.

From the Java Language Specification (3rd Edition):

5.2 Assignment Conversion

...

A narrowing primitive conversion followed by a boxing conversion may be used if the type of the variable is :

  • Byte and the value of the constant expression is representable in the type byte.
  • Short and the value of the constant expression is representable in the type short.
  • Character and the value of the constant expression is representable in the type char.

(Source: http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#184206)

Therefore it is legal to write things such as:

public class TestBoxing
{
  public static void main(String[] args)
  {
    Short s = 1 * 2 * 3 * 4 * 5 * 6; // assign 720
    System.out.println(s);
  }
}

However, the following isn't legal:

public class TestBoxing2
{
  public static void main(String[] args)
  {
    Short s = 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8; // try to assign 40320
    System.out.println(s);
  }
}

If you'd try to compile the TestBoxing2 class, then you'd get a compiler error, because the value of the constant expression 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 (that is: 40320) cannot be represented in the type short. For your convenience I included the compiler error as well:

TestBoxing2.java:5: incompatible types
found : int
required: java.lang.Short
Short s = 1 …

tux4life 2,072 Postaholic

@Adak: I'm pretty sure you probably know this, but for those who don't: The reason it failed is that Turbo C is a 16-bit compiler and the programs is produces is limited to 640 Meg RAM, mimus the amount needed for the operating system and other drivers. It actually winds up to somewhere between 450-540 meg.

AD, You meant kilobytes right?

tux4life 2,072 Postaholic

When looping arrays, an array of length: name.length() , has indexes from 0 to name.length()-1.
An array of length 3 has arr[0], arr[1], arr[2], not arr[3]
So the for loop shouldn't be: for (int i = 0; i <= name.length(); i++)

I think you are confusing array's length attribute with class String's length() method.

[edit]
@kumpul101:

// Using a "normal" for-loop.
for (int i = 0; i < splitname.length; i++) {
  char fname = splitname[i].charAt(0);
  System.out.print(fname + ".");
}

Can alternatively be rewritten as:

// Using an "enhanced" for-loop.
for (String n : splitname) {
  char fname = n.charAt(0);
  System.out.print(fname + ".");
}

[/edit]

tux4life 2,072 Postaholic

You could also use "StringTokenizer" to split the string up and get the initials of each of the tokens

The String method split() is generally recommended for new code.

From the Java API Docs:

StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

tux4life 2,072 Postaholic

How do you suppose I should handle something like this?

Create an initialize() function which takes a pointer to a 'type' as argument, and each time you want to initialize a variable of type 'type', then you call that function and pass it the address of the 'type' variable you wish to initialize.

void initialize( type *i )
{
  /* initialization code */
}