Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   accepting user input (http://www.programmingforums.org/showthread.php?t=12357)

fresher Jan 11th, 2007 1:58 PM

accepting user input
 
Hi all, i've this code below which prompts a user to select from a list of ip addresses.e.g 192.168.2.1....etc. Now i wona modify this code so that if the machine on which the program is run has a different network part (192.168.2.) then I can prompt the user to enter their network ID e.g (148.145.23.) then increment the last bit using a for loop. Anyone able to help me out pls? thanks in advance

I have added this block of code below in order to do this but it doesnt work:
:


System.out.print("Enter your network part or hit enter to proceed: ");
            String network = in.readString();
           
            //test
            System.out.println(network);
           
            //if enter is pressed - same network
            if (network == null)
       
            {
           
            System.out.print("SELECT A MACHINE TO VIEW FROM LIST: ");
            int choice = in.readInteger();
            }

            else
            {
                //pass new network part to ip string variable
                ip = network;
            }


nb: this code is part of my previous post on ' passing a string to a variable.....' click here to view full code:http://www.programmingforums.org/for...-variable.html
any help appreciated.

Arevos Jan 11th, 2007 2:30 PM

Possibly:
:

if ("".equals(network))
Or perhaps:
:

if ("".equals(network.trim()))

fresher Jan 11th, 2007 2:43 PM

Quote:

Originally Posted by Arevos (Post 122513)
Possibly:
:

if ("".equals(network))
Or perhaps:
:

if ("".equals(network.trim()))


thanks for your reply but is network.trim() a java function and what is the meaning of " ".equals..... please? a little comment will be perfect. thanks in advance

Arevos Jan 11th, 2007 2:57 PM

Quote:

Originally Posted by fresher (Post 122515)
thanks for your reply but is network.trim() a java function

Yes, and a quick Google or a look at the Sun javadocs would tell you all about it

Quote:

Originally Posted by fresher (Post 122515)
and what is the meaning of " ".equals..... please? a little comment will be perfect. thanks in advance

To check whether two strings, a and b, are equal, you'd use the following code:
:

a.equals(b)
If b is null, then the above expression returns false. If a is null, then an exception is raised, because you can't have null.equals(b).

So when comparing a string whose value could possibly be null (i.e. network), against a string whose value cannot be null (i.e. ""), then it's best to have the string that cannot be null as the one on the left, and the string that can be null on the right.

:

"".equals(stringThatMightBeNull)    // false when null

stringThatMightBeNull.equals("")    // error when null


fresher Jan 11th, 2007 2:59 PM

Also Arevos the 'if (" ".equals(network))' works if enter is pressed but the else part doesnt work if a network address is entered. In otherwords when the network part is entered, the switch statement is not executed. pls help me out. thanks in advance


All times are GMT -5. The time now is 1:49 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC