Current location: Hot Scripts Forums » Programming Languages » Everything Java » Return String values from a object saved in an array


Return String values from a object saved in an array

Reply
  #1 (permalink)  
Old 04-06-05, 09:30 AM
mr_wazzup mr_wazzup is offline
New Member
 
Join Date: Apr 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Return String values from a object saved in an array

Hi
I have just started programming in Java. I have encountered a problem when accessing data stored in an object array, type of the data is a String. If anyone could help me i would be very thankful

This is the code I have produced so far:

Code:
class Animal{
	public static Object[] animalArray = new Object[10];
}


class Dog{
	
	static String rase;
	
	static String navn;
	
	public Dog(String rase, String navn){
		this.rase = rase;
		this.navn = navn;
	}
}


class Applikasjon{
public static void main(String args[]){
		
	
		
System.out.println("Choose menu 1 \"create new dog\" or 2 \"show element 1\"");
		
int brukerValg = SavitchIn.readLineInt();
		
while(brukerValg > 0){
		
		
switch(brukerValg){
			
case 1:	
				
System.out.println("Create new dog");
				

System.out.println("Enter type of the dog");
String brukerInputRase = SavitchIn.readLine();
				
		

System.out.println("Enter name of the dog");	
String brukerInputNavn =  SavitchIn.readLine();
			
int x = 0;
				
while(x < Animal.animalArray.length){
				
if(Animal.animalArray[x] == null){
				
Dog dog = new Dog(brukerInputRase, brukerInputNavn);
				
Animal.animalArray[x] = dog;
				
break;
				
}
				
else
				
System.out.println(x);	
x = x+1;
				
				
				}
break;
/*
This is where I need the code to print what i have 			
case 2:

System.out.println();
			
}
			
System.out.println("Choose menu 1 \"create new dog\" or 2 \"show element 1\",  0 to quit");

brukerValg = SavitchIn.readLineInt();
}

/*Don't mind this, it is just a test to check that the object actually has been stored	
*/
String output = "Subscript\tValue\n";
				
for (int testA = 0; testA  < Animal.animalArray.length; testA++)
				
output += testA + "\t\t" + Animal.animalArray[testA] + "\n";
				
System.out.print(output);
		
		
		
}
	
}
What i need to do is to make case 2 print the userinput from let's say animalArray[0] (race and name)
The class i use for userinput is SavitchIn.


Any help will be appriciateted

Last edited by MadDog; 04-06-05 at 04:25 PM. Reason: Tag your code!
Reply With Quote
  #2 (permalink)  
Old 04-15-05, 09:11 AM
contryboy contryboy is offline
New Member
 
Join Date: Apr 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
(1)The "rase" and "navn" should not be static
(2)To get the Dog objects, you may try it like this:

Code:
for(int i=0; i<Animal.aimalArray.length; i++) {
    if(Animal.animalArray[i] != null) {
        Dog dog = (Dog)Animal.animalArray[i];
        System.out.println("index: " + i);
        System.out.println("rase: " + dog.rase);
        System.out.println("navn: " + dog.navn);
    }
}
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Disable form fields to be submitted RickyRod JavaScript 2 05-24-04 10:15 AM
Upload file to table so ONLY files tied to primary key are displayed in record? grafixDummy PHP 4 12-20-03 04:28 PM
Declared Functions skipper23 PHP 4 12-17-03 10:06 AM
index page not showing up skipper23 PHP 3 12-15-03 01:10 PM
Help trim code down TheLaughingBandit JavaScript 0 09-02-03 09:50 AM


All times are GMT -5. The time now is 05:30 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.