CharAt(0) out of bounds exception when I have too much white space
Currently stuck on an assignment that requires me to print out the users name as such: Last,First Initial.
(Bob, Billy H.) If I add too many spaces between the first and middle name when inputting, I get an index out of bounds exception. (String out of bounds 0) The program runs completely fine unless I have more than one space between the first and middle name.
I can only use the trim, indexOf, substring,and charAt methods in this program.
import java.util.Scanner;
public class Name {
public static void main(String args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter your name in this format: <spaces>First name<spaces>Middle name<spaces>Last name<spaces>");
String name = s.nextLine();
name = name.trim();
String first, middle, last;
int firstSpace = name.indexOf(' ');
first = name.substring(0, firstSpace);
int secondSpace = name.indexOf(" ", (firstSpace + 1));
middle = name.substring((firstSpace + 1), secondSpace);
middle.trim();
last = name.substring(secondSpace+1);
char middleInitial = middle.charAt(0);
String initial = "";
initial = initial + middleInitial;
for(int i = 1; i < middle.length(); i++) {
char currentLetter = middle.charAt(i);
char lastLetter = middle.charAt(i - 1);
if(lastLetter == ' ') {
initial = initial + "." + currentLetter;
}
}
System.out.println(last + "," + first + ' ' + initial + ".");
}
}
java
|
show 2 more comments
Currently stuck on an assignment that requires me to print out the users name as such: Last,First Initial.
(Bob, Billy H.) If I add too many spaces between the first and middle name when inputting, I get an index out of bounds exception. (String out of bounds 0) The program runs completely fine unless I have more than one space between the first and middle name.
I can only use the trim, indexOf, substring,and charAt methods in this program.
import java.util.Scanner;
public class Name {
public static void main(String args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter your name in this format: <spaces>First name<spaces>Middle name<spaces>Last name<spaces>");
String name = s.nextLine();
name = name.trim();
String first, middle, last;
int firstSpace = name.indexOf(' ');
first = name.substring(0, firstSpace);
int secondSpace = name.indexOf(" ", (firstSpace + 1));
middle = name.substring((firstSpace + 1), secondSpace);
middle.trim();
last = name.substring(secondSpace+1);
char middleInitial = middle.charAt(0);
String initial = "";
initial = initial + middleInitial;
for(int i = 1; i < middle.length(); i++) {
char currentLetter = middle.charAt(i);
char lastLetter = middle.charAt(i - 1);
if(lastLetter == ' ') {
initial = initial + "." + currentLetter;
}
}
System.out.println(last + "," + first + ' ' + initial + ".");
}
}
java
1
on which line does it fail?
– Lenin Raj Rajasekaran
Nov 22 '18 at 4:15
1
Did you try debugging for the problem?
– Busy Bee
Nov 22 '18 at 4:17
when you have multiple spaces there, yoursecondSpace
is before the middle name, and not after as you would expect
– Kartik
Nov 22 '18 at 4:23
and you are ignoring the result ofmiddle.trim();
.. change it tomiddle = middle.trim();
– Kartik
Nov 22 '18 at 4:23
1
Look atmiddle = name.substring((firstSpace + 1), secondSpace);
.middle
will be empty ifsecondSpace == fristSpace + 1
– Ricky Mo
Nov 22 '18 at 4:24
|
show 2 more comments
Currently stuck on an assignment that requires me to print out the users name as such: Last,First Initial.
(Bob, Billy H.) If I add too many spaces between the first and middle name when inputting, I get an index out of bounds exception. (String out of bounds 0) The program runs completely fine unless I have more than one space between the first and middle name.
I can only use the trim, indexOf, substring,and charAt methods in this program.
import java.util.Scanner;
public class Name {
public static void main(String args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter your name in this format: <spaces>First name<spaces>Middle name<spaces>Last name<spaces>");
String name = s.nextLine();
name = name.trim();
String first, middle, last;
int firstSpace = name.indexOf(' ');
first = name.substring(0, firstSpace);
int secondSpace = name.indexOf(" ", (firstSpace + 1));
middle = name.substring((firstSpace + 1), secondSpace);
middle.trim();
last = name.substring(secondSpace+1);
char middleInitial = middle.charAt(0);
String initial = "";
initial = initial + middleInitial;
for(int i = 1; i < middle.length(); i++) {
char currentLetter = middle.charAt(i);
char lastLetter = middle.charAt(i - 1);
if(lastLetter == ' ') {
initial = initial + "." + currentLetter;
}
}
System.out.println(last + "," + first + ' ' + initial + ".");
}
}
java
Currently stuck on an assignment that requires me to print out the users name as such: Last,First Initial.
(Bob, Billy H.) If I add too many spaces between the first and middle name when inputting, I get an index out of bounds exception. (String out of bounds 0) The program runs completely fine unless I have more than one space between the first and middle name.
I can only use the trim, indexOf, substring,and charAt methods in this program.
import java.util.Scanner;
public class Name {
public static void main(String args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter your name in this format: <spaces>First name<spaces>Middle name<spaces>Last name<spaces>");
String name = s.nextLine();
name = name.trim();
String first, middle, last;
int firstSpace = name.indexOf(' ');
first = name.substring(0, firstSpace);
int secondSpace = name.indexOf(" ", (firstSpace + 1));
middle = name.substring((firstSpace + 1), secondSpace);
middle.trim();
last = name.substring(secondSpace+1);
char middleInitial = middle.charAt(0);
String initial = "";
initial = initial + middleInitial;
for(int i = 1; i < middle.length(); i++) {
char currentLetter = middle.charAt(i);
char lastLetter = middle.charAt(i - 1);
if(lastLetter == ' ') {
initial = initial + "." + currentLetter;
}
}
System.out.println(last + "," + first + ' ' + initial + ".");
}
}
java
java
edited Nov 22 '18 at 13:07


