-2

I've tried "\n\n" and "\r" and everything else, including replaceAll("\r\n", "n") and I still do not understand why it doesn't work. I've also tried "\w", "\n", "\n+" - I've basically tried everything under "My split("\n") doesn't work" on Google search.

I'm trying to split a word with a lot of "\n". I basically have two different classes. One generates this word, and via the other class constructor object transfers it into the split("\n") method. But whatever I do, the array still stays empty.

I've also tried word.split(System.getProperty("line.separator")) even though I didn't have a clue as to what it meant, but it also came up under one of the solutions to this problem.

Here's my Code:

//in Class A

public String getWord()
{
    word = word +"\n" +  horizontal;
    return word;
}

//in Class B

classA a = new classA();
String grid = a.getWord();
String [] lines = grid.split("\n");

EDIT: Sorry, typo mistake, I'll just ask again later. I did actually put grid.split("\n") in my code. What now? The array really is empty. I did System.out.println(array.length) and it was 0. Also, I typed System.out.println("array is " + array) and it only gave me "array is" as output. I know I'm making a stupid mistake somewhere, and I know I can't expect people to answer my question if I don't know what info to provide.

I also wanted to add some stuff in the comments section here for the comfort of those sitting in front of their laptops...

word and horizontal is a string. It's actually a crossword puzzle together. See? Look!

LONDONPYVRAOMNDDEFSG
GCPZVBATHYXAZXEZIMOZ
NKDGBERLINCHPLTMHMSM
ZMUKPGCHRKDTYGIMRLHO
TVRWBXPRETORIAJBVKWT
OGIVSDFULULHQHAHEJNV
PNWEJHBAKBJZNBPARIS
PHKCZCYGTXEEXDUCPMXF
QIMQMABRASILIALJOFJQ
GXNXKTAHIQMMIFPSYDLI
CAIROYKZYSWEFPUZPKRG
BTNAUNIDQAYVYAPGWWIN
QXZMQSZBTCBEIJINGBSD
QWQRYTBPTKRBCJUOMJTV
SODHAMSTERDAMEMSLVAM
YQHEVNXQQJXCDZKEYQVT
NAIROBISVDNTCFJNYDEG
AKXVOIGYTZTJHGIAFIKZ
BAGHDADSADJTWOOMVRYT
YCPOBXQQMQKBTDMYPYWT

It's city names. At the end of this, I'm supposed to show the solution to the puzzle by changing cases. I know how to do this, but the problem is that I can't seperate them into lines anymore. I don't know why. That's my only problem here. It seems to work for everyone, except for me.

Answers with clues will be appreciated? To delve into a dark and deep mystery...

Dennis Meng
  • 5,109
  • 14
  • 33
  • 36
