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,784 people online right now. Registration is fast and FREE... Join Now!




Printing IndexOf

 
Reply to this topicStart new topic

Printing IndexOf

JackSkell26
9 Oct, 2007 - 01:26 PM
Post #1

New D.I.C Head
*

Joined: 24 Sep, 2007
Posts: 19


My Contributions
This Program is suppose to print any strings that contain "ch" in the user entered array. However .... my program does quite the opposite, it only prints strings that do not contain "ch". What can I do to change this? I'm sure the program is here, if (s[x].indexOf("CH") == -1). However, when I try different values it does not print anything.

CODE


import java.util.*;

public class CH {

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

    public static void main (String[] args) {

       String[] x = GetArray ();

       Remove (x);

    }

    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 = s.length -1; x >=0; x--) {

            if (s[x].indexOf("CH") == -1)
            if (s[x].indexOf("Ch") == -1)
            if (s[x].indexOf("cH") == -1)
            if (s[x].indexOf("ch") == -1)

           {System.out.println (s[x]);}


        }

    }

}




User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Printing IndexOf
9 Oct, 2007 - 02:49 PM
Post #2

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Hi, I made some obvious changes you'll see them right away in the code below. I only used the .contains() method.

HERE and Enjoy! smile.gif

CODE
import java.util.*;

public class CH {

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

    public static void main (String[] args) {

       String[] x = GetArray ();

       Remove (x);

    }

    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 = s.length -1; x >=0; x--)
            {

            if (s[x].contains("CH")||s[x].contains("Ch")||s[x].contains("cH")||s[x].contains("ch"))
            {
                System.out.println (s[x]);
              }


        }

    }

}


User is offlineProfile CardPM
+Quote Post

JackSkell26
RE: Printing IndexOf
9 Oct, 2007 - 03:33 PM
Post #3

New D.I.C Head
*

Joined: 24 Sep, 2007
Posts: 19


My Contributions
Thank You, Thank You, Thank You! I didn't even think of the contains command ... tongue.gif
User is offlineProfile CardPM
+Quote Post

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

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