I've fixed my code so that it recognizes if their's 4 digits and less or 6 digits and higher but now I want to know whether or not it contains letters within the numbers.
The code below detects the letters and prints the line I want only when I input 5 letters, and I want it to detect even if their's more digits than letters or more letters than digits.
String digit;
String regex;
String regex1;
regex = "[0-9]{5}";
regex1 = "^[a-zA-Z0-9]{5}";
String test;
String validLength= "5";
char one, two, three, four, five;
{
System.out.println("In this game, you will have to input 5 digits.");
do
{
System.out.println("Please input 5-digits.");
digit = console.next();
test = digit.replaceAll("[a-zA-Z]", "");
if (digit.matches(regex))
{
one = (char)digit.charAt(0);
two = (char)digit.charAt(1);
three = (char)digit.charAt(2);
four = (char)digit.charAt(3);
five = (char)digit.charAt(4);
System.out.println((one + two + three + four + five) / 2 );
}
else if (test.length() > 5 || test.length() < 5)
{
System.out.println("You have letters in there.");
}
else if (digit.matches(regex1))
{
test = digit.replaceAll("[a-zA-Z]", "");
System.out.println("You have letters in there.");
}
else
if (digit.length() < 5)
{
System.out.println("You don't have enough digits.");
}
else if (digit.length() > 5)
{
System.out.println("You have to many digits.");
}
} while (!digit.matches(regex));
I won't go into regex here because honestly I think there is some misunderstandings that should be dealt with far before the road of niceties is undergone; plus, I'm no expert and I personally think they are more like rights of passage.
Anyway, let's start from the beginning, or at least when you determine they've entered a valid digit,
if (digit.matches(regex)).
Let's say...
String digits = "12345";
System.out.println(getSum(digits) / 2);
where...
public int getSum(String digits) {
int sum = 0;
for(int i = sum; i < digits.length(); i++) {
sum += digits.charAt(i);
}
return sum;
}
Same as your System.out.println((one + two + three + four + five) / 2 );.
I hope the output of 127 makes you smile.
Going out on a limb, and since you didn't speak of the "char" value sum that, you expected it to treat your chars as decimal digits. Well, that would result in 7. I only guess because of the whole (char)charAt() thing. This -> (char)charAt() sort of shows a lack of understanding that would make the use of regex highly questionable, IMHO of course.
Beyond that else if (test.length() > 5 && test.length() < 5). This says, "if test's length is greater AND less than 5"! Without using some mathematical paradox, tell me that number.
So, on to your question -
but now i want to know whether or not it contains letters within the
numbers.
well let's look at how finding out if any non-digit exists might be done - without regex so we can understand it...
public boolean containsNonDigits(String digits) {
for(int i = 0; i < digits.length(); ++i) {
if(Character.isDigit(digits.charAt(i))) {
continue;
} else {
return true;
}
}
return false;
}
This says, "if the character is a digit keep going; everything's fine, otherwise false".
The other "question" -
i want it to detect even if their's more digits than letters or more
letters than digits.
is an "additive" to the method above so I'll leave that one to you.
You can use Charachter.isDigit(char) and Charachter.isLetter(char) methods.
Here code sample that implements what you asked:
public static void main(String[] args)
{
System.out.println("In this game, you will have to input 5 digits.");
int validLength = 5;
boolean valid = false;
Scanner console = new Scanner(System.in);
while (!valid)
{
System.out.println("Please input 5-digits.");
String digit = console.next();
if (digit.length() != validLength)
{
//if length not valid, mark as not valid and return to next iteration
valid = false;
String message = digit.length() < validLength ? "You don't have enoght digits." : "You have to many digits.";
System.out.println(message);
continue;
}
//here digit.length = 5
int nDigits = 0,nLetters = 0,sum = 0;
for (int i = 0; i < digit.length(); i++)
{
Character ch = digit.charAt(i);
if (Character.isDigit(ch))
{
nDigits++;
sum += Integer.parseInt(ch.toString());
}
else if (Character.isLetter(ch)) {
nLetters++;
}
}
if (nLetters == 0 /* no letters */
|| /* and */
nDigits == validLength /* all chars are digits */)
{
System.out.println(sum/2);
valid = true;
}
else{
System.out.println("You have letters in there.");
}
}
}
Related
I am a student going into software engineering and am taking my first computer science class on coding with java. I'm struggling to get my program to apply a certain set of conditions to a String in order to print a message regarding whether or not a user's input is indeed a valid double literal.
Here are the conditions:
Consists of exactly the following characters: ’+’, ’-’, ’.’ (decimal point), and ’0’ through ’9’
Either the ’+’ or ’-’ character may appear only as the first character
The ’.’ (decimal point) character must appear exactly once
All other characters must be the ’0’ through ’9’ characters
And here is my code:
String doubleliteral;
int a;
char j;
char k;
char l;
char m;
char n;
System.out.print("Enter a valid four character double literal: ");
doubleliteral = keyboard.nextLine();
a = doubleliteral.length();
j = doubleliteral.charAt(0);
k = doubleliteral.charAt(1);
l = doubleliteral.charAt(2);
m = doubleliteral.charAt(3);
n = '.';
char i = (char)(a + 0);
if (i <= 4)
{
System.out.print("\n" + doubleliteral + " is a valid four character double literal.");
}
else if ((j == '+') || (j == '-') || (j <= 9))
{
}
else if ((k <= 9))
{
}
else if (l <=9)
{
}
else if (m <= 9)
{
}
else if ((j != '.') && (k != '.') && (l != '.') && (m != '.'))
{
System.out.print("\n" + doubleliteral + " is not a valid four character double literal.");
}
else
{
System.out.print("\n" + doubleliteral + " is not a valid four character double literal.");
}
keyboard.close();
I've been searching on the internet, in my textbook, and through my professor's presentations for about three days now trying to figure this out but nothing has come of it, please help.
Here is some psudo-code to get you in the right direction. I can't write your school answer here.
Loop through each character. Special case the first. Track if you have dot.
hasDot = false
for (i=0; i<input.length; i++) {
ch = input[i]
if (i === 0) {
validChars = [0,1,2,3,4,5,6,7,8,9,-,+,.]
// validChars must contain ch
// if ch === dot, set hasDot = true
}
else {
validChars = [0,1,2,3,4,5,6,7,8,9,.]
// validChars must contain ch
// if ch === dot and hasDot then error, else set hasDot = true
}
}
There are few conditions you need to check to figure out the validity. Let's see one by one.
+ Or -
There can be maximum one + or - operator and it has to be at the beginning of the given input.
So what you can do is, whenever you're getting a + or - operator, just check whether current index is 0 or not.
If it's not, then mark the input as invalid.
boolean isValid = true;
for(int i = 0; i < doubleliteral.length(); i++)
{
if((doubleliteral[i] == '+' || doubleliteral[i] == '-'))
{
if(i != 0)
{
isValid = false;
}
else
{
// it's valid.
}
}
}
The . (decimal point) character
There can be maximum one . character.
So you keep a count of it, check if it ever becomes more than 1.
boolean isValid = true;
int dotCounter = 0;
for(int i = 0; i < doubleliteral.length(); i++)
{
if( doubleliteral[i] == '.')
{
if(dotCounter != 0)
{
isValid = false;
}
else dotCounter++;
}
}
Check if there's any character other than digits (o to 9)
While traversing each character of the string, you can check if it's digit or not like following-
for(int i = 0; i < doubleliteral.length(); i++)
{
if(Character.isDigit(doubleliteral.charAt(i)))
{
// it's a digit
}
}
Now if we combine everything it'll look like this:
boolean isValid = true;
int dotCounter = 0;
int doubleliteralLength = doubleliteral.length();
for(int i = 0; i < doubleliteralLength; i++)
{
if((doubleliteral[i] == '+' || doubleliteral[i] == '-'))
{
if(i != 0)
{
isValid = false;
break; // we don't need to check any further
}
}
else if( doubleliteral[i] == '.')
{
if(dotCounter != 0)
{
isValid = false;
break;
}
else dotCounter++;
}
else if(Character.isDigit(doubleliteral.charAt(i)) != true)
{
// it's not a digit, not + or -, not .
isValid = false;
break;
}
}
if(doubleliteralLength == 4 && isValid)
{
System.out.print("\n" + doubleliteral + " is a valid four character double literal.");
}
else
{
System.out.print("\n" + doubleliteral + " is not a valid four character double literal.");
}
This question can be easily answered by using Regular Expressions and apply the expression to the inputted String object. Although this question is technically correct, it is not correct for the lesson your professor is teaching (which I am guessing is String parsing).
That said, here are my clues to you:
Symbols (+ or -) can only be present at character index 0.
The (decimal) ASCII values for numbers 0-9 are 48-57. Therefore, an ASCII
value outside this range that is not a "+", "-", or "." (43, 45, or
46 respectively) is an illegal character.
The next thing you need to do is write down a few combinations of characters and test them out manually. For example:
1.34 // invalid (first character must be the sign)
.123 // invalid (first character must be the sign)
-.98 // valid
-0.1 // valid
+2.3 // valid
2.+6 // invalid
4.3- // invalid
30-4 // invalid
+23. // valid (by your requirements, but it should not be)
+0.- // invalid
All of the above contain nothing but valid characters, but not all are correctly formatted. Once you test these out, you should try using other non-numeric characters.
Your code should be something like this (in psedocode)
IF STRING LENGTH > 4, ERROR (String has too many characters)
IF PERIOD COUNT > 1, ERROR // See * note
IF CHAR AT 0 NOT VALID, ERROR (Special case for symbols, period, or numbers)
IF CHAR AT X NOT VALID, ERROR (index 1-3 can only be numbers or the period)
IF CHAR AT 3 == ., ERROR (Last index cannot be the period)
PRINT STING MESSAGE
(*) My suggestion is to use a loop operation to count the number of times the period appears on the String. There are other ways to do this, but my guess is that they are not related to your current lesson. Some of those alternative methods are using regular expressions, using Streams API, using recursion, or using 3rd party libraries. Stay away from any of those solutions.
If all the tests passed, then the only thing that will be printed out will be the message containing the valid numeric string.
Last hint: Do all of the CHAR AT evaluations in a single pass (i.e. loop through your String (array of characters) to determine if the character at any given position is valid. A multiple pass solution is OK, just not elegant or effective.
The solution
import java.util.Scanner;
public class NumericStringEvaluator {
public static void main (String[] args) {
System.out.print("Enter a valid four character double literal: ");
Scanner scanner = new Scanner(System.in);
String input = scanner.next();
scanner.close();
if (input.length() > 4) {
System.err.println("String must be exactly 4 characters");
System.exit(-1);
}
//1. Consists of exactly the following characters: ’+’, ’-’ ...
//2. Either the ’+’ or ’-’ character may appear only as the first character
if (input.charAt(0) != '+' && input.charAt(0) != '-') {
System.err.println("Invalid character at index 0: " + input.charAt(0));
System.exit(0);
}
int periodCount = 0;
for (int i = 1; i < 4; i++) {
char c = input.charAt(i);
//1. Consists of ... ’.’ ...
if (c == '.') {
periodCount++; //3. The ’.’ (decimal point) character must appear exactly once
}
// 1. Consists of ... and ’0’ through ’9’
// 4. All other characters must be the ’0’ through ’9’ characters
// ASCII values for number are 48-57 (anything outside the range is not a number)
// and the special characters have been accounted for already
else if (c < 48 || c > 57) {
System.err.println("Invalid character at index " + i + ": " + input.charAt(i));
System.exit(0);
}
}
// At this point, every character in the string has been validated (no errors found)
//3. The ’.’ (decimal point) character must appear exactly once
if (periodCount != 1) {
System.err.println("Decimal point appears more than once in numeric String.");
System.exit(0);
} else {
System.out.print("\n" + input + " is a valid four character double literal.");
}
}
}
I'm trying to write a simple program that asks the user for an input consisting of zeroes and ones and checks every character looking for a double zero '00', if the current character is a '1' the program is considered to be in state "A" and so it prints the state and the character, if the character is a '0' the program is in state "B" and if there's a '00' the program is in state "C", after entering state "C" (after finding a '00') the program can't exit that state,that means that it will keep checking every character,but the resulting string should be "State C" + character even if the character is a single zero or one.
As of now I have something like this
import java.util.Scanner;
public class onesandzeroes {
public static void main(String[] args) {
System.out.println("Write a String that consists of 0 and 1");
Scanner scanner = new Scanner(System. in);
String inputString = scanner.nextLine();
for (int index = 0; index < inputString.length();
index++) {
char aChar = inputString.charAt(index);
if (aChar == '0'){
System.out.println("State B " + aChar);
/* This is the part I'm having trouble with, I was thinking about something like
if(aChar =='0' && charAt(index + 1 == '0')){
System.out.println("State C" + aChar + charAt(index + 1);
}
to look for a '0' that is followed by another '0' but it doesn't work
*/
} else{
System.out.println("State A " + aChar);
}
}
}
}
I know that you can probably look the entire string and only check if there's a '00' but I want to check every character individually except when it looks for '00'
So I have 2 questions:
How can I look for a '00' in the string? The ideal way would be to check every '0' for a following '0'.
And how can I make the program print "State C" + character ONLY after the first '00' has been found? that is after the program finds a '00' it should stay in State C for '00' and every other character following it.
maybe this way:
import java.util.Scanner;
public class StringScanner {
public static void main(String[] args) {
System.out.println("Write a String that consists of 0 and 1");
final var scanner = new Scanner(System.in);
final String inputString = scanner.nextLine();
var currentState = State.A;
for (int index = 0; index < inputString.length(); index++) {
final char currentChar = inputString.charAt(index);
System.out.println("The current char is " + currentChar + " and I'm in state " + currentState);
if (currentState == State.C) {
// do nothing because final state has already been reached
continue;
}
if (currentState == State.B) {
if (currentChar == '0') {
// 2 zeros found got into final state :)
currentState = State.C;
} else {
//
currentState = State.A;
}
continue;
}
if (currentState == State.A) {
if (currentChar == '0') {
currentState = State.B;
}
}
}
}
private enum State {
A, B, C
}
}
Firstly you can check inputString.contains("00");
If true write
inputString.charAt(inputString.indexOf("00") + 2);
The problem code is below, if you need the entire main method to help me, please ask. The code complies but does not run as expected. I am trying to make the code report back an exclamation mark if the number is out of bounds/larger than the last position of the source text, which is a string the user inputs, so the length cannot be predefined. Exception is 'StringIndexOutOfBoundsException'
TDLR num is an int, sourcetext is a string, both are inputs. Exception: when code should output an '!' instead.
import java.util.Scanner;
public class Temp {
public static void main(String[] args) {
Scanner sc;
int result, num= 0, end = -2, temp, infolost, count;
String word, sourcetext, answer, space= " ";
String sourcetext2, temp2;
char input, result2, chalost;
sc = new Scanner(System.in);
System.out.println("please enter sourcetext");
sourcetext = sc.nextLine(); // user inputs source text
sourcetext = sourcetext.toLowerCase(); // converts sourcetext into lowercase
System.out.print("Would you like to 1 encrypt, or 2 decrypt?");
answer = sc.next(); // user inputs choice
if (answer.equals("1")||(answer.equals("encrypt"))) {
System.out.println("Please enter at least one word to encrypt");
word = sc.next(); // user inputs one word
for (int i= 0; i < word.length(); i++) {
temp = sourcetext.indexOf(word.charAt(i)); // uses index to convert char positions int num
System.out.print(space + temp + space);
}
System.out.print(space + end);
}
else if (answer.equals("2")||(answer.equals("decrypt"))) {
System.out.println("Please enter digits, with one space between each. End with -2");
while (num > -2) {
num = sc.nextInt(); // num to decrypt
if (num > -2) {
result2 = sourcetext.charAt(num); // num converted into characters
System.out.print(result2);
} else if (num > sourcetext.length()) {
System.out.print("!");
} else if (num<0) {
System.out.print("end");
}
}
}
}
}
Try it like this:
int stringLength = sourcetext.length();
if (num > stringLength) {
System.out.print("!");
}
else if (num<0) {
System.out.print("end");
}
This could lead to an IndexOutOfBoundsException - since -1 is greater than -2 - but still out of bounds...
if (num > -2){
result2 = sourcetext.charAt(num); // num converted into characters
System.out.print(sourcetext.indexOf(num));
}
Edit: Unless the users input is -2 - the first if-Statement will always run... You probably need to re-work the logic there.
Edit2: If num is -1 sourcetext.charAt(num); leads to an IndexOutOfBounds. Do something like
if(num == -2) {
System.out.print("end");
} else if (num >= 0 && num < sourcetext.lenght()) {
// index ok
result2 = sourcetext.charAt(num); // num converted into characters
System.out.print(result2);
} else {
// index out of bounds
System.out.print("!");
}
I am submitting the following code to an online judge.
Input Specification
The data provided by Bill and Ted has the following format: The first line consists of the number p of parties followed by the number g of guesses (with 1 ≤ p ≤ 50 and 1 ≤ g ≤ 10000). Then follow p lines, each line consisting of a unique party name of length ≤ 20 (only containing letters a-z, A-Z and digits 0-9) and the achieved vote percentage of this party with one digit after the decimal point. After the parties follow g lines, each consisting of a guess. A guess has the form P1 + P2 + ... + Pk COMP n, where P1 to Pk are party names, COMP is one of the comparison operators <, >, <=, >= or = and n is an integer between 0 and 100, inclusively. Each party name occurs at most once in each guess.
Output Specification
For each guess, sum up the vote percentages of the parties and compare them with the specified integer n. Then, print a line stating whether the guess was correct. See the sample output for details.
but whatever i do it always gives me time limit exceeded ...and here is my final code,
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String Ints = br.readLine();
String Guess;
String [] Ints_array = Ints.split("[ ]+");
int p = Integer.parseInt(Ints_array[0]);
int g = Integer.parseInt(Ints_array[1]);
HashMap<String, Float> Parties = new HashMap<String, Float>();
for(int i = 0 ; i < p ; i++) {
String temp = br.readLine();
String [] temp_array = temp.split("[ ]+");
Parties.put(temp_array[0],Float.parseFloat(temp_array[1]));
}
for(int j = 0 ; j < g ; j++) {
Guess = br.readLine();
float actual = 0;
String [] temp = Guess.split("[ ]+");
if(Guess.contains("+"))
{
for(int i = 0 ; i < temp.length-2 ; i++)
{
if(!temp[i].contains("+"))
actual+= Parties.get(temp[i]);
}
}
else
{
actual=Parties.get(temp[0]);
}
float guess = Float.parseFloat(temp[temp.length-1]);
if( temp[temp.length-2].contains(">"))
{
if(temp[temp.length-2].contains("="))
{
if(actual >= guess)
System.out.println("Guess #"+(j+1)+" was correct.");
else
System.out.println("Guess #"+(j+1)+" was incorrect.");
}
else
{
if(actual > guess)
System.out.println("Guess #"+(j+1)+" was correct.");
else
System.out.println("Guess #"+(j+1)+" was incorrect.");
}
}
else if( temp[temp.length-2].equals("<"))
{
if(temp[temp.length-2].contains("="))
{
if(actual <= guess)
System.out.println("Guess #"+(j+1)+" was correct.");
else
System.out.println("Guess #"+(j+1)+" was incorrect.");
}
else
{
if(actual < guess)
System.out.println("Guess #"+(j+1)+" was correct.");
else
System.out.println("Guess #"+(j+1)+" was incorrect.");
}
}
else if( temp[temp.length-2].equals("="))
{
if(actual == guess)
System.out.println("Guess #"+(j+1)+" was correct.");
else
System.out.println("Guess #"+(j+1)+" was incorrect.");
}
}
}
}
I'll write a part of the answer, because I think that it would take a good search to have a complete answer.
Try to use Set in place of Arrays, because the function contains would be faster.
Raw String split is not the faster way to make a split. Stack Overflow question Performance of StringTokenizer class vs. split method in Java is a comparison of splitting methods.
Hope this would help.
Could someone explain why I am getting no output from this program. I am trying to create a morse code translator for digits using a for loop and if statements but nothing appears and I put in the morse code I want to have translated.
import acm.program.*;
public class MorseDecoder extends ConsoleProgram
{
public void run()
{
String morseDig = "";
int digit = 0;
String morse = readLine("Please enter the morse code: ");
for (int i = 0; i == morse.length(); i += 5)
{
if (morseDig == ".----"){
digit = 1;
}
else if(morseDig == "..---"){
digit = 2;
System.out.println(digit);
}
else if(morseDig == "...--"){
digit = 3;
}
else if(morseDig == "....-"){
digit = 4;
}
else if(morseDig == "....."){
digit = 5;
}
else if(morseDig == "-...."){
digit = 6;
}
else if(morseDig == "--..."){
digit = 7;
}
else if(morseDig == "---.."){
digit = 8;
}
else if(morseDig == "----."){
digit = 9;
}
else if(morseDig == "-----"){
digit = 0;
}
else{
println("NaN");
}
}
}
//~ Methods ...............................................................
}
the NaN at the end is used when the morse code has no numeric translation.
".....".equals(morseDig) etc on down the line. Comparing Strings with == in Java doesn't do what you think it does.
You're also not actually taking user input and setting morseDig anywhere. Your for loop isn't doing what you think because morse.length() is returning 0 and you're not executing the body even once.