E_net4
12.3k63569
12.3k63569
asked Nov 22 '18 at 4:14


CalculationCalculation
61
61
1
on which line does it fail?
– Lenin Raj Rajasekaran
Nov 22 '18 at 4:15
1
Did you try debugging for the problem?
– Busy Bee
Nov 22 '18 at 4:17
when you have multiple spaces there, yoursecondSpace
is before the middle name, and not after as you would expect
– Kartik
Nov 22 '18 at 4:23
and you are ignoring the result ofmiddle.trim();
.. change it tomiddle = middle.trim();
– Kartik
Nov 22 '18 at 4:23
1
Look atmiddle = name.substring((firstSpace + 1), secondSpace);
.middle
will be empty ifsecondSpace == fristSpace + 1
– Ricky Mo
Nov 22 '18 at 4:24
|
show 2 more comments
1
on which line does it fail?
– Lenin Raj Rajasekaran
Nov 22 '18 at 4:15
1
Did you try debugging for the problem?
– Busy Bee
Nov 22 '18 at 4:17
when you have multiple spaces there, yoursecondSpace
is before the middle name, and not after as you would expect
– Kartik
Nov 22 '18 at 4:23
and you are ignoring the result ofmiddle.trim();
.. change it tomiddle = middle.trim();
– Kartik
Nov 22 '18 at 4:23
1
Look atmiddle = name.substring((firstSpace + 1), secondSpace);
.middle
will be empty ifsecondSpace == fristSpace + 1
– Ricky Mo
Nov 22 '18 at 4:24
1
1
on which line does it fail?
– Lenin Raj Rajasekaran
Nov 22 '18 at 4:15
on which line does it fail?
– Lenin Raj Rajasekaran
Nov 22 '18 at 4:15
1
1
Did you try debugging for the problem?
– Busy Bee
Nov 22 '18 at 4:17
Did you try debugging for the problem?
– Busy Bee
Nov 22 '18 at 4:17
when you have multiple spaces there, your
secondSpace
is before the middle name, and not after as you would expect– Kartik
Nov 22 '18 at 4:23
when you have multiple spaces there, your
secondSpace
is before the middle name, and not after as you would expect– Kartik
Nov 22 '18 at 4:23
and you are ignoring the result of
middle.trim();
.. change it to middle = middle.trim();
– Kartik
Nov 22 '18 at 4:23
and you are ignoring the result of
middle.trim();
.. change it to middle = middle.trim();
– Kartik
Nov 22 '18 at 4:23
1
1
Look at
middle = name.substring((firstSpace + 1), secondSpace);
. middle
will be empty if secondSpace == fristSpace + 1
– Ricky Mo
Nov 22 '18 at 4:24
Look at
middle = name.substring((firstSpace + 1), secondSpace);
. middle
will be empty if secondSpace == fristSpace + 1
– Ricky Mo
Nov 22 '18 at 4:24
|
show 2 more comments
1 Answer
1
active
oldest
votes
The reason for error is for input
amid skum asdf
for above input:
int firstSpace = name.indexOf(' '); //firstSpace = 0
int secondSpace = name.indexOf(" ", (firstSpace + 1));//secondSpace = 1
middle = name.substring((firstSpace + 1), secondSpace); // as the two or more continues space inputted, this will select empty string as firstSpace + 1 == secondSpace and later causing the exception
Do name = name.replaceAll(" +", " ");
to replace all two or more white spaces.
As karthik suggested in comments, perform assignment middle = middle.trim();
.
EDIT:
Since you can not use replaceAll
, Modified the code just by using trim
method. Have a closer look at the below snippets:
String middleNameLastName = name.substring(firstSpace+1).trim();
last = middleNameLastName.substring(index+1).trim();
These removes trailing spaces.
import java.util.Scanner;
public class Post1 {
public static void main(String args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter your name in this format: <spaces>First name<spaces>Middle name<spaces>Last name<spaces>");
String name = s.nextLine();
name = name.trim();
String first, middle, last;
int firstSpace = name.indexOf(' ');
first = name.substring(0, firstSpace);
String middleNameLastName = name.substring(firstSpace+1).trim();
int index = middleNameLastName.indexOf(" ");
middle = middleNameLastName.substring(0, index);
last = middleNameLastName.substring(index+1).trim();
System.out.println(last + "," + first + ' ' + middle.charAt(0) + ".");
s.close();
}
}
Thanks for the suggestion. I would have used replaceAll if I was able to, but the assignment only allows me to use the four string methods that I stated above, and replaceAll is unfortunately not one of them.
– Calculation
Nov 22 '18 at 5:34
I see. Missed that.
– secret super star
Nov 22 '18 at 6:08
Do you have any idea on how to solve this problem without using replaceAll, I've tried everything and cannot solve this problem.
– Calculation
Nov 23 '18 at 23:51
@Calculation Edited answer as per your comments
– secret super star
Nov 24 '18 at 15:08
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%2f53423811%2fcharat0-out-of-bounds-exception-when-i-have-too-much-white-space%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The reason for error is for input
amid skum asdf
for above input:
int firstSpace = name.indexOf(' '); //firstSpace = 0
int secondSpace = name.indexOf(" ", (firstSpace + 1));//secondSpace = 1
middle = name.substring((firstSpace + 1), secondSpace); // as the two or more continues space inputted, this will select empty string as firstSpace + 1 == secondSpace and later causing the exception
Do name = name.replaceAll(" +", " ");
to replace all two or more white spaces.
As karthik suggested in comments, perform assignment middle = middle.trim();
.
EDIT:
Since you can not use replaceAll
, Modified the code just by using trim
method. Have a closer look at the below snippets:
String middleNameLastName = name.substring(firstSpace+1).trim();
last = middleNameLastName.substring(index+1).trim();
These removes trailing spaces.
import java.util.Scanner;
public class Post1 {
public static void main(String args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter your name in this format: <spaces>First name<spaces>Middle name<spaces>Last name<spaces>");
String name = s.nextLine();
name = name.trim();
String first, middle, last;
int firstSpace = name.indexOf(' ');
first = name.substring(0, firstSpace);
String middleNameLastName = name.substring(firstSpace+1).trim();
int index = middleNameLastName.indexOf(" ");
middle = middleNameLastName.substring(0, index);
last = middleNameLastName.substring(index+1).trim();
System.out.println(last + "," + first + ' ' + middle.charAt(0) + ".");
s.close();
}
}
Thanks for the suggestion. I would have used replaceAll if I was able to, but the assignment only allows me to use the four string methods that I stated above, and replaceAll is unfortunately not one of them.
– Calculation
Nov 22 '18 at 5:34
I see. Missed that.
– secret super star
Nov 22 '18 at 6:08
Do you have any idea on how to solve this problem without using replaceAll, I've tried everything and cannot solve this problem.
– Calculation
Nov 23 '18 at 23:51
@Calculation Edited answer as per your comments
– secret super star
Nov 24 '18 at 15:08
add a comment |
The reason for error is for input
amid skum asdf
for above input:
int firstSpace = name.indexOf(' '); //firstSpace = 0
int secondSpace = name.indexOf(" ", (firstSpace + 1));//secondSpace = 1
middle = name.substring((firstSpace + 1), secondSpace); // as the two or more continues space inputted, this will select empty string as firstSpace + 1 == secondSpace and later causing the exception
Do name = name.replaceAll(" +", " ");
to replace all two or more white spaces.
As karthik suggested in comments, perform assignment middle = middle.trim();
.
EDIT:
Since you can not use replaceAll
, Modified the code just by using trim
method. Have a closer look at the below snippets:
String middleNameLastName = name.substring(firstSpace+1).trim();
last = middleNameLastName.substring(index+1).trim();
These removes trailing spaces.
import java.util.Scanner;
public class Post1 {
public static void main(String args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter your name in this format: <spaces>First name<spaces>Middle name<spaces>Last name<spaces>");
String name = s.nextLine();
name = name.trim();
String first, middle, last;
int firstSpace = name.indexOf(' ');
first = name.substring(0, firstSpace);
String middleNameLastName = name.substring(firstSpace+1).trim();
int index = middleNameLastName.indexOf(" ");
middle = middleNameLastName.substring(0, index);
last = middleNameLastName.substring(index+1).trim();
System.out.println(last + "," + first + ' ' + middle.charAt(0) + ".");
s.close();
}
}
Thanks for the suggestion. I would have used replaceAll if I was able to, but the assignment only allows me to use the four string methods that I stated above, and replaceAll is unfortunately not one of them.
– Calculation
Nov 22 '18 at 5:34
I see. Missed that.
– secret super star
Nov 22 '18 at 6:08
Do you have any idea on how to solve this problem without using replaceAll, I've tried everything and cannot solve this problem.
– Calculation
Nov 23 '18 at 23:51
@Calculation Edited answer as per your comments
– secret super star
Nov 24 '18 at 15:08
add a comment |
The reason for error is for input
amid skum asdf
for above input:
int firstSpace = name.indexOf(' '); //firstSpace = 0
int secondSpace = name.indexOf(" ", (firstSpace + 1));//secondSpace = 1
middle = name.substring((firstSpace + 1), secondSpace); // as the two or more continues space inputted, this will select empty string as firstSpace + 1 == secondSpace and later causing the exception
Do name = name.replaceAll(" +", " ");
to replace all two or more white spaces.
As karthik suggested in comments, perform assignment middle = middle.trim();
.
EDIT:
Since you can not use replaceAll
, Modified the code just by using trim
method. Have a closer look at the below snippets:
String middleNameLastName = name.substring(firstSpace+1).trim();
last = middleNameLastName.substring(index+1).trim();
These removes trailing spaces.
import java.util.Scanner;
public class Post1 {
public static void main(String args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter your name in this format: <spaces>First name<spaces>Middle name<spaces>Last name<spaces>");
String name = s.nextLine();
name = name.trim();
String first, middle, last;
int firstSpace = name.indexOf(' ');
first = name.substring(0, firstSpace);
String middleNameLastName = name.substring(firstSpace+1).trim();
int index = middleNameLastName.indexOf(" ");
middle = middleNameLastName.substring(0, index);
last = middleNameLastName.substring(index+1).trim();
System.out.println(last + "," + first + ' ' + middle.charAt(0) + ".");
s.close();
}
}
The reason for error is for input
amid skum asdf
for above input:
int firstSpace = name.indexOf(' '); //firstSpace = 0
int secondSpace = name.indexOf(" ", (firstSpace + 1));//secondSpace = 1
middle = name.substring((firstSpace + 1), secondSpace); // as the two or more continues space inputted, this will select empty string as firstSpace + 1 == secondSpace and later causing the exception
Do name = name.replaceAll(" +", " ");
to replace all two or more white spaces.
As karthik suggested in comments, perform assignment middle = middle.trim();
.
EDIT:
Since you can not use replaceAll
, Modified the code just by using trim
method. Have a closer look at the below snippets:
String middleNameLastName = name.substring(firstSpace+1).trim();
last = middleNameLastName.substring(index+1).trim();
These removes trailing spaces.
import java.util.Scanner;
public class Post1 {
public static void main(String args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter your name in this format: <spaces>First name<spaces>Middle name<spaces>Last name<spaces>");
String name = s.nextLine();
name = name.trim();
String first, middle, last;
int firstSpace = name.indexOf(' ');
first = name.substring(0, firstSpace);
String middleNameLastName = name.substring(firstSpace+1).trim();
int index = middleNameLastName.indexOf(" ");
middle = middleNameLastName.substring(0, index);
last = middleNameLastName.substring(index+1).trim();
System.out.println(last + "," + first + ' ' + middle.charAt(0) + ".");
s.close();
}
}
edited Nov 24 '18 at 15:08
answered Nov 22 '18 at 4:42


