0

it looks like case 1, reading the data from a text doc is doing what it supposed to do (coz if i put the output lines under case 1 it works) however case2 doesn't have anything in the nameBox[] .. it didn't get the array index from case 1... any suggestion? thanks very much

public class Namebody //extends Name
{
	public static void main(String[] args)
	{
		System.out.println("<<Welcome to the personal information database>>");
		System.out.println("Please operate by the following options:");
		int option =0;
		int totalName=0;
		
		while(option!=9)
		{
			System.out.println("1.Load data from a file you choose.");
			System.out.println("2.See all the data.");
			System.out.println("3.Search the data.");
			System.out.println("4.Sort the data.");
			System.out.println("5.Edit the data");
			System.out.println("6.Add data");
			System.out.println("7.Delete data.");
			System.out.println("8.Save data");
			System.out.println("9.Exit out.");
			System.out.println();
			System.out.println("Select an option:");
			
			Scanner optionChoice = new Scanner(System.in);
			option = optionChoice.nextInt();
			Name nameBox[]= new Name[500];
	
	
		switch(option)
		{
			case 1:
			{
				/*
				readData(nameBox);
				*/
				int inID;
				int count=0;
				String inLastname, inFirstname,inPhone;
				double inPrice;
				String filename=null;
				Scanner reader = null;
				Scanner keyboard = new Scanner(System.in);
				
				System.out.println("Please give me the file name.");
				filename= keyboard.nextLine();
			
				try
				{
					reader = new Scanner(new FileReader(filename));
				}
			
				catch(FileNotFoundException e)
				{
					System.out.println("File not found!");
					System.exit(0);
				}
				
				while(reader.hasNextLine())
				{
					inID=reader.nextInt();
					inLastname= reader.next();
					inFirstname= reader.next();
					inPhone= reader.next();
					inPrice= reader.nextDouble();
					
					Name temp = new Name(inID, inLastname, inFirstname, inPhone, inPrice);
				
					nameBox[count]= temp;
					count++;
				}
				break;
				/*
				totalName= getTotalName(nameBox);
				System.out.println("total names are "+totalName);
				break;
				*/
			}
			
			case 2:
			{
				/*
				System.out.println(totalName);
				showData(nameBox, totalName);
				
				break;
				*/
				for(int i=0; i<totalName; i++)
				{
					nameBox[i].output();
				}
				break;
			}
			
			
			case 9:
			{
				System.out.println("Good Bye");
				System.exit(0);
			}
			
			default:
			{
				System.out.println("This is not an vaild option.");
				
			}
		}
		
		
	
	}
	} //for the while loop
2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by javaAddict
0
while(option!=9) {

Name nameBox[]= new Name[500];

}

You create a new nameBox every time you repeat the loop, so the first time you are in the loop you give values to the local nameBox, but when the loop executes again, you create again a new nameBox (new Name[500]) that naturally hasn't anything inside.
Put the declaration of nameBox outside the while-loop

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.