Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By jim829

Thread: if ( string = "text" ) problem

  1. #1
    Vantrax is offline Member
    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0

    Question if ( string = "text" ) problem

    Hey guys,
    Some of you might recognize me a previous java troubleshooting, but that has gratefully been fixed.
    Now I was experimenting with what I already knew from statements to make a little 'cmd' simulation, apart from other errors, I encounter this one problem, whatever I type as input, the script always goes to the 'else' section, and I have no idea how to solve it

    Here's my little script, I have no idea what I do wrong.
    Java Code:
    import java.util.Scanner;
    public class Mone {
    
    	public static void main(String[] args) {
    		System.out.println("Macrohard Doors [Version 4.3.2013]");
    		System.out.println("Copyright (c) 1996 Macrohard Inc. All rights preserved");
    		System.out.println("");
    		System.out.println("Anon:");
    		Scanner scan = new Scanner(System.in);
    		String lone = scan.nextLine();
    		
    		if (lone == "ipconfig") {
    			System.out.println("Wireless network");
    			System.out.println("");
    			System.out.println("Local IP . . . . . . : ez28::w2e3:15as:5w1b:b3c2%29");
    			System.out.println("IPv4-adres. . . . . . . . . . . . . : 137.159.3.104");
    			System.out.println(". . . . . . . . . . . : 258.258.258.1");
    			System.out.println("Standardgateway. . . . . . . . . . : 137.159.3.1");
    		} else {
    			System.out.println(lone + " is not recognized as intern or exterd command, program or batchfile.");
    		}
    	}
    
    }
    When I type 'ipconfig' and press enter, the program still displays the text from the else statement.

    PS: I also have trouble displaying C:\Users\Anon;

    Java Code:
    System.out.println("C:\Users\Anon");
    doesn't work

    PPS: that is not my real IP of course, and anon => anonymous ;)

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,772
    Rep Power
    5

    Default Re: if ( string = "text" ) problem

    Except in rare situations, never use == to compare objects. Use equals.

    Java Code:
    if (lone.equals("ipconfig") {
    ...
    }
    Regards,
    Jim
    Vantrax likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Vantrax is offline Member
    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0

    Default Re: if ( string = "text" ) problem

    thanks jim, you've got my thumbs up!!! :)

    EDIT:

    I've tried to convert it into a switch statement, but I get this error:

    Java Code:
    Scanner scan = new Scanner(System.in);
    		String lone = scan.nextLine();
    		
    		switch (lone) {
    		case (lone.equals ("ipconfig")):
    		
    			System.out.println("Wireless network");
    			System.out.println("");
    			System.out.println("Local IP. . . . . . . . . . . . . . : ez28::w2e3:15as:5w1b:b3c2%29");
    			System.out.println("IPv4-adres. . . . . . . . . . . . . : 137.159.3.104");
    			System.out.println("Subnetmask. . . . . . . . . . . . . : 258.258.258.1");
    			System.out.println("Standardgateway . . . . . . . . . . : 137.159.3.1");
    			break;
    		
    			default:
    			System.out.println(lone + " is not recognized as intern or exterd command, program or batchfile.");
    @case (lone.equals ("ipconfig")):

    It says "cannot convert from boolean to string,
    how can I solve this?
    Last edited by Vantrax; 03-05-2013 at 09:32 PM.

  4. #4
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    384
    Rep Power
    5

    Default Re: if ( string = "text" ) problem

    Hi Vantrax.

    Switches expect true values for each case statement. In this instance use the following:
    Java Code:
    case "ipconfig":
    A point to note, comparing string values only became available in Java SE 7 so will not work on older Java installations.

    Regards.

  5. #5
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: if ( string = "text" ) problem

    Quote Originally Posted by Ronin View Post
    A point to note, comparing string values only became available in Java SE 7 so will not work on older Java installations.
    I guess what you mean is not comparing string values, but using string in switch statement. Prior to JDK 7 we can not use string in switch statement.

  6. #6
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    384
    Rep Power
    5

    Default Re: if ( string = "text" ) problem

    Quote Originally Posted by wsaryada View Post
    I guess what you mean is not comparing string values, but using string in switch statement. Prior to JDK 7 we can not use string in switch statement.
    I do indeed.

    Thanks.

Similar Threads

  1. Replies: 0
    Last Post: 12-07-2012, 08:29 AM
  2. Replies: 3
    Last Post: 10-30-2012, 03:06 PM
  3. Replies: 1
    Last Post: 05-07-2012, 05:35 PM
  4. Replies: 3
    Last Post: 03-03-2010, 12:23 AM
  5. Replies: 0
    Last Post: 04-02-2009, 07:02 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
  •