secret super starsecret super star
1,025115
1,025115
Thanks for the suggestion. I would have used replaceAll if I was able to, but the assignment only allows me to use the four string methods that I stated above, and replaceAll is unfortunately not one of them.
– Calculation
Nov 22 '18 at 5:34
I see. Missed that.
– secret super star
Nov 22 '18 at 6:08
Do you have any idea on how to solve this problem without using replaceAll, I've tried everything and cannot solve this problem.
– Calculation
Nov 23 '18 at 23:51
@Calculation Edited answer as per your comments
– secret super star
Nov 24 '18 at 15:08
add a comment |
Thanks for the suggestion. I would have used replaceAll if I was able to, but the assignment only allows me to use the four string methods that I stated above, and replaceAll is unfortunately not one of them.
– Calculation
Nov 22 '18 at 5:34
I see. Missed that.
– secret super star
Nov 22 '18 at 6:08
Do you have any idea on how to solve this problem without using replaceAll, I've tried everything and cannot solve this problem.
– Calculation
Nov 23 '18 at 23:51
@Calculation Edited answer as per your comments
– secret super star
Nov 24 '18 at 15:08
Thanks for the suggestion. I would have used replaceAll if I was able to, but the assignment only allows me to use the four string methods that I stated above, and replaceAll is unfortunately not one of them.
– Calculation
Nov 22 '18 at 5:34
Thanks for the suggestion. I would have used replaceAll if I was able to, but the assignment only allows me to use the four string methods that I stated above, and replaceAll is unfortunately not one of them.
– Calculation
Nov 22 '18 at 5:34
I see. Missed that.
– secret super star
Nov 22 '18 at 6:08
I see. Missed that.
– secret super star
Nov 22 '18 at 6:08
Do you have any idea on how to solve this problem without using replaceAll, I've tried everything and cannot solve this problem.
– Calculation
Nov 23 '18 at 23:51
Do you have any idea on how to solve this problem without using replaceAll, I've tried everything and cannot solve this problem.
– Calculation
Nov 23 '18 at 23:51
@Calculation Edited answer as per your comments
– secret super star
Nov 24 '18 at 15:08
@Calculation Edited answer as per your comments
– secret super star
Nov 24 '18 at 15:08
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%2f53423811%2fcharat0-out-of-bounds-exception-when-i-have-too-much-white-space%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
1
on which line does it fail?
– Lenin Raj Rajasekaran
Nov 22 '18 at 4:15
1
Did you try debugging for the problem?
– Busy Bee
Nov 22 '18 at 4:17
when you have multiple spaces there, your
secondSpace
is before the middle name, and not after as you would expect– Kartik
Nov 22 '18 at 4:23
and you are ignoring the result of
middle.trim();
.. change it tomiddle = middle.trim();
– Kartik
Nov 22 '18 at 4:23
1
Look at
middle = name.substring((firstSpace + 1), secondSpace);
.middle
will be empty ifsecondSpace == fristSpace + 1
– Ricky Mo
Nov 22 '18 at 4:24