Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
college student learning java. I am working on a homework assignment on creating class exceptions. For some reason my if statement seems to keep generating my class exception method. Take a look.
public class ICDEDriver {
public static void main(String[] args) throws InvalidDocumentCodeException {
Scanner scan = new Scanner(System.in);
InvalidDocumentCodeException problem = new InvalidDocumentCodeException("this is my error message")
System.out.println("Enter a valid document code: ");
String code = scan.nextLine();
if(code.charAt(0) != 'U' || code.charAt(0) != 'C' || code.charAt(0) != 'P')
throw problem;
System.out.println("End of main method");
}
}
So my issue is that when I enter a "valid document code" (document code must start with U, C, or P) it still triggers the InvalidDocumentCodeException. How can I fix it to where the if statement will accept U, C, or P as the first character? Thanks!
Here try to use the && operator
if((code.charAt(0) != 'U' && code.charAt(0) != 'C' && code.charAt(0) != 'P'))
throw problem;
The || operator does evaluate to true even if one side of the logic is false, && will make sure all of them are true.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 months ago.
Improve this question
I am creating a program where the user enters a string 5 - 15 characters long and must not contain the letter 'a.' I cannot figure out how to do it and what the problem with the code below is. Any suggestions regarding what I can do?
// STRING BETWEEN 5 - 15 CHARACTERS, MUST NOT CONTAIN LETTER 'a'
String lengthA;
System.out.println("Enter a string that is 5 - 15 characters long without using the letter 'a', 'A': ");
while(true) {
lengthA = keyboard.nextLine();
if (lengthA.length() < 5 - 15 && lengthA.contains("a", "A")) {
System.out.println("Try again");
} else {
System.out.println("Congratulations! You have passed!");
break;
}
}
UPDATE: I have tried the codes that were suggested, however, all of them only check if the string contains the letter 'a'. It does not check for the length requirement. Any way to make sure that both the length and no 'a' requirement is there? Please and thank you!
Try
if ( lengthA.length()< 5 || lengthA.length()>15 || lengthA.toLowerCase().contains("a")) {
System.out.println("Try again");
}
contains can take only 1 parameter thus lengthA.contains("a", "A") its invalid. By converting the input in either lowercase or uppercase you can simply check whether the input contains your mentioned character sequences or not.
Think logically what you want and implement them one after another. Your conditioning logic is also wrong.
Another variant to Sayan post
Update the condition to check the length is between 5 and 15 and if the string contains one or more a or A using Regex
if (lengthA.length() < 5 || lengthA.length() > 15 || lengthA.matches("[aA]+")) {
System.out.println("Try again");
} else {
System.out.println("Congratulations! You have passed!");
break;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
/*package whatever //do not write package name here */
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
public static void main (String[] args) {
//code
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t--!=0)
{
String S = sc.nextLine();
char []arr=S.toCharArray();
Arrays.stream(arr).filter(arr.hasLowerCase()).filter(arr.isUpperCase()).filter(arr.isNumeric()).filter(forEach(arr->System.out.println("Yes")));
}
}
}
You could use the methods defined in Character class to filter the input character stream.
input.chars()
.filter(X -> Character.isUpperCase(X) ||
Character.isLowerCase(X) ||
Character.isDigit(X)
)
.forEach(System.out::println);
EDIT:
To get a boolean result of whether such character exists in the stream, use anyMatch() instead of filter.
if (input.chars()
.anyMatch(X -> Character.isUpperCase(X) || Character.isLowerCase(X) || Character.isDigit(X))) {
System.out.println("Yes");
} else {
System.out.println("No");
}
EDIT 2:
Im not sure if you are checking if the stream contains ALL OF THEM or AT LEAST ONE OF THEM. The above answer is for contains at least one. If you are looking for all of the characters, just change the || to &&.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Why is the below code not working when using "!" not equals; it gave me "while" statement has an empty body? and when I'm removing the "!" it works...
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String input = "";
while (!input.equals("quit")); {
System.out.print("Input : ");
input = scanner.next().toLowerCase();
System.out.println(input);
}
}
}
you have to remove the semicolon
while(!input.equals("quit")) {
...
}
Remove the ; in the loop before {
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am trying to iterate through each character in a string that is inputted and check if any letter is a.
Here is my Java code:
import java.util.Scanner;
public class Input
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String input;
input = in.nextLine();
for (int i=0; i<input.length(); i++)
{
char chararcter = input.charAt(i);
String s = Character.toString(character); //the error is here
if (s.equals("a"))
{
System.out.println("You typed an A.");
}
}
}
}
For reference, here is a Python analog.
input=raw_input()
for i in range (0,len(input)):
if input[i] == "a":
print "You typed an A."
I apologize for the simplistic nature of this question; I'm very new to Java. Thank you for helping.
You've change the spelling in your declaration.
char character = input.charAt(i); // <-- not chararcter (extra rc).
You don't have to convert character to String. just simply do character comparison.
if (chararcter == 'a')
{
System.out.println("You typed an A.");
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
The code runs properly the first time then run it again using the while loop and lets say the first time I entered AA and it becomes CC then it runs again I enter AA again it will come out with CCCC do it again it comes out with CCCCCC I don't want that I need it to not keep the data from the string each time it loops.
import java.util.*;
public class SecretCypher {
public static void main(String args[]) {
Scanner kb = new Scanner(System.in);
StringBuffer e = new StringBuffer();
System.out.println("Welcome to Secret Cypher!");
char loop = 'Y';
while(loop == 'Y' || loop == 'y') {
System.out.println("");
System.out.println("Enter your cypher in upper case.");
String s = kb.nextLine();
char[] cs = s.toCharArray();
for (int i = 0; i < cs.length; i++) {
e.append((char)('A' + (cs[i] - 'A' + 2) % 26));
}
if(s == s.toLowerCase()) {
System.out.println("Remember to use upper case letters!");
System.exit(0);//Also I was bored of using break and this works any where in the code.
}
System.out.println(e.toString());
System.out.println("Do you want to enter another cypher? > ");
String again = kb.nextLine();
if(again.charAt(0) == 'N') {
System.out.println("Hope you come back again!");
break;
}
}
}
}
You're reusing the same string buffer. If you keep putting things into the same buffer without clearing it, you're obviously going to get extraneous stuff from previous iterations.
Simply declare the StringBuffer inside the while loop so that it is created on each iteration.
Anyway, you should learn to use your debugger, instead of asking here for us to debug. If anything, using the debugger can offer extremely valuable insight into the troubles that you are having here.