Results 1 to 9 of 9
Thread: string split problem
- 07-10-2012, 09:51 PM #1
Member
- Join Date
- Jul 2012
- Location
- Asheville, NC
- Posts
- 14
- Rep Power
- 0
string split problem
I don't know enough of Java, so I am just stabbing in the dark here. I am lost. What this program should do is get the local IP and display only the 2nd octet of the IP.
It doesn't compile and the error message is"incompatible types" for where the arrow is in the code and it is pointing at the "(". Required String???
Java Code:import java.net.*; import java.io.*; import javax.swing.JOptionPane; public class findip { public static void main(String[] args) throws IOException{ String myip,myip2; InetAddress thisIp =InetAddress.getLocalHost(); myip=thisIp.getHostAddress(); myip2=myip.split("."); //<-------- //myip2=String.split(myip,"."); //need to just show or have 2nd octet JOptionPane.showMessageDialog(null,"IP:__"+myip+"\n\nNum:__"+myip2); }}
- 07-10-2012, 10:06 PM #2
Member
- Join Date
- Jul 2012
- Location
- Zuid-Holland, The Netherlands
- Posts
- 16
- Rep Power
- 0
Re: string split problem
Your problem is that, after having split the string, the information is being saved as an array. Here's your code, but now corrected.
This website helped gave me a better understanding of arrays and how they work:Java Code:import java.net.*; import java.io.*; import javax.swing.JOptionPane; public class findip { public static void main(String[] args) throws IOException{ String myip; String[] myip2; InetAddress thisIp =InetAddress.getLocalHost(); myip=thisIp.getHostAddress(); myip2=myip.split("."); //<-------- //myip2=String.split(myip,"."); //need to just show or have 2nd octet JOptionPane.showMessageDialog(null,"IP:__"+myip+"\n\nNum:__"+myip2); }}
All About Java Arrays
- 07-10-2012, 10:08 PM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Re: string split problem
Can you copy and paste the error?
- 07-10-2012, 10:16 PM #4
Member
- Join Date
- Jul 2012
- Location
- Zuid-Holland, The Netherlands
- Posts
- 16
- Rep Power
- 0
Re: string split problem
If I'm not mistaken:
findip.java:9: incompatible types
found : java.lang.String[]
required: java.lang.String
myip2=myip.split("."); //<--------
^
1 error
- 07-10-2012, 10:24 PM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Re: string split problem
As Waflix said, and the error hints, you tried to put a String array in a String. A String array is an array such that each element is itself a String.
- 07-10-2012, 10:48 PM #6
Member
- Join Date
- Jul 2012
- Location
- Asheville, NC
- Posts
- 14
- Rep Power
- 0
Re: string split problem
I haven't gotten to arrays yet, so this is more complicated that what I think I understand. I will revisit this thread when I have more info.
- 07-31-2012, 03:55 PM #7
Member
- Join Date
- Jul 2012
- Location
- Asheville, NC
- Posts
- 14
- Rep Power
- 0
Re: string split problem
I did finally figure out what I was doing wrong with this string split problem. First, I didn't have the String array setup correctly and then I wasn't using the split correctly.
This is what I like about programming. There is always another challenge around the corner.
Java Code:import java.net.*; import java.io.*; import javax.swing.JOptionPane; public class findip { public static void main(String[] args) throws IOException{ String[] myip2=new String[3]; InetAddress thisIp =InetAddress.getLocalHost(); myip2=thisIp.getHostAddress().split("\\."); JOptionPane.showMessageDialog(null,"IP:__"+myip2[1]); }}
- 07-31-2012, 07:44 PM #8
Re: string split problem
You don't need to assign the array variable to a new array that is never used. Simply declare and assign it:
dbJava Code:InetAddress thisIp =InetAddress.getLocalHost(); String[] myip2=thisIp.getHostAddress().split("\\.");Why do they call it rush hour when nothing moves? - Robin Williams
- 08-02-2012, 10:36 PM #9
Member
- Join Date
- Jul 2012
- Location
- Asheville, NC
- Posts
- 14
- Rep Power
- 0
Similar Threads
-
String split help
By YoungJavaBoy in forum New To JavaReplies: 7Last Post: 01-19-2011, 01:39 AM -
Split a String with split()--Help
By danilson in forum New To JavaReplies: 7Last Post: 11-19-2010, 04:08 PM -
String Split
By sarovarc in forum New To JavaReplies: 6Last Post: 04-19-2010, 05:06 AM -
How to split a String using split function
By Java Tip in forum java.langReplies: 4Last Post: 04-17-2009, 08:27 PM -
How to split a String using split function
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:32 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks