# Thread: plz help me in this Problem>>

1. Member
Join Date
Dec 2009
Posts
3
Rep Power
0

## plz help me in this Problem>>

Hi .. :)

i am new in java field,
i have a question and i couldn't solve it!

the question is:

proplem: Counting
Complete the function TotalChars. TotalChars returns the total number of occurrences of the character ch in each string in list. You should use the string member function to find the number of characters in a string and s[k] for the value of the kth character (indexing starts at 0).

e.g. information technology : i=1, n=2, f=1, o=4, r=1, m=1, a=1, t=1, e=1,
c=1, h= 1, l= 1, g=1, y=1
You should write this function either recursively or iteratively (non-recursively).

plz if somebody can help me to write this code in simple way with eclips or drjava..

& thank you

2. Let's see what you can do first followed by more specific questions and then we'll better be able to help you. Much luck!

3. Member
Join Date
Feb 2008
Posts
78
Rep Power
0
If this is a school assignment, your teacher must have taught you something before given out an assignment. So, start by putting together what you were taught and if you get stuck come with further questions.

4. I find it easier to write a program after I walk through the steps first. So, instead of trying to write code that does the task, how would you do the task, as a human?

Given a phrase, for example "Java Forums", how would you go about counting how many times the each character occurs?

5. Member
Join Date
Dec 2009
Posts
3
Rep Power
0
Hi again :)

this is my answer for the question in attachment..

plz , can somebody check it :)

thanks

6. It would be better if you posted code in the forum with code tags, and then asked much more specific questions regarding your code. Do this and you'll have a much better chance of getting helpful help.

Much luck!

7. Member
Join Date
Feb 2008
Posts
78
Rep Power
0
Hi,

1) Copy all your java code
2) Type 3)
Java Code:
` paste the java code you copied here`
4) Finally, copy everything the way they look in step 3 and past it on the forum for people to read and response to.

I hope this helps.
Menre

8. Member
Join Date
Feb 2008
Posts
78
Rep Power
0
I have noticed that the forum board interpreted my code tags as html tags. So, my previous posting will be difficult for you to understand.

Type below.

1) Copy all your java code
2) See the image attached
3) See the image attached
4) Finally, copy everything the way they look in step 3 and past it on the forum for people to read and response to.

Another option.
5) You can also use the html code tag which is one of the features available on the message board. To use this feature and follow the steps I explained in 1-4 above, see the image that I have attached to this message.
I put a red circle around the icon to generate the code tag. I pointed a line to it and called it Step 5. To use that icon, click on it to generate the opening and closing code tags. Then, copy your java code and paste it between the opening and closing code tags. If you do that, we will be able to read your Java code on the forum.

I hope this helps.

Menre

9. Member
Join Date
Feb 2008
Posts
78
Rep Power
0
Hi,
I do not know about other members on the forum. I tried to download and open your Zip file, but it was not successful. I am working on a Mac, but don't know if it will be successful on a Windows platform.
Menre

10. Member
Join Date
Dec 2009
Posts
3
Rep Power
0
sorry for the late replay

this is the code in 3 separate classes :

