Results 1 to 5 of 5
  1. #1
    Arbalest is offline Member
    Join Date
    Sep 2012
    Posts
    11
    Rep Power
    0

    Default method not taking effect

    hello everyone i'm kinda new in java and i'm having trouble with the method swap which is not taking effect here's the code

    Java Code:
    import java.util.*;
    public class methodstest1 {
        
        public static void swap(int a, int b){
            if(a>b){
                int temp =a;
                a=b;
                temp=b;
                
            }
        }
        public static int sum(int a, int b){
           int summ=0;
            for(int i=a; i<b; i++){
                summ+=i;
            }
            return summ;
        }
        public static void main(String[] args){
            Scanner input = new Scanner(System.in);
            System.out.print("type first delimiter ");
            int del1=input.nextInt();
            System.out.print("type 2nd delimiter ");
            int del2=input.nextInt();
            swap(del1, del2);
            System.out.println(" total sum = "+sum(del1, del2));
            
        }
        
        
        
    }

    output : the program is supposed to sum all numbers written by user as delimiters, the method swap is supposed swapping delimiters in case the 1st one is superior to the 2nd but it's not taking effect , i know i'm doing a stupid error but i just can't locate it, if someone could help me with this and thx
    Last edited by Arbalest; 10-19-2012 at 08:08 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: method not taking effect

    Java parameters are pass by value always, so a swap method on primitive parameters (int, byte, double, etc...) will never work.

  3. #3
    Arbalest is offline Member
    Join Date
    Sep 2012
    Posts
    11
    Rep Power
    0

    Default Re: method not taking effect

    i see ,so i cancel the method and write the code within the main right?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: method not taking effect

    Quote Originally Posted by Arbalest View Post
    i see ,so i cancel the method and write the code within the main right?
    Yes, that's one way to do this.

  5. #5
    Arbalest is offline Member
    Join Date
    Sep 2012
    Posts
    11
    Rep Power
    0

Similar Threads

  1. jQuery effect
    By lilleza87 in forum New To Java
    Replies: 4
    Last Post: 01-11-2011, 02:36 PM
  2. Pulse effect on text
    By Lingerz in forum AWT / Swing
    Replies: 2
    Last Post: 10-29-2010, 12:59 AM
  3. fading effect
    By silversurfer2in in forum AWT / Swing
    Replies: 4
    Last Post: 06-11-2010, 03:23 PM
  4. Smoke Effect help
    By jammyjamsticy in forum AWT / Swing
    Replies: 1
    Last Post: 04-20-2009, 12:34 PM
  5. shadow effect in swt-sash
    By Jack in forum SWT / JFace
    Replies: 2
    Last Post: 07-02-2007, 05:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •