0

I want to split up an input by the user and insert only the second element and then every element after that. Don't worry about any of the database code, it all works fine.

Basically the user is going to input !add name output If it sees !add, its going to log the name and output to the database. The name is one word but the output can be a string of many words. The problem is when I split the string the output becomes too much to manage and I don't know how to insert it into the database.

This is what I have so far

public void onMessage(message) { String[] splitMessage = message.split(" "); if (splitMessage[0] == "!add") { try { conn = connectionMain.getConnection(); String query = "insert ignore into commands(name, output) values(?,?)"; pstmt = conn.prepareStatement(query); pstmt.setString(1, splitMessage[1]); pstmt.setString(2, /*Question*/); pstmt.executeUpdate(); /* Catch/Finally Statements Below */ } } } 
2
  • 1
    Not entirely sure what you mean, but you may want to use message.split(" ",3) instead of message.split(" ") and then use splitMessage[2] to set your second parameter. See docs.oracle.com/javase/8/docs/api/java/lang/… but take care that the resulting array is only guaranteed to have at most 3 elements (ie: it could have less) Commented Mar 3, 2015 at 23:34
  • Oh I missed that on the docs. I need to improve my skim reading skills. Thank you. Commented Mar 3, 2015 at 23:54

1 Answer 1

1

You need to substring after the first (space) separator.

int idx = message.indexOf(" "); String question = message.substring(idx + 1); pstmt.setString(2, question); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.