0

I am trying to change the information in create a new character array of Str without the whitespaces. However, I cant succeed in doing sp

I dont know why the output says:

Haveaniceday
Have a nice day

instead of:

Haveaniceday
Haveaniceday

import java.util.*;
class DelExtraSpace
{
    public static void main(String args[])
    {
        char c[],s[];
        String str; 
        Scanner scn = new Scanner(System.in);

        str = "Have  a nice   day";

        c = str.toCharArray();
        s = new char [c.length];

        for(int i = 0; i < c.length; i++)
        {
            if(!Character.isSpaceChar(c[i]))
            {
                System.out.print(c[i]);
                s[i] = c[i]; // I want the value of c[i] to be assigned to s[i] only when c[i] is not a whitespace
            }               
        }
        System.out.println();
        for(int j = 0; j < s.length; j++)
            System.out.print(s[j]);
    }
}
6
  • 3
    Your quesiton is not clear. What is your code supposed to do and what is the output you expect ? Commented Aug 24, 2014 at 23:10
  • I was expecting that System.out.println(); for(int j = 0; j < s.length; j++) System.out.print(s[j]); Should have printed: Haveaniceday Haveaniceday Commented Aug 24, 2014 at 23:17
  • Sorry I am new here and have some issues with formatting my replies Commented Aug 24, 2014 at 23:21
  • Post code as an edit to your question since code does not format well in comments. Commented Aug 24, 2014 at 23:26
  • I ran your code here ideone.com/PN8BeJ and it appears to output your expected output. Commented Aug 24, 2014 at 23:32

1 Answer 1

1

I suppose that you are wondering why s is not changed.

In this case, try this:

public static void main(String args[]) {
    char c[], s[];
    String str;
    Scanner scn = new Scanner(System.in);

    str = "Have  a nice   day";

    c = str.toCharArray();
    s = new char[c.length];

    int ii = 0;                                 // ADDED
    for (int i = 0; i < c.length; i++) {
        if (!Character.isSpaceChar(c[i])) {
            System.out.print(c[i]);
            s[ii] = c[i];                       // CHANGED                              
            ii++;                               // ADDED
        }
    }
    System.out.println();
    for (int j = 0; j < s.length; j++)
        System.out.print(s[j]);
}

I added 3 comments for the modified/added lines.

Now the output should be:

Haveaniceday
Haveaniceday

Short explanation

Obviously, s will be shorter than c (because it doesn't contain those spaces). This is why you need a new contor (ii) instead of i.

  • ii takes values from 0 to 11
  • i takes values from 0 to 17
ii -> i
0 -> 0
1 -> 1
2 -> 2
3 -> 3
                 // 4 and 5 are indexes for spaces => they are ignored
4 -> 6           
                 // 7 is index for space => it is ignored
5 -> 8            
6 -> 9
7 -> 10
8 -> 11
                 // 12, 13 and 14 are indexes for spaces => they are ignored
9 -> 15
10 -> 16
11 -> 17

E.g.: When 4 and 5 were ignored, ii was not incremented because we don't need "gaps" in the final array, only the values that are not space.

Sign up to request clarification or add additional context in comments.

2 Comments

Some explanation would be nice, I suppose.
Yes Curiosu, if you could also explain why I didnt get the desired output with my code on my machine but on ideone.com/PN8BeJ i get the same output that I was looking for

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.