Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Beginner Java Course, Stuck on my First Assignment. Help Needed.

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Beginner Java Course, Stuck on my First Assignment. Help Needed.

    I am taking my first Java course and I am stuck on my assignment which is the following:

    Write a java program that prompts the user to input two integer values, swap the values of two variables (don’t just display them in opposite locations), and then display the swapped values back to the user.

    I have an idea of what it is supposed to look like:

    Please enter two values:
    Value #1 = 1
    Value #2 = 4
    Swapping…
    Value #1 = 4
    Value #2 = 1

    I understand that i have to use the Scanner object to get the user inputs and I understand how to prompt the user to insert values. What I really need help on is the "swapping" of the values and displaying them back to the user.

    Any help is really appreciated. Thank You.
    Last edited by jlcarrillo; September 12th, 2014 at 01:36 AM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Beginner Java Course, Stuck on my First Assignment. Help Needed.

    Welcome to the forum! Please read this topic to learn how to post code correctly and other useful info for new members.

    Start with what you know and code/test that. Come back with that code and ask for help with the next part, if you still need it. Be sure to post your code correctly per the above link.

  3. #3
    Junior Member
    Join Date
    Sep 2014
    Location
    Mumbai
    Posts
    10
    My Mood
    Cheerful
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Beginner Java Course, Stuck on my First Assignment. Help Needed.

    Try to build logic and if it do not workout post the algorithm here we will try to help you.Building logic is the very important part of programming.so try yourself.

  4. #4
    Junior Member
    Join Date
    Sep 2014
    Location
    India
    Posts
    18
    My Mood
    Fine
    Thanks
    11
    Thanked 2 Times in 2 Posts

    Default Re: Beginner Java Course, Stuck on my First Assignment. Help Needed.

    first of all...get the two values that is to swap from the user with the help of scanner object.... then you can swap the values by taking a third temporary variable that stores the value of the first two variables temporarily, one by one, for sometime.

  5. #5
    Member
    Join Date
    Dec 2013
    Location
    Honolulu
    Posts
    83
    Thanks
    1
    Thanked 4 Times in 2 Posts

    Default Re: Beginner Java Course, Stuck on my First Assignment. Help Needed.

    Ok. I hope this does it.

    public class Swapping   {
       int x = 1;
       int y = 4;
     
        public static void main(String args[])  {
         If(x)
          {
             System.in.println(x);
             }
              System.in.println(y);
     
               switch(x)
                }
                case 1;
                int y = x;
                System.out.println("case 1);
                break;
                case 2;
                int x = y;
                System.out.println("case 2);
                break;
                }
        }
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Beginner Java Course, Stuck on my First Assignment. Help Needed.

    planetHollywood
    Your post is over 3 years too late.

    In the future please don't post full solutions for OPs questions without any comments to explain what the code is doing

    and also make sure the code will compile, execute and produce the correct results before posting solutions.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Java beginner - help needed!
    By Mace in forum Object Oriented Programming
    Replies: 3
    Last Post: February 6th, 2019, 07:22 AM
  2. Help needed in deadlock assignment using java
    By prince143 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 13th, 2014, 03:36 PM
  3. Beginner... stuck on how to use FormattedFields in java for inserting dates
    By Interface123 in forum Java Theory & Questions
    Replies: 4
    Last Post: February 11th, 2014, 07:15 AM
  4. Java Beginner. Help needed.
    By Jack1 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 3rd, 2013, 12:18 AM
  5. Help me with my 1st Java Assignment! Stuck!!
    By adnan.alvee in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 2nd, 2013, 07:55 PM