I think your fix is correct if you only want the first name.
----- Original Message -----
From: "Ravishankar Rajagopalan" <[email protected]>
To: "Sue-Fen Cuti" <[email protected]>, "jpassion java"
<[email protected]>
Cc: "Sang Shin" <[email protected]>
Sent: Wednesday, June 12, 2013 11:28:46 PM
Subject: Re: [jpassion_java] Java: Homework on Arrays
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.
For more options, visit https://groups.google.com/groups/opt_out.