Thanks Sang and Sue,
The error that i got initially is given below:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source
code - incompatible types
required: java.lang.String
found: java.lang.String[]
at GreatestNumber.main(GreatestNumber.java:24)
Java Result: 1
However, based on Sue's advice I changed it to the following and now it
works fine:
firstname[counter] = personfull[counter].split(" ")[0];
This only gets the first name.
Let me know if there is the right approach.
Thank you.
Regards,
Ravishankar Rajagopalan
On Thu, Jun 13, 2013 at 7:07 AM, Sue-Fen Cuti <[email protected]> wrote:
> I bet I know the reason.****
>
> ** **
>
> The culprit is on the split(). Split returns an array of Strings. My
> guess is that when counter = 1 and personfull has more than 2 words or
> when count = 2 and personful has more than 1 word. You need to rewrite the
> code for it to run successfully.****
>
> ** **
>
> firstname[counter] = personfull[counter].split(" ");****
>
> ** **
>
> ** **
>
> *From:* [email protected] [mailto:
> [email protected]] *On Behalf Of *Ravishankar Rajagopalan
> *Sent:* Tuesday, June 11, 2013 3:51 PM
> *To:* [email protected]
> *Subject:* [jpassion_java] Java: Homework on Arrays****
>
> ** **
>
> Hi,****
>
> ** **
>
> I am trying out the homework for Java Arrays. I am getting an error with
> the split statement. I have given the code that I am using below. Could you
> please let me know what is wrong with my approach?****
>
> ** **
>
> import javax.swing.JOptionPane;****
>
> ****
>
> public class LongestName {****
>
> ****
>
> public static void main(String[] args) {****
>
> //int[] num = new int[10];****
>
> String [] personfull = new String[3];****
>
> String [] firstname = new String[3];****
>
> int counter;****
>
> int max = 0;****
>
> int totalnumber = 3;****
>
> ****
>
> for (counter = 0;counter < totalnumber;counter++){****
>
> personfull[counter] = JOptionPane.showInputDialog("Enter the
> name");****
>
> firstname[counter] = personfull[counter].split(" ");****
>
> } ****
>
> ****
>
> for(counter = 0;counter < totalnumber; counter++){****
>
> System.out.println(personfull[counter]);****
>
> System.out.println(firstname[counter]);****
>
> }****
>
> ****
>
> ****
>
> }****
>
> }****
>
> ** **
>
> Thank you.****
>
> ** **
>
> Regards,****
>
> Ravishankar Rajagopalan****
>
> --
> You received this message because you are subscribed to the Google Groups
> "JPassion.com: Java Programming" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> Visit this group at http://groups.google.com/group/jpassion_java?hl=en-US.
> For more options, visit https://groups.google.com/groups/opt_out.
>
> ****
>
>
--
You received this message because you are subscribed to the Google Groups
"JPassion.com: Java Programming" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
Visit this group at http://groups.google.com/group/jpassion_java?hl=en-US.
For more options, visit https://groups.google.com/groups/opt_out.