Results 1 to 8 of 8
  1. #1
    Alexm92 is offline Member
    Join Date
    Sep 2015
    Posts
    9
    Rep Power
    0

    Default How to save the value of one variable to another variable

    Hi I'm just wondering could anyone help me. I want to find the first and last occurrence of the number 12 after the user types in a series of numbers and print them after the number 0 is inputted. I can get the last occurrence but cant get the first. Once the second 12 is inputted then firstOcc is reset to the index of the lastOcc. Any help would be appreciated. Thanks.
    Java Code:
    {
    	public static void main(String args[])
    	
    	{
    		int index=1;
    		int currentNumber=0;
    		int firstOcc=0;
    		int lastOcc=0;
    		
    		System.out.print("Enter the first number: ");
    		currentNumber = EasyIn.getInt();
    		while (currentNumber != 0)
    		{
    			
    			if (currentNumber == 12)
    			{
    
                             firstOcc = (index)                         
    
    			}
    	
    			if (currentNumber == 12)
    			{
    				lastOcc = (index);
    			}
    			
    		    index++;
    			System.out.print("Enter a number: "+ (index )+ " : ") ;
    			currentNumber = EasyIn.getInt();
    		
    		}
    		
    			System.out.println("Last :" + lastOcc);
    			System.out.println("First: "+ firstOcc);
    			
    		
    	}
    }
    Last edited by Alexm92; 10-13-2015 at 11:39 AM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: How to save the value of one variable to another variable

    You are setting firstOcc to the index of a number 12, no matter if it has been set before; firstOcc ends up to the index of the last occurance of a number 12. Don't set firstOcc if it has been set before ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: How to save the value of one variable to another variable

    Can the code "remember" that it has saved the first location and not change it.
    Or have two searches: First search until found and when found then do the second search.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    Alexm92 is offline Member
    Join Date
    Sep 2015
    Posts
    9
    Rep Power
    0

    Default Re: How to save the value of one variable to another variable

    Quote Originally Posted by JosAH View Post
    You are setting firstOcc to the index of a number 12, no matter if it has been set before; firstOcc ends up to the index of the last occurrence of a number 12. Don't set firstOcc if it has been set before ...

    kind regards,

    Jos
    Thanks for the help Jos. I know what is going wrong and what is happening with the code as is, but I still don't know how to fix it. If I don't set firstOcc how will it know when the first 12 is inputted?

  5. #5
    Alexm92 is offline Member
    Join Date
    Sep 2015
    Posts
    9
    Rep Power
    0

    Default Re: How to save the value of one variable to another variable

    Quote Originally Posted by Norm View Post
    Can the code "remember" that it has saved the first location and not change it.
    Or have two searches: First search until found and when found then do the second search.
    Yea how do I make the code "remember" that it has saved the first location and not change it? I'm not familiar with searches or arrays yet. I'm on my 6th week of college and haven't got that far yet.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: How to save the value of one variable to another variable

    Yea how do I make the code "remember"
    One way is to set the initial value for firstOcc to an invalid index (for example -1). When it is given a value, it will no longer have that value and the code will know it has been changed.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Alexm92 is offline Member
    Join Date
    Sep 2015
    Posts
    9
    Rep Power
    0

    Default Re: How to save the value of one variable to another variable

    Quote Originally Posted by Norm View Post
    One way is to set the initial value for firstOcc to an invalid index (for example -1). When it is given a value, it will no longer have that value and the code will know it has been changed.
    Ok I set firstOcc to -1 but its still comes out the same as lastOcc. Is this how you meant I should do it?

    Java Code:
    	{
    		int index=1;
    		int currentNumber=0;
    		int firstOcc=-1;
    		int lastOcc=0;
    		
    		System.out.print("Enter the first number: ");
    		currentNumber = EasyIn.getInt();
    		while (currentNumber != 0)
    		{
    			
    			if (currentNumber == 12)
    			{
    				firstOcc = (index);
    			}

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: How to save the value of one variable to another variable

    The code needs to check if firstOcc's value has been changed (from -1) BEFORE changing its value. If it has been changed, do not change it again.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 2
    Last Post: 03-16-2014, 12:39 AM
  2. Replies: 2
    Last Post: 11-02-2013, 08:53 AM
  3. Replies: 2
    Last Post: 01-09-2013, 08:10 PM
  4. Dynamic variable name based on other variable
    By nadissen in forum Eclipse
    Replies: 4
    Last Post: 05-06-2011, 06:22 PM
  5. Replies: 11
    Last Post: 07-07-2010, 06:02 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
  •