Welcome to Dream.In.Code
Become a Java Expert!

Join 149,584 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,777 people online right now. Registration is fast and FREE... Join Now!




Need help printing array!

 
Reply to this topicStart new topic

Need help printing array!

JackSkell26
8 Oct, 2007 - 01:42 PM
Post #1

New D.I.C Head
*

Joined: 24 Sep, 2007
Posts: 19


My Contributions
Hello All, (I made some changes to the program trying to use a void method instead of boolean.) This is just the foundation for a program that I am working on. The program is to ask the user to enter the amount of strings they would like in an array. The user then enters the array of strings. The program is then suppose to remove any words/strings containing "ch" and print them backwards. Right now I'm only searching for the letter 'A' though, since I currently can't remember how to search for two adjacent letters. The program won't compile because the Remove method sees x[i] as an int and not an array.

CODE


import java.util.*;

public class CH2 {

    public static Scanner in = new Scanner (System.in);

    public static void main (String[] args) {

        String[] x = GetArray ();

    }

    public static String[] GetArray() {

        int sz = -1;
        while (sz<=0)

        {System.out.print ("How many strings do you want in the array? ");
        sz = in.nextInt();}

        in.nextLine();
        String[] array = new String [sz];

        for (int i = 0; i < sz; i++) {
               System.out.print ("Enter string "+(i+1)+":  ");
               array[i] = in.nextLine();

         }
         return array;
     }

         public static void Remove (String[] s) {

             for (int x = 0; x < s.length; x++) {
            for (int i = 0; i < s[x].length(); i++)
            if (s[x].charAt(i) != 'A')
            System.out.println (x[i]);
            else
            System.out.print ("");}

}

}



This post has been edited by JackSkell26: 8 Oct, 2007 - 05:08 PM
User is offlineProfile CardPM
+Quote Post

spullen
RE: Need Help Printing Array!
8 Oct, 2007 - 07:24 PM
Post #2

D.I.C Regular
Group Icon

Joined: 22 Mar, 2007
Posts: 330



Thanked: 1 times
Dream Kudos: 50
My Contributions
x[i] is an int that's why it is throwing that error.
If you printed out s[x].charAt(i) that would work. You have 2 loops where the first loop is using x as an int to index the array s and since x is an int you can't index it like an array.
User is offlineProfile CardPM
+Quote Post

JackSkell26
RE: Need Help Printing Array!
8 Oct, 2007 - 08:14 PM
Post #3

New D.I.C Head
*

Joined: 24 Sep, 2007
Posts: 19


My Contributions
Thank you for a response, I made this change to the program and it now compiles and works (sort of). However, when the program prints out the array, it prints out one letter at a time skipping ONLY the charAt letter (in this case 'A') and not the entire word. Example - when two words are entered, Hello, Apple. it prints

H
e
l
l
o
p
p
l
e

If anybody could help with this, or help me figure out a way to search for 'ch' instead of just one letter at a time...it would be greatly appreciated.
User is offlineProfile CardPM
+Quote Post

JackSkell26
RE: Need Help Printing Array!
8 Oct, 2007 - 09:21 PM
Post #4

New D.I.C Head
*

Joined: 24 Sep, 2007
Posts: 19


My Contributions
Edit: Posting New Topic On Forums.

This post has been edited by JackSkell26: 9 Oct, 2007 - 01:22 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 10:52PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month