CAPTiNDANCE
CAPTiNDANCE

Reputation: 87

How do I split a string into an array

I am trying to split each line of a String read by my FileReader and BufferedReader. I am getting a syntax error at String [] x = Login.Split("");

What method can I use to split the String? I will be adding a method to see if the string matches made by another input, but for now I need to figure out how to split a string and still have each line Separate.
(I am trying to find another way to make a login since I have java 8 and cannot use sql and the ucanaccess didn't seem to help either ) I also attempted to use a delimiter, but couldn't find a good applicable example.

private void SubmitActionPerformed(java.awt.event.ActionEvent evt) {      
    try {
        FileReader FR = new FileReader("Login.txt");
        BufferedReader Login = new BufferedReader(FR);
        StringBuilder sb= new StringBuilder();

        String str;
        String [] x = Login.Split("");
        while ((str = Login.readLine()) !=null ) { 
            sb.append(str);
            sb.append("\n");
        }
        Login.close();
        FR.close();

        System.out.println(sb.toString());
        Scanner read = new Scanner(Login);

        } catch (Exception ex) {
            System.out.println("exception 2, " );
            ex.printStackTrace();
        }
    }  

Upvotes: 2

Views: 669

Answers (3)

Rudi Visagie
Rudi Visagie

Reputation: 40

I am not clear on what you want?

A : You want to put single seperate lines from your reader into an aray? Well as far as I know that the readline() method reads a line of text already.

or

B You want to split the words in the line in that case you can try to use this : String[] x = Login.trim().split("//s"); //The RegEx will account for more than one space between words and the trim() will cut of spaces at the start and end of the line if any.

BUT I can see that you are not reading a line into your reader before trying to split the words in the array. So it is trying to split something that doesn't have any data in it.

Note I'm just starting in Java but I hope this helps.

Upvotes: 2

user207421
user207421

Reputation: 310893

I am getting a syntax error at String [] x = Login.Split("");

No you aren't, you are getting a compiler error reading something like 'no such method'. That's not a syntax error.

Login (stupid name) is a BufferedReader, and BufferedReader doesn't have a Split() method. String on the other hand does have a split() method (note the case). Presumably that is what you're looking for.

But splitting on an empty string isn't going to accomplish anything useful.

Upvotes: 1

Scary Wombat
Scary Wombat

Reputation: 44834

Based upon your new comment

String str;
while ((str = Login.readLine()) !=null ) { 

    String [] x = str.Split(" ");

    // ?? new method to scan string array
    scanThis (x);
    sb.append(str);
    sb.append("\n");

}

void scanThis (String arr[]) 
{
  for (int x = ; x < arr.length; x++) {
  {
      if (arr[x].equals ("login")) {
          System.out.println ("Hurray");
          break;
      }
   } 
}     

Upvotes: 1

Related Questions