Having issue with “else if” in Java where need to have second example of text input by user to change...
I am trying to use the "else if" function to have a second instance where if the first character of the input is, say "p", to change the full text to lower case using ".toLowerCase".
Am trying to just keep the one String, or should I need to set a second String to get this criteria to run.
Code currently is:
public static void main(String args) {
Scanner input = new Scanner(System.in);
System.out.print("Please enter a uncoded string: ");
String first = input.nextLine();
if (first.charAt(0) == 'u') {
first = first.toUpperCase();{
if (first.chatAt(0) == 'l') {
first = first.toLowerCase();
}
}
System.out.println("The decoded string is: " + first);
}
}
}
The first part works to change text to upper case using "toUpperCase" but am having trouble getting to include the second itteration to run.
Get this error message in the Console:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method chatAt(int) is undefined for the type String
Any help appreciated.
Thanks
java if-statement
add a comment |
I am trying to use the "else if" function to have a second instance where if the first character of the input is, say "p", to change the full text to lower case using ".toLowerCase".
Am trying to just keep the one String, or should I need to set a second String to get this criteria to run.
Code currently is:
public static void main(String args) {
Scanner input = new Scanner(System.in);
System.out.print("Please enter a uncoded string: ");
String first = input.nextLine();
if (first.charAt(0) == 'u') {
first = first.toUpperCase();{
if (first.chatAt(0) == 'l') {
first = first.toLowerCase();
}
}
System.out.println("The decoded string is: " + first);
}
}
}
The first part works to change text to upper case using "toUpperCase" but am having trouble getting to include the second itteration to run.
Get this error message in the Console:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method chatAt(int) is undefined for the type String
Any help appreciated.
Thanks
java if-statement
Is there another String class in your project?
– shmosel
Nov 19 '18 at 23:13
Not yet, but would assume it would have to be, as just would be a second criteria that would need to be met. Was trying to use "else if" just to move onto a second scenario.
– AFanmhi
Nov 19 '18 at 23:21
You should definitely not have another String class. But if you did, it might explain the error you're seeing.
– shmosel
Nov 19 '18 at 23:22
2
The error is due to mistyping of charAt(int). You wrote chatAt
– M.G
Nov 19 '18 at 23:26
add a comment |
I am trying to use the "else if" function to have a second instance where if the first character of the input is, say "p", to change the full text to lower case using ".toLowerCase".
Am trying to just keep the one String, or should I need to set a second String to get this criteria to run.
Code currently is:
public static void main(String args) {
Scanner input = new Scanner(System.in);
System.out.print("Please enter a uncoded string: ");
String first = input.nextLine();
if (first.charAt(0) == 'u') {
first = first.toUpperCase();{
if (first.chatAt(0) == 'l') {
first = first.toLowerCase();
}
}
System.out.println("The decoded string is: " + first);
}
}
}
The first part works to change text to upper case using "toUpperCase" but am having trouble getting to include the second itteration to run.
Get this error message in the Console:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method chatAt(int) is undefined for the type String
Any help appreciated.
Thanks
java if-statement
I am trying to use the "else if" function to have a second instance where if the first character of the input is, say "p", to change the full text to lower case using ".toLowerCase".
Am trying to just keep the one String, or should I need to set a second String to get this criteria to run.
Code currently is:
public static void main(String args) {
Scanner input = new Scanner(System.in);
System.out.print("Please enter a uncoded string: ");
String first = input.nextLine();
if (first.charAt(0) == 'u') {
first = first.toUpperCase();{
if (first.chatAt(0) == 'l') {
first = first.toLowerCase();
}
}
System.out.println("The decoded string is: " + first);
}
}
}
The first part works to change text to upper case using "toUpperCase" but am having trouble getting to include the second itteration to run.
Get this error message in the Console:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method chatAt(int) is undefined for the type String
Any help appreciated.
Thanks
java if-statement
java if-statement
asked Nov 19 '18 at 23:11
AFanmhiAFanmhi
186
186
Is there another String class in your project?
– shmosel
Nov 19 '18 at 23:13
Not yet, but would assume it would have to be, as just would be a second criteria that would need to be met. Was trying to use "else if" just to move onto a second scenario.
– AFanmhi
Nov 19 '18 at 23:21
You should definitely not have another String class. But if you did, it might explain the error you're seeing.
– shmosel
Nov 19 '18 at 23:22
2
The error is due to mistyping of charAt(int). You wrote chatAt
– M.G
Nov 19 '18 at 23:26
add a comment |
Is there another String class in your project?
– shmosel
Nov 19 '18 at 23:13
Not yet, but would assume it would have to be, as just would be a second criteria that would need to be met. Was trying to use "else if" just to move onto a second scenario.
– AFanmhi
Nov 19 '18 at 23:21
You should definitely not have another String class. But if you did, it might explain the error you're seeing.
– shmosel
Nov 19 '18 at 23:22
2
The error is due to mistyping of charAt(int). You wrote chatAt
– M.G
Nov 19 '18 at 23:26
Is there another String class in your project?
– shmosel
Nov 19 '18 at 23:13
Is there another String class in your project?
– shmosel
Nov 19 '18 at 23:13
Not yet, but would assume it would have to be, as just would be a second criteria that would need to be met. Was trying to use "else if" just to move onto a second scenario.
– AFanmhi
Nov 19 '18 at 23:21
Not yet, but would assume it would have to be, as just would be a second criteria that would need to be met. Was trying to use "else if" just to move onto a second scenario.
– AFanmhi
Nov 19 '18 at 23:21
You should definitely not have another String class. But if you did, it might explain the error you're seeing.
– shmosel
Nov 19 '18 at 23:22
You should definitely not have another String class. But if you did, it might explain the error you're seeing.
– shmosel
Nov 19 '18 at 23:22
2
2
The error is due to mistyping of charAt(int). You wrote chatAt
– M.G
Nov 19 '18 at 23:26
The error is due to mistyping of charAt(int). You wrote chatAt
– M.G
Nov 19 '18 at 23:26
add a comment |
1 Answer
1
active
oldest
votes
Currently you have nested if
statements instead of else if
, where if the first letter is u, then you enter that block and check if the first letter is l. Keep in mind the code below will not work as you would like if the user enters strings such as "Unicorn".
import java.util.Scanner;
public class Testing{
public static void main(String args) {
Scanner input = new Scanner(System.in);
System.out.print("Please enter a uncoded string: ");
String first = input.nextLine();
input.close();
if (first.charAt(0) == 'u') {
first = first.toUpperCase();
} else if (first.charAt(0) == 'l') {
first = first.toLowerCase();
}
System.out.println("The decoded string is: " + first);
}
}
Also typically the if else
will be paired with an additional else afterwards to handle all other cases, like so:
if (condition){//Check one case
// do something
} else if (condition){//Check another case
// do something
} else { //All other cases
// do something else
}
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%2f53383967%2fhaving-issue-with-else-if-in-java-where-need-to-have-second-example-of-text-in%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
Currently you have nested if
statements instead of else if
, where if the first letter is u, then you enter that block and check if the first letter is l. Keep in mind the code below will not work as you would like if the user enters strings such as "Unicorn".
import java.util.Scanner;
public class Testing{
public static void main(String args) {
Scanner input = new Scanner(System.in);
System.out.print("Please enter a uncoded string: ");
String first = input.nextLine();
input.close();
if (first.charAt(0) == 'u') {
first = first.toUpperCase();
} else if (first.charAt(0) == 'l') {
first = first.toLowerCase();
}
System.out.println("The decoded string is: " + first);
}
}
Also typically the if else
will be paired with an additional else afterwards to handle all other cases, like so:
if (condition){//Check one case
// do something
} else if (condition){//Check another case
// do something
} else { //All other cases
// do something else
}
add a comment |
Currently you have nested if
statements instead of else if
, where if the first letter is u, then you enter that block and check if the first letter is l. Keep in mind the code below will not work as you would like if the user enters strings such as "Unicorn".
import java.util.Scanner;
public class Testing{
public static void main(String args) {
Scanner input = new Scanner(System.in);
System.out.print("Please enter a uncoded string: ");
String first = input.nextLine();
input.close();
if (first.charAt(0) == 'u') {
first = first.toUpperCase();
} else if (first.charAt(0) == 'l') {
first = first.toLowerCase();
}
System.out.println("The decoded string is: " + first);
}
}
Also typically the if else
will be paired with an additional else afterwards to handle all other cases, like so:
if (condition){//Check one case
// do something
} else if (condition){//Check another case
// do something
} else { //All other cases
// do something else
}
add a comment |
Currently you have nested if
statements instead of else if
, where if the first letter is u, then you enter that block and check if the first letter is l. Keep in mind the code below will not work as you would like if the user enters strings such as "Unicorn".
import java.util.Scanner;
public class Testing{
public static void main(String args) {
Scanner input = new Scanner(System.in);
System.out.print("Please enter a uncoded string: ");
String first = input.nextLine();
input.close();
if (first.charAt(0) == 'u') {
first = first.toUpperCase();
} else if (first.charAt(0) == 'l') {
first = first.toLowerCase();
}
System.out.println("The decoded string is: " + first);
}
}
Also typically the if else
will be paired with an additional else afterwards to handle all other cases, like so:
if (condition){//Check one case
// do something
} else if (condition){//Check another case
// do something
} else { //All other cases
// do something else
}
Currently you have nested if
statements instead of else if
, where if the first letter is u, then you enter that block and check if the first letter is l. Keep in mind the code below will not work as you would like if the user enters strings such as "Unicorn".
import java.util.Scanner;
public class Testing{
public static void main(String args) {
Scanner input = new Scanner(System.in);
System.out.print("Please enter a uncoded string: ");
String first = input.nextLine();
input.close();
if (first.charAt(0) == 'u') {
first = first.toUpperCase();
} else if (first.charAt(0) == 'l') {
first = first.toLowerCase();
}
System.out.println("The decoded string is: " + first);
}
}
Also typically the if else
will be paired with an additional else afterwards to handle all other cases, like so:
if (condition){//Check one case
// do something
} else if (condition){//Check another case
// do something
} else { //All other cases
// do something else
}
edited Nov 19 '18 at 23:33
answered Nov 19 '18 at 23:24
M.GM.G
388310
388310
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%2f53383967%2fhaving-issue-with-else-if-in-java-where-need-to-have-second-example-of-text-in%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
Is there another String class in your project?
– shmosel
Nov 19 '18 at 23:13
Not yet, but would assume it would have to be, as just would be a second criteria that would need to be met. Was trying to use "else if" just to move onto a second scenario.
– AFanmhi
Nov 19 '18 at 23:21
You should definitely not have another String class. But if you did, it might explain the error you're seeing.
– shmosel
Nov 19 '18 at 23:22
2
The error is due to mistyping of charAt(int). You wrote chatAt
– M.G
Nov 19 '18 at 23:26