Home arrow static arrow Java Programming [Archive] - String toString ??
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - String toString ??
This topic has 13 replies on 1 page.

Posts:31
Registered: 6/26/04
String toString ??  
Jun 28, 2004 2:34 AM



 
I have created an array wich keeps counts how many times a certain mark is given:

private int[] frequenties = new int[10];
int telfreq = 0;

How do I use String toString to find out how many times a certain mark is given and put it in a frequencylist like:

frequency[1]=0
frequency[2]=0
frequency[3]=0
........
frequency[10]=0
 

Posts:2,830
Registered: 9/1/03
Re: String toString ??  
Jun 28, 2004 2:41 AM (reply 1 of 13)



 
How do I use String toString to find out how many
times a certain mark is given and put it in a
frequencylist like:

I have absolutely no idea.

A hashmap could be helpful.
 

Posts:1,135
Registered: 1/16/04
Re: String toString ??  
Jun 28, 2004 2:43 AM (reply 2 of 13)



 
I have created an array wich keeps counts how many
times a certain mark is given:

private int[] frequenties = new int[10];
int telfreq = 0;

How do I use String toString to find out how many
times a certain mark is given and put it in a
frequencylist like:

frequency[1]=0
frequency[2]=0
frequency[3]=0
........
frequency[10]=0

Can you please restate the problem, so far I understand the you have an int array and are trying to use String.toString() which returns a String to read marks and update the array ?

- MaxxDmg...

- ' He who never sleeps... '
 

Posts:31
Registered: 6/26/04
Re: String toString ??  
Jun 28, 2004 2:49 AM (reply 3 of 13)



 
the marks given are put into the array:

public void addMark(int newmark)  {  frequencies[countfreq++] = newmark;


With String toString() I need to fill a list (freqList) like the example

frequency[1]=0
frequency[2]=0
frequency[3]=0
........
frequency[10]=0
 

Posts:4,000
Registered: 24.02.01
Re: String toString ??  
Jun 28, 2004 3:16 AM (reply 4 of 13)



 
Just use a loop:
for (int i = 0; i < array.length; i ++) {    freq[array[i]] ++;}
(you need to create freq first)
 

Posts:1,135
Registered: 1/16/04
Re: String toString ??  
Jun 28, 2004 3:24 AM (reply 5 of 13)



 
the marks given are put into the array:

public void addMark(int newmark){frequencies[countfreq++] = newmark;


With String toString() I need to fill a list
(freqList) like the example

frequency[1]=0
frequency[2]=0
frequency[3]=0
........
frequency[10]=0

Okay, I think I know where the confusion is...

frequencies is an array of integers...
int [] frequencies = new int[10];// array of int with 10 elements indexed from 0-9

and frequency is an array or list of Strings...
String [] frequency = new String[10]; // array of String with 10 elements indexed from 0-9

and you are wonder how to use Integer.toString(int n) method...
frequency[0] = Integer.toString(frequencies[0]);

Is this what you are asking for ?

Also note, that both arrays have 10 elements, and arrays are indexed starting at 0. So the range is 0 to 9...

- MaxxDmg...

- ' He who never sleeps... '

 

Posts:31
Registered: 6/26/04
Re: String toString ??  
Jun 28, 2004 3:30 AM (reply 6 of 13)



 
Is freq a method?
 

Posts:4,000
Registered: 24.02.01
Re: String toString ??  
Jun 28, 2004 3:38 AM (reply 7 of 13)



 
No, that's your array. I'm not giving you the complete source, you know - for you to get your homework done, you need to do something yourself too.
 

Posts:31
Registered: 6/26/04
Re: String toString ??  
Jun 28, 2004 3:45 AM (reply 8 of 13)



 
This what I have made of the class FrequentieTabel so far
package frequentie_tabel; public class FrequentieTabel{   //attibuten  //array van maximaal 10 elementen  //default waarde is 0  private int[] frequenties = new int[10];  int telfreq = 0;   //constructor  public void voegToe(int nieuwcijfer)  {  frequenties[telfreq++] = nieuwcijfer;  }   //volgens mij is deze berekening gemiddelde goed  public double berekenGemiddelde()  {     double som=0;     for (int i=0; i < frequenties.length; i++)           som+=frequenties[i];     return som/telfreq;  }   //geeft het percentage voldoendes (6 en hoger)  public double berekenPercvold()  {     int teller=0;     for (int i=0; i < frequenties.length; i++)        if (frequenties[i] > 5)           teller++;     return teller * 100.0 / telfreq;  }   //om alle waarden van de array uit te lezen  public String toString()  {   frequenties[0] = Integer.toString(frequenties[0]);  } }
 

Posts:31
Registered: 6/26/04
Re: String toString ??  
Jun 28, 2004 3:54 AM (reply 9 of 13)



 
I'm really desperate. I have never done a lot of programming and this is the final assignment.
 

Posts:2,830
Registered: 9/1/03
Re: String toString ??  
Jun 28, 2004 3:58 AM (reply 10 of 13)



 
I have never done a lot of
programming

No kidding.

I'm really desperate.

If you are so desperate, do some research, read the reply's you
have been given, and come up with an answer - this is a trivial
task for a 'final assignment', if you can't come up with the answer
yourself, or with the guidence of your lecturer then you do not
deserve to pass.

Research around - you will find the answer - you have been
given it in this thread, in 2 different ways at least.
 

Posts:31
Registered: 6/26/04
Re: String toString ??  
Jun 28, 2004 4:00 AM (reply 11 of 13)



 
I used to like Ausiies a lot. (just kidding) and was over 2 times.
I will study the answers given.
Thanks
 

Posts:1,135
Registered: 1/16/04
Re: String toString ??  
Jun 28, 2004 4:20 AM (reply 12 of 13)



 
This what I have made of the class FrequentieTabel so
far
package frequentie_tabel; public class FrequentieTabel { //attibuten //array van maximaal 10 elementen//default waarde is 0private int[] frequenties = new int[10];int telfreq = 0;  //constructorpublic void voegToe(int nieuwcijfer) {frequenties[telfreq++] = nieuwcijfer; }  //volgens mij is deze berekening gemiddelde goedpublic double berekenGemiddelde() { double som=0;for (int i=0; i < frequenties.length; i++)som+=frequenties[i];return som/telfreq;}  //geeft het percentage voldoendes (6 en hoger) public double berekenPercvold(){int teller=0;for (int i=0; i < frequenties.length; i++)if (frequenties[i] > 5)teller++;return teller * 100.0 / telfreq;} //om alle waarden van de array uit te lezenpublic String toString(){frequenties[0] = Integer.toString(frequenties[0]);}}

Okay, this is now starting to make sense...

This will give you errors...
public String toString(){frequenties[0] = Integer.toString(frequenties[0]);// give error}}

The reason it will give you an error is that you are attempting to assign a String to and int...

But now that I can see that what you are doing is creating\overriding a toString methods...

I am going to venture the guess that you want to return a String that contains all the marks...

So I think this may be what you are after, but correct me if I am wrong...
public String toString(){StringBuffer theFrequenties = new StringBuffer();for (int i = 0; i < frequenties.length; i ++){          if ( i !=0){          theFrequenties.append(" "); only adds a space after the first iteration...          }          theFrequenties.append(frequenties[i]);}return theFrequenties.toString}}

this returns all marks in one single String with spaces between elements
or you could do this...
public String toString(){  return java.util.Arrays.toString(theFrequenties); // returns a string of all elements in array }}

and this second one returns a String with brackets "[" "]" at the beginning and end of the String with each element seperated by a comma "," and space...

Hope this helps...
And you can see why including some code can help us give you better answers...

- MaxxDmg...

- ' He who never sleeps...'
 

Posts:450
Registered: 6/5/03
Re: String toString ??  
Jun 28, 2004 4:21 AM (reply 13 of 13)



 
ma küll päris täpselt ei saa aru mida sa seal koodis oled üritanud teha (sest sinna on pisut raske süveneda, seoses võõrkeelsete muutuja/meetodi nimedega), aga võimalik et sa tahad midagi sarnast tehtud saada:
class ... {  ...  public void tihedusteArvutaja(int[] hinded) {    hindeidKokku = hinded.length; // see on globaalne muutuja    for (int i = 0; i < hinded.length; i++) {      tihedused[hinded[i]]++;    }  }  public String tihedusedStringina(int[] tihedused) {    StringBuffer puhver = new StringBuffer(30); // 30 on lihtsalt suva pakkumine    for (int i = 0; i tihedused.length; i++) {      puhver.append((float)tihedus[i] / (float)hindeidKokku).append(" ");    }    return puhver.toString();  }}

tuletab natuke meelde seda koodi mis Joss kuskilt veebist leidis ning mis oli jaapani keelne.
and looking at that, i realized that we all should programm in english.

BTW, that toString method, returns only first frequency, if you would concatanate those frequencies into one larger string, then you could return it all at once, much like i'm doing in my frequenciesAsAString() method.
 
This topic has 13 replies on 1 page.