views:

188

answers:

4

hi, basically id like a few hints or tips on how to solve this question.. maybe a few things which i could read up on about arraylists and loop which would make it simple for me to understand!..

the question is :

Processing an ArrayList of Characters: cList is an ArrayList of objects of type Character that has been declared and intialised. Write a loop that will count the Characters that are not spaces and print the number out to the terminal window.

and second question would be:

Looping through a String

Assuming that a variable has been declared like this:

String s;

and that a value has already been assigned to s, write a loop statement that will print the characters of s in reverse order (so if s = "HELLO", your loop should print "OLLEH").

for the first question i tried to do:

public ArrayList()
public static void main(String[] args) {
    int countBlank;
    char ch;
public ArrayList (int cList)      
{
    int cList = ;
    while(cList ){
        System.out.println(cList);
        }
        }

and second question :

i have no idea, but a read up would be great!

thank you!

+2  A: 

You could start by reading up on ArrayList Javadoc and documentation on mindprod.

In your example you haven't declared cList as arraylist nor filled it with Character objects. Instead of a while you might look into for. To get the characters in a String, String.toCharArray() might be of use.

rsp
A: 

An arraylist is probably too fat for that:

import java.text.CharacterIterator;
import java.text.StringCharacterIterator;

public class CI {
    private static final String text = "Hello";

    public static void main(String[] args) {
        CharacterIterator it = new StringCharacterIterator(text);

        for (char ch = it.last(); ch != CharacterIterator.DONE; ch = it
                .previous()) {
            System.out.print(ch);
        }
    }
}
stacker
Oops it was retagged as homework, I'm sorry to give a ready to run example
stacker
A: 

Answer to Question 1: import java.util.*;

public class ProcessListOfChars{

    public static int printCharsInListIgnoreSpaces(List<Character> cList){
            int count =0;
            for(Character character:cList){
            //      System.out.println("Value :"+character);
                    if(character!=null &&!character.toString().trim().equals("")){
                            ++count;
                    }
            }
            return count;
    }

    public static void main(String... args){
            List<Character> cList = new ArrayList<Character>();
            cList.add('c'); //Autoboxed char to Charater Wrapper Class
            cList.add(' '); //Space character
            cList.add('r');
            cList.add('b');
            cList.add(' ');
            int count = printCharsInListIgnoreSpaces(cList);
            System.out.println("Count of Characers :"+count);
    }

}

Rishi
hope this solves the first problemsecond one is pretty simpler and i want you to think about it logically.
Rishi
thank you for your help, but the compilation gives erros such as : Test.java:9: illegal start of expression import java.util.ArrayList; ^Test.java:10: illegal start of expressionpublic class ProcessListOfChars{^Test.java:10: ';' expectedpublic class ProcessListOfChars{ ^3 errors
Kumar
+1  A: 

For your first question, you want to loop through the list and count the number of times a character isn't a space.

int counter = 0;
for (Character c : characters) {
    if (c == null {
        continue; // Note: You can have null values in an java.util.ArrayList
    }
    if (!Character.isSpaceChar(c)) {
        counter++;
    }
}
System.out.println(counter);  

For your second question:

for (int i = s.length() - 1; i >= 0; i--) {
    System.out.print(s.charAt(i));
}
mangoDrunk