Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By roofninja

Thread: string split problem

  1. #1
    roofninja is offline Member
    Join Date
    Jul 2012
    Location
    Asheville, NC
    Posts
    14
    Rep Power
    0

    Default 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);
    }}

  2. #2
    Waflix is offline Member
    Join Date
    Jul 2012
    Location
    Zuid-Holland, The Netherlands
    Posts
    16
    Rep Power
    0

    Default 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.

    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);
    }}
    This website helped gave me a better understanding of arrays and how they work:
    All About Java Arrays

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: string split problem

    Can you copy and paste the error?

  4. #4
    Waflix is offline Member
    Join Date
    Jul 2012
    Location
    Zuid-Holland, The Netherlands
    Posts
    16
    Rep Power
    0

    Default 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

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default 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.

  6. #6
    roofninja is offline Member
    Join Date
    Jul 2012
    Location
    Asheville, NC
    Posts
    14
    Rep Power
    0

    Default 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.

  7. #7
    roofninja is offline Member
    Join Date
    Jul 2012
    Location
    Asheville, NC
    Posts
    14
    Rep Power
    0

    Default 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]);
    }}
    DarrylBurke likes this.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default 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:
    Java Code:
        InetAddress thisIp =InetAddress.getLocalHost();
        String[] myip2=thisIp.getHostAddress().split("\\.");
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    roofninja is offline Member
    Join Date
    Jul 2012
    Location
    Asheville, NC
    Posts
    14
    Rep Power
    0

    Default Re: string split problem

    I am reading and learning from an old book. So I am sure some of the things that I am learning, are out dated. I will remember this. Thanks.

Similar Threads

  1. String split help
    By YoungJavaBoy in forum New To Java
    Replies: 7
    Last Post: 01-19-2011, 01:39 AM
  2. Split a String with split()--Help
    By danilson in forum New To Java
    Replies: 7
    Last Post: 11-19-2010, 04:08 PM
  3. String Split
    By sarovarc in forum New To Java
    Replies: 6
    Last Post: 04-19-2010, 05:06 AM
  4. How to split a String using split function
    By Java Tip in forum java.lang
    Replies: 4
    Last Post: 04-17-2009, 08:27 PM
  5. How to split a String using split function
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 09:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •