How to get the length of the array equal to the length of a string?

Viewed 112

I hope I can get my point across in this.

  • In the isoddeven function, I count the number of letters or digits/special symbols depending on, whether the length of the variable n is odd or even. Then I take the count variable.

    And return its value, so that I can use it as the length for the char array.

When I run my code, it brings out a total number that exceeds the total of the actual count(it doubles). I can't use regrex in this part, it needs to be a loop that counts.

Really need some advice on anything I should add. Or is there anything that's actually wrong with my code?

This is the output i want :

    enter a string :
    rain!21
    length is odd, there are 3 non-alphabetic characters in the string
    enter 3 strings in the array
    **********

the output i get instead :

    enter a string :
    joy!@u77
    length is even, there are 4 aplabetic characters in the string
    **********
    enter 8 strings in the array
    **********

Here is my code :

    import java.io.*;
    public class Stringarray {
        BufferedReader input = new BufferedReader (new InputStreamReader (System.in));
        String n;
        String arr[] = new String [4];
        int c = 0, c2 = 0, i;
        char ch;
        
         public static void main (String args[]) throws IOException {
             Stringarray obj = new Stringarray ();
             
             obj.Constructor ();
             obj.stringInput ();
             obj.IsOddEven ();
             obj.createArray ();
             obj.replaceDigit ();
         }//main
         
         public void Constructor () {
             System.out.println("your name and section here ");
         }//constructor
         
         public int stringInput () throws IOException {
             System.out.println("enter a string :");
             n = input.readLine();
            return n.length();
         }//stringInput
         
         public int IsOddEven () {
             if (n.length() % 2 == 0) {
                 for ( i = 0; i<n.length(); i++){
                     ch = n.charAt(i);
                     if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' ){
                     c++;//letters
                     }
                 }
                 System.out.println("length is even, there are " + c + " aplabetic characters in the string");
             }
             else {
                 for (i = 0; i<n.length(); i++){
                     if (ch >= '0' && ch <= '9'){
                         c++;//digits
                     }
                     else {
                         c2++;//special characters
                     }
                 }
                 System.out.println("length is odd, there are " + c+c2 + " non-alphabetic characters in the string");
             }
             int x = c + c2;
             System.out.println("**********");
             return x;
         }//IsOddEven
         
         public void createArray() throws IOException {
             System.out.println("enter " + n.length() +" strings in the array\n**********");
             for (int i = 0; i<n.length(); i++){
                 n = input.readLine();
                 arr[i] = n;
             }
             System.out.println("**********");
         }//createArray
         
         public void replaceDigit () {
             System.out.println("all digits in the array are replaced with #");
             for (int i = 0; i<arr.length; i++){
                 arr[i] = arr[i].replaceAll("[^a-zA-Z!]", "#");
                 System.out.println(arr[i]);
             }
         }//replaceDigit
         
    }

2 Answers

Your code is a nice example of a sphagetti code and there are too many issues to list them all. I would advise you to read 'Clean Code' - it will definitely help you to improve your coding skills.

Regarding the original question, look at your method createArray. It uses n.length() instead of your counters, i.e. the length of the input line. I assume that's not what you actually want from it.

Your error is at System.out.println("enter " + n.length() +" strings in the array\n**********");. You are using n.length() for how many it should add, so in the case of joy!@u77 the length is 8 casing it to add 8 to the array. Judging by your code, I think you would want to do "enter " + x +" strings.... Not sure if your storing it in x, but the problem lies at n.length().

Related