breakstorm
  • 9
  • 1
  • 7
  • What is the initial value you're giving `word`? – T.J. Crowder Feb 02 '14 at 13:30
  • 1
    *"But whatever I do, the array still stays empty."* I very much doubt that. The array may have a *single* entry in it, but it won't be empty. – T.J. Crowder Feb 02 '14 at 13:31
  • 1
    The code you've shown doesn't make any sense. For one thing, you're calling `split` on `a` (an instance of `classA`), not on `grid` (the variable referencing the string returned by `a.getWord`). – T.J. Crowder Feb 02 '14 at 13:32
  • 2
    You call `a.split()` but `a` is not a string. What is the split implementation in `a`? – Tim B Feb 02 '14 at 13:32
  • If yours was a typo and you are calling `split` on `grid` instead of `a`, then your example should work: [IDEone](http://ideone.com/97eTKD) – BackSlash Feb 02 '14 at 13:33
  • Does the \n still carry the same value if it is transferred to another class? Because the split ("\n" really doesn't seem to work the way it should.... – breakstorm Feb 02 '14 at 14:07
  • "I've also tried word.split(System.getProperty("line.separator")) even though I didn't have a CLUE as to WHAT it meant, but it also came up under one of the solutions to this problem." Please don't do that. Adding code that you don't understand to a problem you can't solve will only give you a second problem to solve. – Dennis Meng Feb 02 '14 at 18:40
  • Also, I've edited the post for you. Try to keep comments as comments and not put too much commentary in the question itself (if it's not directly related to the problem, it'll only make it harder for us to see exactly what you're looking for). Thanks for editing instead of creating a new question though; that's *exactly* the right way to go about it. – Dennis Meng Feb 02 '14 at 18:50
  • I know... But that's my way of learning ... Or panicking... or whatever :) Thanks for your tip, Dennis ^^ I still have a long way to go 0_~ Including interacting with my peers on StackOve ^^ – breakstorm Feb 02 '14 at 18:50

3 Answers3

1

It should be

grid.split("\n");

not

instance.split("\n")
Maroun
  • 94,125
  • 30
  • 188
  • 241
Girish
  • 1,717
  • 1
  • 18
  • 30
  • It was actually a typo on my part >< I did actually do grid.split("\n") in my code. I didn't copy paste it...darn! My bad :) – breakstorm Feb 02 '14 at 13:50
  • But since you get points if I mark it correct, and you took the time to read my post, I'll just do it ^^ I'm going to delete my question anyway- once it lets me ^^ – breakstorm Feb 02 '14 at 13:50
  • I'll probably come up later and ask my question again, since I still have the prob though. Once I know which info to provide you guys with, and I'm SURE I DIDN'T MAKE ANY MISTAKES, I'll post again -__- You guys are quite fast answering, you know that? :D – breakstorm Feb 02 '14 at 13:52
  • I know I marked it correct, but I still wish I knew WHY my code won't work... :'( Has anyone else actually TRIED this before? – breakstorm Feb 02 '14 at 14:05
  • @breakstorm please read the javadocs of split you was calling it on object – Girish Feb 02 '14 at 14:07
  • I did fix the typo didn't I ? I did do it on the String, I just didn't type it on the website here correctly >< And I meditated on the javadocs- just so you know. To brace myself for the invisible critics on the internet... – breakstorm Feb 02 '14 at 14:19
  • I'm just unticking for now, so that people will still be encouraged to answer :D I need more clues as to what went wrong.At the end of the day, I'll still tick it right back ^^ – breakstorm Feb 02 '14 at 14:22
0

Call grid.split("\n");

You can't split a class.

Better a.getWord().split("\n");

Roberto Anić Banić
  • 1,411
  • 10
  • 21
0

In your code there isn't no method named split , also your didn"t call your method getword inside System.out.println() .... First Class :

public class A {
public String returnedWord ="";

   public String getWord(String word , String horizontal)
      {
           returnedWord = word +"\n" +  horizontal;
           return returnedWord ;
      }


}

the Second Class :

public class B {

public String word = "Hello";
public String horizontal = "World";
public static void main (String [] args ) {


    A a = new A();
    System.out.println(a.getword(word,horizontal));

}

}

you will get the output below :

Hello 
World
  • But the thing is that it works like that in any case if you do "\n"...What I wanted to do was to seperate it into lines as strings or whatever... – breakstorm Feb 02 '14 at 13:54
  • Like if my "grid" is "something\nsomething\nsomething" and I do split("\n") then it would store it as an array [something, something, something] :) You get me? – breakstorm Feb 02 '14 at 13:55
  • so you want to manipulate any object or component , but your first question is about String not object or component !! – Karim Massi Feb 02 '14 at 15:08
  • No, literally! I didn't come here to fight :/ My question was why isn't my split ("\n") not working. That was all I wanted to know. I don't mean to "manipulate any object or component", whatever that means. Because I'm still dealing with a string, if anything- I'm just creating an array from the string....Well, let's stop fighting ^^ This isn't helping me... much – breakstorm Feb 02 '14 at 18:38
  • that nice :) , i not in fight too :) , try to see these solution in this topic : [Check if string contains \n Java ](http://stackoverflow.com/questions/5518451/check-if-string-contains-n-java) good luck – Karim Massi Feb 02 '14 at 20:14