I am trying to calculate an arithmetic expression, which is entered as a string (for example, ( 5+4*5-1/8 ), which will give the result 3). I enter an expression and convert it into an array. First; the result will start with the first element and it will change in the loop. But the problem is operator precedence. How can I use the operator presedence in a loop? Here is my code:
import java.util.Scanner;
public class HesapMakinesi {
private char value[];
private int count;
private Scanner str = new Scanner(System.in);
private String process;
HesapMakinesi() {
System.out.print("Enter the process ");
process = str.next();
//System.out.println(islem);
Initializer(process);
}
private void Initializer(String process) {
count = process.toCharArray().length;
value = new char [count];
int i;
System.arraycopy(process.toCharArray(), 0, value, 0, count);
//System.out.println(value);
if(value[0]=='-' || value[0]=='+' || value[0]=='/' || value[0]=='*' || // A process cannot start with an operator
value[count-1]=='-' || value[count-1]=='+' || value[count-1]=='/' || value[count-1]=='*') {
System.out.println("You have entered a wrong process.Please enter again!!!");
System.out.print("Enter the process: ");
process = str.next();
Initializer(process);
}
for(i=0; i<count; i++) { // A process cannot include a character except operators
if( value[i]!='+' && value[i]!='-' && value[i]!='*' && value[i]!='/' && value[i]!='(' && value[i]!=')' && !Character.isDigit(value[i]) ) {
System.out.println("You have entered a wrong process.Please enter again!!!");
System.out.print("Enter the process: ");
process = str.next();
Initializer(process);
}
}
for(i=0; i<count-1; i++) { // A process cannot have operators sequantially
if( !Character.isDigit(value[i]) && !Character.isDigit(value[i+1]) ) {
if( (value[i] == '+' && value[i+1] == '+' ) || (value[i] == '+' && value[i+1] == '-' ) || (value[i] == '+' && value[i+1] == '*' ) ||
(value[i] == '+' && value[i+1] == '/' ) ) {
System.out.println("You have entered a wrong process.Please enter again!!!");
System.out.print("Enter the process: ");
process = str.next();
Initializer(process);
}
else if( (value[i] == '-' && value[i+1] == '+' ) || (value[i] == '-' && value[i+1] == '-' ) || (value[i] == '-' && value[i+1] == '*' ) ||
(value[i] == '-' && value[i+1] == '/' ) ) {
System.out.println("You have entered a wrong process.Please enter again!!!");
System.out.print("Enter the process: ");
process = str.next();
Initializer(process);
}
else if( (value[i] == '*' && value[i+1] == '+' ) || (value[i] == '*' && value[i+1] == '-' ) || (value[i] == '*' && value[i+1] == '*' ) ||
(value[i] == '*' && value[i+1] == '/' ) ) {
System.out.println("You have entered a wrong process.Please enter again!!!");
System.out.print("Enter the process: ");
process = str.next();
Initializer(process);
}
else if( (value[i] == '/' && value[i+1] == '+' ) || (value[i] == '/' && value[i+1] == '-' ) || (value[i] == '/' && value[i+1] == '*' ) ||
(value[i] == '/' && value[i+1] == '/' ) ) {
System.out.println("You have entered a wrong process.Please enter again!!!");
System.out.print("Enter the process: ");
process = str.next();
Initializer(process);
}
}
}
//sCount();
}
/*private void Count(){
double result,temp;
int i;
for(i=0; i<count; i++) {
if( value[i]!= )
}
}*/
}
That's not how you do it. You need to parse the expression before evaluating it. I suggest you to read the Shunting-yard algorithm.
Following on from my comment... If you're dealing with a simple expression where you can only have numbers and signs +-/* then you can have a simple approach:
split your expression by lowest precedence operators first (+-) remembering the signs.
Compute each piece - as now the precedence isn't important, since everything is of the same level
sum those computed pieces taking into account the sign of the piece from step 1.
In your example, you'll end up with something like this:
(Split by +-) three pieces: (1) 5; (2) 4*5 with sign +; (3) 1/8 with sign -
Compute each of the three pieces: (1) 5; (2) 20; (3) 0.125
Sum the three pieces with their respective signs: 5+20-0.125 = 24.875
Related
This program takes in a statement and then print the number of vowels, numerics, etc. I wanted to know how I could print the list of numerics and vowels which have been given as input.
Can someone tell me how this could be done using an array?
package strings;
import java.util.Scanner;
public class Strings
{
public static void main(String [] abc)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a statement:");
String statement = sc.nextLine().toLowerCase();
System.out.println("------------------------------------------------");
System.out.println("Total characters: " + statement.length());
int vowels = 0,num = 0,spaces = 0,spl = 0,others = 0;
char alpha;
for (int i = 0;i < statement.length();i++)
{
alpha = statement.charAt(i);
if (alpha == 'a' || alpha == 'e' || alpha == 'i' || alpha == 'o' || alpha == 'u')
{
vowels++;
}
else if (alpha == ' ')
{
spaces++;
}
else if (alpha == '0' || alpha == '1' || alpha == '2' || alpha == '3' || alpha == '4' || alpha == '5' || alpha == '6' || alpha == '7' || alpha == '8' || alpha == '9')
{
num++;
}
else if (alpha =='!' || alpha =='#' || alpha =='#' || alpha =='$' || alpha =='%' || alpha =='^' || alpha =='&' || alpha =='*' || alpha =='(' || alpha ==')')
{
spl++;
}
else
{
others++;
}
}
System.out.println("Total vowels: " + vowels);
System.out.println("Total spaces: " + spaces);
System.out.println("Total numerics: " + num);
System.out.println("Total special characters: " + spl);
System.out.println("Other characters: " + others);
System.out.println("");
for (int i = 0;i < statement.length();i++)
{
System.out.println("The vowels are as follows:");
System.out.println(alpha);
}
}
}
You can use java.util.ArrayList, for example, create an ArrayList for vowels:
List<Character> vowelList = new ArrayList<>();
...
for (int i = 0; i < statement.length(); i++) {
if (alpha == 'a' || alpha == 'e' || alpha == 'i' || alpha == 'o' || alpha == 'u') {
vowels++;
vowelList.add(alpha);
} else {
...
}
System.out.println("The vowels are as follows:");
System.out.println(vowelList);
If in case you don't want to use any external packages.
....
Character v[] = new Character[statement.length()];
....
if (alpha == 'a' || alpha == 'e' || alpha == 'i' || alpha == 'o' || alpha == 'u'){
v[vowels++] = alpha;
}
....
System.out.println("The vowels are as follows:");
for (Character c : v) {
if (c != null) {
System.out.println(c);
}
}
I am trying to write a program that takes a string as input.
This string must be an equation with only brackets, operations or digit.
The code I posted below is what I came up with, but when I run it, anything I enter is apparently an invalid string. Can someone please tell what I did wrong?
System.out.println("Enter a string");
String str = s.nextLine(); //s is a Scanner object
int n = str.length();
for(int i = 0; i < n; i++) {
if( !Character.isDigit(str.charAt(i)) || str.charAt(i) != '+'
|| str.charAt(i) != '-' || str.charAt(i) != '*'
|| str.charAt(i) != '/' || str.charAt(i) != '('
|| str.charAt(i) != ')' || str.charAt(i) != '['
|| str.charAt(i) != ']' || str.charAt(i) != '{'
|| str.charAt(i) != '}') {
System.out.println("invalid string, try again: ");
str = s.nextLine();
}
...}
As suggested in the comments, switch || for &&:
if( !Character.isDigit(str.charAt(i)) && str.charAt(i) != '+'
&& str.charAt(i) != '-' && str.charAt(i) != '*'
&& str.charAt(i) != '/' && str.charAt(i) != '('
&& str.charAt(i) != ')' && str.charAt(i) != '['
&& str.charAt(i) != ']' && str.charAt(i) != '{'
&& str.charAt(i) != '}') {
System.out.println("invalid string, try again: ");
str = s.nextLine();
}
Or, to make your code easier to understand:
final String validChars = "0123456789+-*/()[]{}";
for(int i = 0; i < n; i++) {
if(!validChars.contains(str.charAt(i))) {
System.out.println("invalid string, try again: ");
str = s.nextLine();
// potential bug here - i and n are not being reset
}
}
Note: You have a bug where you don't reset the i index or the n length when reading a new line from the scanner in the case where your previous line contained an invalid character (see comment in code above).
if you dont want change all of your code you can that :
System.out.println("Enter a string");
String str = s.nextLine(); // s is a Scanner object
int n = str.length();
if (n > 0) {
for (int i = 0; i < n; i++) {
if (!(Character.isDigit(str.charAt(i)) || str.charAt(i) == '+' || str.charAt(i) == '-'
|| str.charAt(i) == '*' || str.charAt(i) == '/' || str.charAt(i) == '(' || str.charAt(i) == ')'
|| str.charAt(i) == '[' || str.charAt(i) == ']' || str.charAt(i) == '{'
|| str.charAt(i) == '}')) {
System.out.println("invalid string, try again: ");
str = s.nextLine();
n = str.length();
i = -1;//reset i for the new line ( set at -1 for the next i++ of the next loop )
}
}
} else {
System.out.println("empty string ");
}
package hello;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class Characts {
#Test
public void testSwitch () {
assertTrue(isValid("123+321*"));
assertFalse(isValid("A123+321*"));
assertTrue(isValid("123+321*\n")); // don't forget returns and line feeds
}
private boolean isValid(String str) {
for (int i = 0 ; i < str.length(); i++) {
char s = str.charAt(i);
if (Character.isDigit(s)) {
continue;
} else {
switch (s) {
case '+' :
case '-' :
case '*' :
case '/' :
case '(' :
case ')' :
case '[' :
case ']' :
case '{' :
case '}' :
continue;
}
return false;
}
}
return true;
}
}
I'm working on a server/client battleship game using sockets. Part of the project requires entry validation on the client side for entering tile locations. A user is supposed to enter a letter A-E and a number 1-5, and right now if you type in something invalid, it seems to freeze. Any help would be much appreciated, thanks in advance!
do{
System.out.println("------------------------------------------------------------------------------------------------");
System.out.println("Please type in a board position in the format of a letter followed by number, such as 'A1'. ");
Scanner sc = new Scanner(System.in);
String BoardChoice = sc.next();
if(BoardChoice.equals("A1" ) || BoardChoice.equals("B1" ) || BoardChoice.equals("C1" ) || BoardChoice.equals("D1" ) || BoardChoice.equals("E1" ) ||
BoardChoice.equals("A2" ) || BoardChoice.equals("B2" ) || BoardChoice.equals("C2" ) || BoardChoice.equals("D2" ) || BoardChoice.equals("E2" ) ||
BoardChoice.equals("A3" ) || BoardChoice.equals("B3" ) || BoardChoice.equals("C3" ) || BoardChoice.equals("D3" ) || BoardChoice.equals("E3" ) ||
BoardChoice.equals("A4" ) || BoardChoice.equals("B4" ) || BoardChoice.equals("C4" ) || BoardChoice.equals("D4" ) || BoardChoice.equals("E4" ) ||
BoardChoice.equals("A5" ) || BoardChoice.equals("B5" ) || BoardChoice.equals("C5" ) || BoardChoice.equals("D5" ) || BoardChoice.equals("E5" ))
{
flagtoo = false;
writer.writeUTF(BoardChoice);
}
else
{
System.out.println("Invalid Input Please re-enter!");
}
}while(flagtoo);
I would suggest you test each character separately by breaking them out with charAt, and please respect variable naming conventions. Something like
boolean valid = false;
String boardChoice = sc.nextLine(); // <-- not next
if (boardChoice.length() == 2) {
char col = boardChoice.charAt(0);
char row = boardChoice.charAt(1);
// The parenthesis here are just for clarity.
valid = ((col >= 'A' && col <= 'E') && (row >= '1' && row <= '5'));
}
My suggestion on the other hand will suggest to implement regex with a pattern that allows you to match 1st char alpha(no matter capitalization) followed by a number,
both in a range from a → e and 1 → 5
Scanner sc = new Scanner(System.in);
System.out.println("Please type in a ....y number, such as 'A1'. ");
do {
inp = sc.nextLine();
if (inp.matches("^[a-eA-E1-5]{0,2}")) {
inpArr[k++] = inp;
} else {
System.out.println("invalid input");
}
} while (k < numberOfElements);
System.out.println(Arrays.toString(inpArr));
I need to compare char values with set char values 'g' 'c' 'a' 't'(lower and upper case), for i want only those values to be entered. I can not seem to get certain cases of my input validation working.
f in the below strings can stand for any length of string that is not characters g,c,a,t.
The string "fffffff" keeps in the loop.
The string "fgf" keeps in the loop.
However, i want the strings, "fffffg" or "gfg" to exit the loop, and they are not doing so.
The actual purpose of the exercise, to take a user input of nucleotides like g,c,a,t like the one's in DNA, and convert them into the complementary string of RNA. G is complement to C and vice versa. A is complement to U(the T is replaced with U) and vice versa.
So if the string is "gcat", the response for RNA should be "cgua".
import java.text.DecimalFormat;
import javax.swing.SwingUtilities;
import javax.swing.JOptionPane;
import java.util.Random;
//getting my feet wet, 1/13/2015, program is to take a strand of nucleotides, G C A T, for DNA and give
//the complementary RNA strand, C G U A.
public class practiceSixty {
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
String input = null;
boolean loopControl = true;
char nucleotide;
while(loopControl == true)
{
input = JOptionPane.showInputDialog(null, " Enter the sequence of nucleotides(G,C,A and T) for DNA, no spaces ");
for(int i = 0; i < input.length(); i++)
{
nucleotide = input.charAt(i);
if(!(nucleotide == 'G' || nucleotide == 'g' || nucleotide == 'C' || nucleotide == 'c' || nucleotide == 'A' || nucleotide == 'a' || nucleotide == 'T' || nucleotide == 't' ))
{
loopControl = true;
}
else if(nucleotide == 'G' || nucleotide == 'g' || nucleotide == 'C' || nucleotide == 'c' || nucleotide == 'A' || nucleotide == 'a' || nucleotide == 'T' || nucleotide == 't' )
{
loopControl = false;
System.out.println(nucleotide);
}
}
}
JOptionPane.showMessageDialog(null, "the data you entered is " + input);
StringBuilder dna = new StringBuilder(input);
for(int i = 0; i < input.length(); i++)
{
nucleotide = input.charAt(i);
if(nucleotide == 'G' || nucleotide == 'g' )
{
dna.setCharAt(i, 'c');
}
else if( nucleotide == 'C' || nucleotide == 'c')
{
dna.setCharAt(i, 'g');
}
if(nucleotide == 'A' || nucleotide == 'a')
{
dna.setCharAt(i, 'u');
}
else if(nucleotide == 'T' || nucleotide == 't')
{
dna.setCharAt(i, 'a');
}
}
JOptionPane.showMessageDialog(null, "the DNA is , " + input + " the RNA is " + dna);
}
});
}
}
You could do your check with a single regular expression, and then just use a do/while loop to keep prompting for input until the user enters something valid.
do {
input = JOptionPane.showInputDialog(
null, " Enter the sequence of nucleotides(G,C,A and T) for DNA, no spaces ");
} while (!input.matches("[GCATgcat]+"));
The regular expression will match any input that consists of one or more letters of the 8 shown. When you don't get a match, the loop repeats.
I'm super new to programming so I would love to keep this simple. The compiler accepts my code, but when I run the program and type in for example the letter A I just get a ton of errors. I tried earlier using String letter instead of int letter, but I just got compiler errors stating I couldn't convert Strings to characters or something. I'm really confused and could use a quick explanation and fix so I can get a number back. Here's my code:
import java.util.Scanner;
import java.lang.String;
public class PhoneAlgorithm {
public static void main(String[] args){
int digit = -1;
Scanner in;
in = new Scanner(System.in);
System.out.print("Enter an uppercase letter to find out the corresponding digit on a telephone: ");
int letter;
letter = Integer.parseInt(in.next());
if (letter == 'A' || letter == 'B' || letter == 'C') {
digit = 2; }
else if (letter == 'D' || letter == 'E' || letter == 'F') {
digit = 3; }
else if (letter == 'G' || letter == 'H' || letter == 'I') {
digit = 4; }
else if (letter == 'J' || letter == 'K' || letter == 'L') {
digit = 5; }
else if (letter == 'M' || letter == 'N' || letter == 'O') {
digit = 6; }
else if (letter == 'P' || letter == 'Q' || letter == 'R' || letter == 'S') {
digit = 7; }
else if (letter == 'T' || letter == 'U' || letter == 'V') {
digit = 8; }
else if (letter == 'W' || letter == 'X' || letter == 'Y' || letter == 'Z') {
digit = 9; }
else if (letter >= 'a' && letter >= '3') {
System.out.print("You did not enter a valid uppercase letter. Try again!");
}
if (digit != -1) {
System.out.println("The corresponding digit on your telephone is: " + digit);
}
}
}
When you use parseInt(str), you will get an Exception if the parameter str cannot be converted to an integer.
You must use char, since you are comparing the input with single characters:
char letter;
letter = in.nextLine().charAt(0);
str.charAt(index) Returns the char value at the specified index.
I have modified your code, I guess this is what you are looking for..
import java.util.Scanner;
public class Try {
public static void main(String[] args) {
//declarations
char letter;
int digit=0;
// Asking the user to enterstring
System.out.println("Enter the string");
String enterString;
//creating a scanner object and reading the string
Scanner input = new Scanner(System.in);
enterString= input.next();
System.out.println("Entered string is "+enterString);
int temp=0;
for(int i=0;i<enterString.length();i++){
letter=(char)enterString.codePointAt(i);
if (letter == 'A' || letter == 'B' || letter == 'C') {
digit = digit*10+2; }
else if (letter == 'D' || letter == 'E' || letter == 'F') {
digit = digit*10+3; }
else if (letter == 'G' || letter == 'H' || letter == 'I') {
digit = digit*10+4; }
else if (letter == 'J' || letter == 'K' || letter == 'L') {
digit = digit*10+5; }
else if (letter == 'M' || letter == 'N' || letter == 'O') {
digit = digit*10+6; }
else if (letter == 'P' || letter == 'Q' || letter == 'R' || letter == 'S') {
digit = digit*10+7; }
else if (letter == 'T' || letter == 'U' || letter == 'V') {
digit = digit*10+8; }
else if (letter == 'W' || letter == 'X' || letter == 'Y' || letter == 'Z') {
digit = digit*10+9; }
else if (letter >= 'a' && letter >= '3') {
System.out.print("You did not enter a valid uppercase letter. Try again!");
}
/*if (digit != 0) {
System.out.println("The corresponding digit on your telephone is: " + digit);
}*/
}
if (digit != 0) {
System.out.println("The corresponding digit on your telephone is: " + digit);
}
}
}