[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 261
  • Last Modified:

why toString isn't work?

i write the following code but i have 2 error

why toString method doesn't work?

variable num increment dynamicly when another object is created, it must give me an error when i try to modify it like that
c[n].num = 3;


import java.util.Scanner;
public class cell {
    static int num;
	char gene[] = new char[20];
	char[] genetype = {'T','g','c','a'};	
	cell(){
		for ( int i = 0 ; i < 20 ; i++){
			gene[i] = genetype[(int)(Math.random() * 4)];
		}
    	++num;    	
  	}
  	///
	public String toString(){
	
		return (" cell[ : "+this.num + " ] = : " + this.gene );
	}
	///
    public static void main(String[] args) {
    	cell c[] ;
		c = new cell[20];
        for ( int i = 0 ; i < 10 ; ++i){
                c[i] = new cell();
        }
        
		Scanner input = new Scanner(System.in);
		int n = input.nextInt();
		c[n].toString();
		
    }
}

Open in new window

0
mega-scorpion
Asked:
mega-scorpion
  • 7
  • 5
  • 2
2 Solutions
 
CEHJCommented:
Make that
 return " cell[ : "+this.num + " ] = : " + new String(gene);

Open in new window

0
 
mega-scorpionAuthor Commented:
it doesn't work
0
 
CEHJCommented:
What does 'doesn't work' mean?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
mega-scorpionAuthor Commented:
return " cell[ : "+this.num + " ] = : " + new String(gene);

nothing prints in the console
0
 
CEHJCommented:
Change main to
import java.util.Scanner;
public class cell {
    static int num;
        char gene[] = new char[20];
        char[] genetype = {'T','g','c','a'};    
        cell(){
                for ( int i = 0 ; i < 20 ; i++){
                        gene[i] = genetype[(int)(Math.random() * 4)];
                }
        ++num;          
        }
        ///
        public String toString(){
        
                return (" cell[ : "+this.num + " ] = : " + this.gene );
        }
        ///
    public static void main(String[] args) {
        cell c[] ;
                c = new cell[20];
        for ( int i = 0 ; i < 10 ; ++i){
                c[i] = new cell();
        }
        
                Scanner input = new Scanner(System.in);
                int n = input.nextInt();
                c[n].toString();
                
    }
}

Open in new window

0
 
mega-scorpionAuthor Commented:
ohh sorry its works now  
i should print it first
System.out.print(c[n].toString());

could you solve me the second section of this question :)
0
 
CEHJCommented:
Bad past - ignore that
    public static void main(String[] args) {
        cell c[] ;
        c = new cell[20];
        for ( int i = 0 ; i < 10 ; ++i){
            c[i] = new cell();
            System.out.println(c[i]);
        }
    }   

Open in new window

0
 
mega-scorpionAuthor Commented:

variable num must be unique and cannot be changed number
how i could make it ?
0
 
objectsCommented:
>
c[n].num = 3;


should be:

cell.num = 3;

or to incremenet

cel.num++;

0
 
objectsCommented:
if you want it to nbe a constant then use:

    static final int num = 3;

0
 
CEHJCommented:
>>variable num must be unique and cannot be changed number


final long num = (long)(Math.random() * Long.MAX_VALUE);

Open in new window

0
 
CEHJCommented:
If it needs to be in sequence
final long num = cell.nextId(); // (Where nextId is a static method)

Open in new window

0
 
mega-scorpionAuthor Commented:
static final int num = 3;

C:\Documents and Settings\xp-user\cell\src\cell.java:13: cannot assign a value to final variable num
        ++num;  
          ^
1 error
0
 
CEHJCommented:
:-)
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 7
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now