• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 265
  • 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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

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