Please help, cant get this working!

Associate
Joined
14 Oct 2011
Posts
21
Hi guys, can someone please advise me on why this wont work!
Thanks

Code:
import java.util.Scanner;
/* This program is from lecture 3, it asks the user for their name, then says hello name
 * if that name = lucy then i want it to say something else.
 * 19/10/11
 * @author ahr */ 
public class lecture { 
	
	public static void main (String[] args) throws Exception {
		String  name;
		System.out.println("Enter your name");
		Scanner Scan = new Scanner(System.in);
		name = Scan.next(); 
		if (name = 'lucy') 
			System.out.print("Hi Rucy Roo");
		else
			System.out.print("hello " + name);
		
	}

}
 
Sorry, I'm not a Javascript coder, but it would probably help to explain what is going wrong with it?

Edit - Java even. Stupid brain :mad:
 
Code:
import java.util.Scanner;
/* This program is from lecture 3, it asks the user for their name, then says hello name
 * if that name = lucy then i want it to say something else.
 * 19/10/11
 * @author ahr */ 
public class lecture { 
	
	public static void main (String[] args) throws Exception {
		String  name;
		System.out.println("Enter your name");
		Scanner Scan = new Scanner(System.in);
		name = Scan.next(); 
		if (name.equals("lucy")) 
			System.out.print("Hi Rucy Roo");
		else
			System.out.print("hello " + name);
		
	}

}

if (name = 'lucy') , you're assigning here. If you were to do if (name == 'lucy') that wouldnt work either. You need to use string.equals(arg).
 
That's interesting because in .NET strings are treated as value types (believe they are immutable as well) so:

Code:
string name = "lucy";

if(name == "lucy")
{
//would hit here as condition is true
}

The above would actually work, I think :p. I know this is Java but just found this interesting :).

Also would 'lucy' throw an error because he is declaring a char rather than a string? I.e. Should be "lucy".
 
It worked!

This works, thank you so much! :]



Code:
import java.util.Scanner;
/* This program is from lecture 3, it asks the user for their name, then says hello name
 * if that name = lucy then i want it to say something else.
 * 19/10/11
 * @author ahr */ 
public class lecture { 
	
	public static void main (String[] args) throws Exception {
		String  name;
		System.out.println("Enter your name");
		Scanner Scan = new Scanner(System.in);
		name = Scan.next(); 
		if (name.equals("lucy")) 
			System.out.print("Hi Rucy Roo");
		else
			System.out.print("hello " + name);
		
	}

}

if (name = 'lucy') , you're assigning here. If you were to do if (name == 'lucy') that wouldnt work either. You need to use string.equals(arg).
 
In C# you could just use case/switch.

Is there not something like this for Java?

*edit*

As an example.

Code:
namespace GreetName
{
    class programme
    {
        static void Main(string[] args)
        {
                Console.WriteLine("Enter your name");
                string userName = Console.ReadLine();

                switch (userName.ToUpper())
                {

                    case "LUCY":
                        Console.WriteLine("Hi Rucy Roo");
                        break;

                    default:
                        Console.WriteLine("Hello" + userName);
                        break;

                }
           }
     }
}

Not checked in VS as im at work, but summit like that, im sure java has the same kind of thing
 
Last edited:
Im confident i know why, the name.equals and brackets lucy mean that it is isolated (kinda like bodmas) and the .equals replaces the = .

As for C# i have no idea, we havnt started learning that yet!
 
That's interesting because in .NET strings are treated as value types (believe they are immutable as well)

It's not that they're treated as value types, it's because .NET supports operator overloading, so the String type has the == operator overloaded to compare the contents of the string rather than comparing object references.
 
As for C# i have no idea, we havnt started learning that yet!

After a quick Java search (I feel dirty) but i belive it would be something like....

Code:
public class lecture { 
	
	public static void main (String[] args) throws Exception {
                String  name;
		System.out.printIn("Enter your name");
		name = in.readLine();; 

        switch (name)
        {
        case lucy:
          System.out.print("Hi Rucy Roo"); break;
        default:
          System.out.print("hello " + name);
        }
    }
}

Just how i would go about things, as it makes things easy if you was needed to add extra results.

Note that this code probably dont work, but it's the general idea :p

*edit*

you can use elseif in your example to add more too. But i think switch is much better in this case
 
Last edited:
Back
Top Bottom