Java Code:
```/* This class implements doubly linked lists.
* @author  Empty Heart*/

public class DLL<E> {

/* A reference to the first node in this DLL. */
private DLLNode<E> first;

/* A reference to the last node in this DLL. */
private DLLNode<E> last;

/*Construct an empty DLL.*/
public DLL() {
first = null;
last = null;

}

/* Return the first element in this DLL, or null if DLL empty.
* @return  The first element in this DLL, or null if DLL empty.*/
public E getFirst() {
return first != null ? first.element : null;
}

/* Return the last element in this DLL, or null if DLL empty.
* @return  The last element in this DLL, or null if DLL empty.*/
public E getLast() {
return last != null ? last.element : null;
}

/* Return the number of nodes in this DLL.
* @return  The number of nodes in this DLL.*/

public int size() {
int size = 0;

for (DLLNode<E> curr = first; curr != null; curr = curr.next) {

size++;
}
return size;
}

/* Add an element to the start of this DLL.
* @param elem  An element to add to the start of this DLL.*/
insert(elem, null);
}

/* Add an element to the end of this DLL.
* @param elem  An element to add to the end of this DLL.*/
// implement this method (only takes one line of code)
insert(elem,this.last);
}

/* Add a new element after the element 'prev' (or at the end of this DLL if
* 'prev' isn't found).
* @param elem  A new element to add to this DLL.
* @param prev  An element to add 'elem' after.*/
public void addAfter(E elem, E prev) {

// implement this method (start by searching for prev)
DLLNode<E> prevNode = search(prev);
if (elem=="i" && elem !=prev ) insert(elem,prevNode);
if (elem=="n" && elem !=prev ) insert(elem,prevNode);
if (elem=="f" && elem !=prev ) insert(elem,prevNode);
if (elem=="o" && elem !=prev ) insert(elem,prevNode);
if (elem=="r" && elem !=prev ) insert(elem,prevNode);
if (elem=="m" && elem !=prev ) insert(elem,prevNode);
if (elem=="a" && elem !=prev ) insert(elem,prevNode);
if (elem=="t" && elem !=prev ) insert(elem,prevNode);
if (elem=="e" && elem !=prev ) insert(elem,prevNode);
if (elem=="c" && elem !=prev ) insert(elem,prevNode);
if (elem=="h" && elem !=prev ) insert(elem,prevNode);
if (elem=="l" && elem !=prev ) insert(elem,prevNode);
if (elem=="g" && elem !=prev ) insert(elem,prevNode);
if (elem=="y" && elem !=prev ) insert(elem,prevNode);

}

/* Delete a given element from this DLL.
* @param elem  An element to delete from this DLL.*/
public void delete(E target) {

DLLNode<E> node = search(target);

if(node == null) return;

DLLNode<E> predecessor = node.prev;
DLLNode<E> successor = node.next;

if (node == this.first) {
this.first = successor;
successor.prev = null;
} else if(node == this.last){
predecessor.next = null;
this.last = predecessor;
} else {
predecessor.next = successor;
successor.prev = predecessor;
}

}

/* Return a string representation of DLL to allow System.out.println(aList).
* @return  A String representation of this DLL.*/
public String toString() {
String result = "[";
for (DLLNode<E> curr = first; curr != null; curr = curr.next) {
result += curr;
if (curr != last)
result += ", ";
}
return result + "]";
}

/* Return a string representation of all the elements in this DLL, in
* last-to-first order.
* @return  A String representation of this DLL in reverse order.*/

public String reverseString() {
String result = "[";
for (DLLNode<E> curr = last; curr != null; curr = curr.prev) {
result += curr;
if (curr != first)
result += ", ";
}
return result + "]";
}

/* Find the first Node which contains the given element.
* @param elem  The element to search for.
* @return The first Node containing the given element, or null if no Node
*         contains the element.*/

private DLLNode<E> search(E target) {
for (DLLNode<E> curr = first; curr != null; curr = curr.next) {
if (target.equals(curr.element)) {
return curr;
}
}
return null;
}

/* Insert a new element after the element 'prev'.
* If 'prev' is null then insert 'elem' at the front of this DLL.
* @param elem  A new element to insert into this DLL.
* @param prev  An element to insert 'elem' after.
* If 'prev' is null insert 'elem' at the start of this DLL.*/
private void insert(E elem, DLLNode<E> prev) {

DLLNode<E> newNode = new DLLNode<E>(elem,prev,null);

if (prev == null) {
newNode.next = this.first;
this.first = newNode;
} else {
newNode.next = prev.next;
prev.next = newNode;
}

DLLNode<E> successor = newNode.next;

if(successor == null) {
newNode.prev = this.last;
this.last = newNode;
} else {
newNode.prev = successor.prev;
successor.prev = newNode;
}
}

public void traverse() {

System.out.println("Traversing the list:");
System.out.print("First element=" + this.first);
System.out.println(". Last element=" + this.last);

System.out.format("%1\$10s %2\$10s %3\$10s\n","element","prev","next");
System.out.println("   -----------------------------------");

for (DLLNode<E> curr = first; curr != null; curr = curr.next)
System.out.format("%1\$10s %2\$10s %3\$10s\n",curr.element,curr.prev,curr.next);
}

} // end class DLL```
Java Code:
```/* @author  Empty Heart*/

/* A simple container class which holds an element, and references to
* previous and next Nodes.*/
public class DLLNode<E> {

/** The element this Node holds. */
public E element;

/** A reference to the Node before this one. */
public DLLNode<E> prev;

/** A reference to the Node after this one. */
public DLLNode<E> next;

/* Create a new Node with the given element, adjacent nodes.
* @param elem  The element this Node holds.
* @param prev  The Node which comes before this one.
* @param next  The Node which comes after this one. */
public DLLNode(E elem, DLLNode<E> prev, DLLNode<E> next) {
this.element = elem;
this.prev = prev;
this.next = next;
}

/* Return a string representation of this Node.
* @return A string representation of this Node. */
public String toString() {
return element.toString();
}

} // end class Node```
Java Code:
```/* @author  Empty Heart*/

public class DLLTester {

private DLL<String> dll = null;

public void createDLL()
{
this.dll = new DLL<String>();
}

{
int countI=0; int countN=0;int countF=0;int countO=0;int countR=0;int countM=0;int countA=0;
int countT=0;int countE=0;int countC=0;int countH=0;int countL=0;int countG=0;int countY=0;

System.out.println("total number of 'i' occurrences is = "+countI);
System.out.println("total number of 'n' occurrences is = "+countN);
System.out.println("total number of 'f' occurrences is = "+countF);
System.out.println("total number of 'o' occurrences is = "+countO);
System.out.println("total number of 'r' occurrences is = "+countR);
System.out.println("total number of 'm' occurrences is = "+countM);
System.out.println("total number of 'a' occurrences is = "+countA);
System.out.println("total number of 't' occurrences is = "+countT);
System.out.println("total number of 'e' occurrences is = "+countE);
System.out.println("total number of 'c' occurrences is = "+countC);
System.out.println("total number of 'h' occurrences is = "+countH);
System.out.println("total number of 'l' occurrences is = "+countL);
System.out.println("total number of 'g' occurrences is = "+countG);
System.out.println("total number of 'y' occurrences is = "+countY);
showStatus();
}

public void removeElements()
{

}

public void showStatus()
{
System.out.println("The list=" + this.dll.toString());

System.out.println("The length of the list=" + this.dll.size());

dll.traverse();
}

public static void main(String[] args) {

DLLTester dllTester = new DLLTester();
dllTester.createDLL();
dllTester.removeElements();
}
}```
sorry again and thanks for help Menre :)
Last edited by empty heart; 12-15-2009 at 05:43 PM.

