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 2 of 2

Thread: array problem

  1. #1
    Member
    Join Date
    Apr 2011
    Posts
    32
    Thanks
    20
    Thanked 0 Times in 0 Posts

    Default array problem

    Hello everyone, below is my code;

    import java.util.Scanner;
    public class user1{
     
    public static void main(String args[])
    	{
    	Scanner input = new Scanner(System.in);
    	String array[] = new String [15];//array for name only
    	int x;
    	do{
    	//store name
    	System.out.println("Please enter your name below");
    	name(array);
     
    	displayname(array);
    	System.out.println("To exit, press 5; to contiue press any other number");
    	x=input.nextInt();
    	}while(x!=5);
    	{
    	System.out.println("end");
    	}
    	}//end main
     
    	public static void name(String a[])
    	{
    	Scanner input = new Scanner(System.in);
    	int i=0;
    	a[i] = new Scanner (System.in).nextLine();
     
    	i=i+4;
     
    	}//end method
     
     
    	public static void displayname(String g[])
    	{
    	//look here if issues on 2nd user, 3rd etc.
    	int i=0;
    	System.out.print("|"+ g[i]+ "|"); 
    	}
     
    }//end class

    My problem is that when I add a new string name, the previous string name is replaced. I need to somehow increment my int variable' i' inside my static method named 'displayname', however everytime that method is called, 'i' is set to zero...Please help!! Thankyou


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: array problem

    Define the variable i outside the scope of the method. Several ways to do this: define it as an instance variable, define it as static outside the method so it can be accessed by the static method....

  3. The Following User Says Thank You to copeg For This Useful Post:

    u-will-neva-no (May 2nd, 2011)

Similar Threads

  1. [SOLVED] array problem
    By darego in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 12th, 2010, 06:15 PM
  2. Problem with array
    By mingming8888 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 27th, 2010, 12:17 PM
  3. [SOLVED] Array Problem
    By KevinGreen in forum Object Oriented Programming
    Replies: 2
    Last Post: January 24th, 2010, 09:07 PM
  4. Problem with 2d array
    By Anyone in forum Collections and Generics
    Replies: 2
    Last Post: November 14th, 2009, 09:32 PM
  5. Java program for 2-D Array Maze
    By Peetah05 in forum Collections and Generics
    Replies: 11
    Last Post: May 8th, 2009, 04:30 AM