Jump to content


Photo

Java


  • Please log in to reply
2 replies to this topic

#1 BloodReign

BloodReign

    Serf

  • Lurker
  • 15 posts

Posted 19 February 2012 - 09:32 AM

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

#2 Charcoal

Charcoal

    Guru

  • Hero
  • 14,036 posts

Posted 19 February 2012 - 09:56 AM

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().

Edited by Charcoal, 19 February 2012 - 09:57 AM.

I<3G
Kikz: Good for you, your sig says something. whoop-de-do dude.
-=-=-=-=-=-=-=-=-=-
AzpUvwG.png@Charcoal|

#3 BloodReign

BloodReign

    Serf

  • Lurker
  • 15 posts

Posted 19 February 2012 - 10:44 AM

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users