11. One problem is your use of == to compare Strings. Instead you should use either the equals method or equalsIgnoreCase. If you're not sure why, please let us know and we can explain further.

OK, now I'm confused. I saw you were using if (variable == "..") here:
Java Code:
```	  if (elem=="i" && elem !=prev ) insert(elem,prevNode);
if (elem=="n" && elem !=prev ) insert(elem,prevNode);
if (elem=="f" && elem !=prev ) insert(elem,prevNode);
// ...```

But then I noticed in your method signature that elem is a parameterized object variable:
Java Code:
```   public void addAfter(E elem, E prev) {
// implement this method (start by searching for prev)
DLLNode<E> prevNode = search(prev);
if (elem=="i" && elem !=prev ) insert(elem,prevNode);
if (elem=="n" && elem !=prev ) insert(elem,prevNode);
if (elem=="f" && elem !=prev ) insert(elem,prevNode);
// ...```

So it's as if you're forcing elem to be a String object? Or am I missing something here?
Last edited by Fubarable; 12-15-2009 at 08:24 PM.

12. Member
Join Date
Mar 2011
Posts
11
Rep Power
0
hi, am a student working on java , i wanted to add my details on top of each of my classes AUTOMATICALLY

that is have:

/*
*@Last update 24/03/2011
*/
public class etc{}
could you help me out, thanks

Auto insert author details

hi, am a student working on java , i wanted to add my details on top of each of my classes AUTOMATICALLY

that is have:

public class etc{}
could you help me out, thanks

Auto insert author details
Hello nadissen, please do not hijack others threads, unless your question is not related with the original. And also please do not post the same message in multiple sub-forums. Choose the correct one before post. If you want to move the thread into a different sub-forum then contact one of our moderators.

Please pay your attention on that, you may banned otherwise in Java-Forums.

14. Member
Join Date
Mar 2011
Posts
11
Rep Power
0
hello, i didn't check on the previous codes since i found them quite long for a simple counter program, here is what me i wrote (Note: i made the program consider "A" to be equals to "a", that is NOT case sensitive)

Here my main

/**
* @since Mar 28, 2011
*
*/
import java.util.*;

public class String_counter {

public static void main (String args[])
{
System.out.print("Enter string: ");
Scanner input = new Scanner (System.in);
String data = input.nextLine().toLowerCase();

Vector<variable> character = new Vector<variable>();
int found;

for (int i = 0; i < data.length(); i++)
{
String temp = Character.toString(data.charAt(i));
found=-1;

for (int j = 0; j < character.size(); j++)
{
if ((character.get(j).getValue()).equals(temp)==true)
{
character.get(j).setFrequency( (character.get(j).getFrequency()) +1);
found=1;
break;
}
if (found==1)
break;
}

if (found==-1)
{
}
}

for (int i = 0; i < character.size(); i++)
{
System.out.println(character.get(i).getValue()+" "+character.get(i).getFrequency());
}
}
}
and here my object class

/**
* @since Mar 28, 2011
*
*/
public class variable {

public String value;
public int frequency;

public variable(String v, int f)
{
this.value = v;
this.frequency = f;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

public int getFrequency() {
return frequency;
}

public void setFrequency(int frequency) {
this.frequency = frequency;
}

}
If anything part seems not clear and require explanation, just ask :)