trouble in java, splitting strings
I want to scan a sentence that the user types, but I want to scan the words separately and then perform the actions depending on the words the user has put.
}else if(choice.equals("load")){
System.out.println("Enter the name of the file:");
String name = input.next();
loadFile(name);
what I want is the user to type "load file.txt" directly
java string split
|
show 6 more comments
I want to scan a sentence that the user types, but I want to scan the words separately and then perform the actions depending on the words the user has put.
}else if(choice.equals("load")){
System.out.println("Enter the name of the file:");
String name = input.next();
loadFile(name);
what I want is the user to type "load file.txt" directly
java string split
4
What problems are you having?
– Hovercraft Full Of Eels
Dec 18 '12 at 22:15
Show us some input and expected output. You can split string byString.split("\s+")
for whitespaces
– Smit
Dec 18 '12 at 22:16
this is one case of my code, so for each one of them i need the user to type the full sentence of what he wants and with one scan the program will execute.
– Angelo Mico
Dec 18 '12 at 22:16
I basically answered that question in my comment under your other question.
– jlordo
Dec 18 '12 at 22:17
@smit the input should be "load file.txt" and the program will execute that file without the need for the user to firstly type if he wants to load and then asked again what file he wants to load
– Angelo Mico
Dec 18 '12 at 22:18
|
show 6 more comments
I want to scan a sentence that the user types, but I want to scan the words separately and then perform the actions depending on the words the user has put.
}else if(choice.equals("load")){
System.out.println("Enter the name of the file:");
String name = input.next();
loadFile(name);
what I want is the user to type "load file.txt" directly
java string split
I want to scan a sentence that the user types, but I want to scan the words separately and then perform the actions depending on the words the user has put.
}else if(choice.equals("load")){
System.out.println("Enter the name of the file:");
String name = input.next();
loadFile(name);
what I want is the user to type "load file.txt" directly
java string split
java string split
edited Nov 20 '18 at 2:09
Cœur
17.6k9104145
17.6k9104145
asked Dec 18 '12 at 22:15
Angelo MicoAngelo Mico
194
194
4
What problems are you having?
– Hovercraft Full Of Eels
Dec 18 '12 at 22:15
Show us some input and expected output. You can split string byString.split("\s+")
for whitespaces
– Smit
Dec 18 '12 at 22:16
this is one case of my code, so for each one of them i need the user to type the full sentence of what he wants and with one scan the program will execute.
– Angelo Mico
Dec 18 '12 at 22:16
I basically answered that question in my comment under your other question.
– jlordo
Dec 18 '12 at 22:17
@smit the input should be "load file.txt" and the program will execute that file without the need for the user to firstly type if he wants to load and then asked again what file he wants to load
– Angelo Mico
Dec 18 '12 at 22:18
|
show 6 more comments
4
What problems are you having?
– Hovercraft Full Of Eels
Dec 18 '12 at 22:15
Show us some input and expected output. You can split string byString.split("\s+")
for whitespaces
– Smit
Dec 18 '12 at 22:16
this is one case of my code, so for each one of them i need the user to type the full sentence of what he wants and with one scan the program will execute.
– Angelo Mico
Dec 18 '12 at 22:16
I basically answered that question in my comment under your other question.
– jlordo
Dec 18 '12 at 22:17
@smit the input should be "load file.txt" and the program will execute that file without the need for the user to firstly type if he wants to load and then asked again what file he wants to load
– Angelo Mico
Dec 18 '12 at 22:18
4
4
What problems are you having?
– Hovercraft Full Of Eels
Dec 18 '12 at 22:15
What problems are you having?
– Hovercraft Full Of Eels
Dec 18 '12 at 22:15
Show us some input and expected output. You can split string by
String.split("\s+")
for whitespaces– Smit
Dec 18 '12 at 22:16
Show us some input and expected output. You can split string by
String.split("\s+")
for whitespaces– Smit
Dec 18 '12 at 22:16
this is one case of my code, so for each one of them i need the user to type the full sentence of what he wants and with one scan the program will execute.
– Angelo Mico
Dec 18 '12 at 22:16
this is one case of my code, so for each one of them i need the user to type the full sentence of what he wants and with one scan the program will execute.
– Angelo Mico
Dec 18 '12 at 22:16
I basically answered that question in my comment under your other question.
– jlordo
Dec 18 '12 at 22:17
I basically answered that question in my comment under your other question.
– jlordo
Dec 18 '12 at 22:17
@smit the input should be "load file.txt" and the program will execute that file without the need for the user to firstly type if he wants to load and then asked again what file he wants to load
– Angelo Mico
Dec 18 '12 at 22:18
@smit the input should be "load file.txt" and the program will execute that file without the need for the user to firstly type if he wants to load and then asked again what file he wants to load
– Angelo Mico
Dec 18 '12 at 22:18
|
show 6 more comments
2 Answers
2
active
oldest
votes
I try to write a small code for you may be this can help you a little bit
public class Tester2 {
/**
* @param args
*/
public static void main(String args) {
boolean state = true;
while(state) {
System.out.println("Please enter your text");
Scanner scan = new Scanner(System.in);
String input = scan.nextLine();
if(input.contains("load")) {
String fileName = getFileName(input);
if(fileName != "" || fileName!= null) {
load(fileName);
}
}else{
}
}
}
private static String getFileName(String input) {
String values = input.split("\s+");
String fileName = "";
for(String s : values) {
if(s.indexOf(".") > 0) {
fileName = s;
break;
}
}
return fileName;
}
}
Whenever you have questions, feel free to ask
– Festus Tamakloe
Dec 18 '12 at 22:32
,yup i think its kind of what i want i just dont get this : for(String s : values) { if(s.indexOf(".") > 0) { fileName = s; break; why did you do this?
– Angelo Mico
Dec 18 '12 at 22:49
for(String s : values) ==> iteration/loop over the values if(s.indexOf(".") > 0) ==> here we want to which word has an extension in another which word contains(".") if we get one we passe it to fileName then we do not need anymore to continue (break). If your satisfy with my answer just vote up and accept it
– Festus Tamakloe
Dec 18 '12 at 22:50
ok,one more thing,I need to have some other options other than "load" like "inspect" an other class etc.. should I continue with this way? put an "else" and carry on with the next option?and do i need to do the thing i asked above again for each of the options i have?
– Angelo Mico
Dec 18 '12 at 23:05
You can do it in the same. but use else if(conditions){} try write a litte method where you can some some codes to avoid the repetition
– Festus Tamakloe
Dec 18 '12 at 23:10
|
show 2 more comments
It's too long for a comment, and I don't really understand what your last comment means. Here's some sample code. Try it with inputs like
- load filename.txt
- load foo bar baz
- save foo
whatever ...
System.out.println("What would you like to do");
Scanner scanner = new Scanner(System.in);
String line = scanner.nextLine();
String input = line.split("\s+");
if (input[0].equals("load")) {
for (int i = 1; i < input.length; i++) {
System.out.println("Token #" + i + " after load: " + input[i]);
}
} else {
System.out.println("You did not enter load, I ignore your input");
}
If you have a question about the behavior or anything else: feel free to leave a comment.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f13942390%2ftrouble-in-java-splitting-strings%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I try to write a small code for you may be this can help you a little bit
public class Tester2 {
/**
* @param args
*/
public static void main(String args) {
boolean state = true;
while(state) {
System.out.println("Please enter your text");
Scanner scan = new Scanner(System.in);
String input = scan.nextLine();
if(input.contains("load")) {
String fileName = getFileName(input);
if(fileName != "" || fileName!= null) {
load(fileName);
}
}else{
}
}
}
private static String getFileName(String input) {
String values = input.split("\s+");
String fileName = "";
for(String s : values) {
if(s.indexOf(".") > 0) {
fileName = s;
break;
}
}
return fileName;
}
}
Whenever you have questions, feel free to ask
– Festus Tamakloe
Dec 18 '12 at 22:32
,yup i think its kind of what i want i just dont get this : for(String s : values) { if(s.indexOf(".") > 0) { fileName = s; break; why did you do this?
– Angelo Mico
Dec 18 '12 at 22:49
for(String s : values) ==> iteration/loop over the values if(s.indexOf(".") > 0) ==> here we want to which word has an extension in another which word contains(".") if we get one we passe it to fileName then we do not need anymore to continue (break). If your satisfy with my answer just vote up and accept it
– Festus Tamakloe
Dec 18 '12 at 22:50
ok,one more thing,I need to have some other options other than "load" like "inspect" an other class etc.. should I continue with this way? put an "else" and carry on with the next option?and do i need to do the thing i asked above again for each of the options i have?
– Angelo Mico
Dec 18 '12 at 23:05
You can do it in the same. but use else if(conditions){} try write a litte method where you can some some codes to avoid the repetition
– Festus Tamakloe
Dec 18 '12 at 23:10
|
show 2 more comments
I try to write a small code for you may be this can help you a little bit
public class Tester2 {
/**
* @param args
*/
public static void main(String args) {
boolean state = true;
while(state) {
System.out.println("Please enter your text");
Scanner scan = new Scanner(System.in);
String input = scan.nextLine();
if(input.contains("load")) {
String fileName = getFileName(input);
if(fileName != "" || fileName!= null) {
load(fileName);
}
}else{
}
}
}
private static String getFileName(String input) {
String values = input.split("\s+");
String fileName = "";
for(String s : values) {
if(s.indexOf(".") > 0) {
fileName = s;
break;
}
}
return fileName;
}
}
Whenever you have questions, feel free to ask
– Festus Tamakloe
Dec 18 '12 at 22:32
,yup i think its kind of what i want i just dont get this : for(String s : values) { if(s.indexOf(".") > 0) { fileName = s; break; why did you do this?
– Angelo Mico
Dec 18 '12 at 22:49
for(String s : values) ==> iteration/loop over the values if(s.indexOf(".") > 0) ==> here we want to which word has an extension in another which word contains(".") if we get one we passe it to fileName then we do not need anymore to continue (break). If your satisfy with my answer just vote up and accept it
– Festus Tamakloe
Dec 18 '12 at 22:50
ok,one more thing,I need to have some other options other than "load" like "inspect" an other class etc.. should I continue with this way? put an "else" and carry on with the next option?and do i need to do the thing i asked above again for each of the options i have?
– Angelo Mico
Dec 18 '12 at 23:05
You can do it in the same. but use else if(conditions){} try write a litte method where you can some some codes to avoid the repetition
– Festus Tamakloe
Dec 18 '12 at 23:10
|
show 2 more comments
I try to write a small code for you may be this can help you a little bit
public class Tester2 {
/**
* @param args
*/
public static void main(String args) {
boolean state = true;
while(state) {
System.out.println("Please enter your text");
Scanner scan = new Scanner(System.in);
String input = scan.nextLine();
if(input.contains("load")) {
String fileName = getFileName(input);
if(fileName != "" || fileName!= null) {
load(fileName);
}
}else{
}
}
}
private static String getFileName(String input) {
String values = input.split("\s+");
String fileName = "";
for(String s : values) {
if(s.indexOf(".") > 0) {
fileName = s;
break;
}
}
return fileName;
}
}
I try to write a small code for you may be this can help you a little bit
public class Tester2 {
/**
* @param args
*/
public static void main(String args) {
boolean state = true;
while(state) {
System.out.println("Please enter your text");
Scanner scan = new Scanner(System.in);
String input = scan.nextLine();
if(input.contains("load")) {
String fileName = getFileName(input);
if(fileName != "" || fileName!= null) {
load(fileName);
}
}else{
}
}
}
private static String getFileName(String input) {
String values = input.split("\s+");
String fileName = "";
for(String s : values) {
if(s.indexOf(".") > 0) {
fileName = s;
break;
}
}
return fileName;
}
}
edited Dec 18 '12 at 23:12
answered Dec 18 '12 at 22:30


Festus TamakloeFestus Tamakloe
9,68484559
9,68484559
Whenever you have questions, feel free to ask
– Festus Tamakloe
Dec 18 '12 at 22:32
,yup i think its kind of what i want i just dont get this : for(String s : values) { if(s.indexOf(".") > 0) { fileName = s; break; why did you do this?
– Angelo Mico
Dec 18 '12 at 22:49
for(String s : values) ==> iteration/loop over the values if(s.indexOf(".") > 0) ==> here we want to which word has an extension in another which word contains(".") if we get one we passe it to fileName then we do not need anymore to continue (break). If your satisfy with my answer just vote up and accept it
– Festus Tamakloe
Dec 18 '12 at 22:50
ok,one more thing,I need to have some other options other than "load" like "inspect" an other class etc.. should I continue with this way? put an "else" and carry on with the next option?and do i need to do the thing i asked above again for each of the options i have?
– Angelo Mico
Dec 18 '12 at 23:05
You can do it in the same. but use else if(conditions){} try write a litte method where you can some some codes to avoid the repetition
– Festus Tamakloe
Dec 18 '12 at 23:10
|
show 2 more comments
Whenever you have questions, feel free to ask
– Festus Tamakloe
Dec 18 '12 at 22:32
,yup i think its kind of what i want i just dont get this : for(String s : values) { if(s.indexOf(".") > 0) { fileName = s; break; why did you do this?
– Angelo Mico
Dec 18 '12 at 22:49
for(String s : values) ==> iteration/loop over the values if(s.indexOf(".") > 0) ==> here we want to which word has an extension in another which word contains(".") if we get one we passe it to fileName then we do not need anymore to continue (break). If your satisfy with my answer just vote up and accept it
– Festus Tamakloe
Dec 18 '12 at 22:50
ok,one more thing,I need to have some other options other than "load" like "inspect" an other class etc.. should I continue with this way? put an "else" and carry on with the next option?and do i need to do the thing i asked above again for each of the options i have?
– Angelo Mico
Dec 18 '12 at 23:05
You can do it in the same. but use else if(conditions){} try write a litte method where you can some some codes to avoid the repetition
– Festus Tamakloe
Dec 18 '12 at 23:10
Whenever you have questions, feel free to ask
– Festus Tamakloe
Dec 18 '12 at 22:32
Whenever you have questions, feel free to ask
– Festus Tamakloe
Dec 18 '12 at 22:32
,yup i think its kind of what i want i just dont get this : for(String s : values) { if(s.indexOf(".") > 0) { fileName = s; break; why did you do this?
– Angelo Mico
Dec 18 '12 at 22:49
,yup i think its kind of what i want i just dont get this : for(String s : values) { if(s.indexOf(".") > 0) { fileName = s; break; why did you do this?
– Angelo Mico
Dec 18 '12 at 22:49
for(String s : values) ==> iteration/loop over the values if(s.indexOf(".") > 0) ==> here we want to which word has an extension in another which word contains(".") if we get one we passe it to fileName then we do not need anymore to continue (break). If your satisfy with my answer just vote up and accept it
– Festus Tamakloe
Dec 18 '12 at 22:50
for(String s : values) ==> iteration/loop over the values if(s.indexOf(".") > 0) ==> here we want to which word has an extension in another which word contains(".") if we get one we passe it to fileName then we do not need anymore to continue (break). If your satisfy with my answer just vote up and accept it
– Festus Tamakloe
Dec 18 '12 at 22:50
ok,one more thing,I need to have some other options other than "load" like "inspect" an other class etc.. should I continue with this way? put an "else" and carry on with the next option?and do i need to do the thing i asked above again for each of the options i have?
– Angelo Mico
Dec 18 '12 at 23:05
ok,one more thing,I need to have some other options other than "load" like "inspect" an other class etc.. should I continue with this way? put an "else" and carry on with the next option?and do i need to do the thing i asked above again for each of the options i have?
– Angelo Mico
Dec 18 '12 at 23:05
You can do it in the same. but use else if(conditions){} try write a litte method where you can some some codes to avoid the repetition
– Festus Tamakloe
Dec 18 '12 at 23:10
You can do it in the same. but use else if(conditions){} try write a litte method where you can some some codes to avoid the repetition
– Festus Tamakloe
Dec 18 '12 at 23:10
|
show 2 more comments
It's too long for a comment, and I don't really understand what your last comment means. Here's some sample code. Try it with inputs like
- load filename.txt
- load foo bar baz
- save foo
whatever ...
System.out.println("What would you like to do");
Scanner scanner = new Scanner(System.in);
String line = scanner.nextLine();
String input = line.split("\s+");
if (input[0].equals("load")) {
for (int i = 1; i < input.length; i++) {
System.out.println("Token #" + i + " after load: " + input[i]);
}
} else {
System.out.println("You did not enter load, I ignore your input");
}
If you have a question about the behavior or anything else: feel free to leave a comment.
add a comment |
It's too long for a comment, and I don't really understand what your last comment means. Here's some sample code. Try it with inputs like
- load filename.txt
- load foo bar baz
- save foo
whatever ...
System.out.println("What would you like to do");
Scanner scanner = new Scanner(System.in);
String line = scanner.nextLine();
String input = line.split("\s+");
if (input[0].equals("load")) {
for (int i = 1; i < input.length; i++) {
System.out.println("Token #" + i + " after load: " + input[i]);
}
} else {
System.out.println("You did not enter load, I ignore your input");
}
If you have a question about the behavior or anything else: feel free to leave a comment.
add a comment |
It's too long for a comment, and I don't really understand what your last comment means. Here's some sample code. Try it with inputs like
- load filename.txt
- load foo bar baz
- save foo
whatever ...
System.out.println("What would you like to do");
Scanner scanner = new Scanner(System.in);
String line = scanner.nextLine();
String input = line.split("\s+");
if (input[0].equals("load")) {
for (int i = 1; i < input.length; i++) {
System.out.println("Token #" + i + " after load: " + input[i]);
}
} else {
System.out.println("You did not enter load, I ignore your input");
}
If you have a question about the behavior or anything else: feel free to leave a comment.
It's too long for a comment, and I don't really understand what your last comment means. Here's some sample code. Try it with inputs like
- load filename.txt
- load foo bar baz
- save foo
whatever ...
System.out.println("What would you like to do");
Scanner scanner = new Scanner(System.in);
String line = scanner.nextLine();
String input = line.split("\s+");
if (input[0].equals("load")) {
for (int i = 1; i < input.length; i++) {
System.out.println("Token #" + i + " after load: " + input[i]);
}
} else {
System.out.println("You did not enter load, I ignore your input");
}
If you have a question about the behavior or anything else: feel free to leave a comment.
answered Dec 18 '12 at 23:07
jlordojlordo
31.1k54371
31.1k54371
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f13942390%2ftrouble-in-java-splitting-strings%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
4
What problems are you having?
– Hovercraft Full Of Eels
Dec 18 '12 at 22:15
Show us some input and expected output. You can split string by
String.split("\s+")
for whitespaces– Smit
Dec 18 '12 at 22:16
this is one case of my code, so for each one of them i need the user to type the full sentence of what he wants and with one scan the program will execute.
– Angelo Mico
Dec 18 '12 at 22:16
I basically answered that question in my comment under your other question.
– jlordo
Dec 18 '12 at 22:17
@smit the input should be "load file.txt" and the program will execute that file without the need for the user to firstly type if he wants to load and then asked again what file he wants to load
– Angelo Mico
Dec 18 '12 at 22:18