Java, programming |
![]() ![]() |
Java, programming |
Feb 19 2012, 09:32 AM
Post
#1
|
|
|
Lurker Serf |
Hey guys, I've just started a dual cert4 in programming and networking and I've been given some java code to mod changing variables from numbers to names.
Here is what I came up with but it doesn't work could someone give me a hand with it please.. public class Swap{ public static void main(String[]args){ int nameOne = Susan, nameTwo = Peter, temp; //Swap two variables: temp = nameOne; nameOne = nameTwo; nameTwo = temp; System.out.println("nameOne= " + nameOne + "; nameTwo=" + nameTwo); } } |
|
|
|
Feb 19 2012, 09:56 AM
Post
#2
|
|
|
Hero Guru ![]() |
First off you're declaring nameOne, nameTwo and temp as integers.
Integers can only hold whole numbers. But I'm guessing you want to have the variables hold the names "Susan" and "Peter". To do that you need to change the int to String. This changes what time of data the variable holds. You'll also need to surround the "Susan" and "Peter" in double quotes, just like you've done down the bottom in the system.out.println(). This post has been edited by Charcoal: Feb 19 2012, 09:57 AM -------------------- I<3G
Kikz: Good for you, your sig says something. whoop-de-do dude. |
|
|
|
Feb 19 2012, 10:44 AM
Post
#3
|
|
|
Lurker Serf |
First off you're declaring nameOne, nameTwo and temp as integers. Integers can only hold whole numbers. But I'm guessing you want to have the variables hold the names "Susan" and "Peter". To do that you need to change the int to String. This changes what time of data the variable holds. You'll also need to surround the "Susan" and "Peter" in double quotes, just like you've done down the bottom in the system.out.println(). Cheers Charcoal forgot about the name variables that needed to be covered when changing it from numbers |
|
|
|
![]() ![]() |
| Lo-Fi Version | Time is now: 23rd May 2013 - 03:26 AM |