Question

In: Computer Science

Question for Java String[] tokens = expression.split(" "); for (String token : tokens) { if (token.equals("+")...

Question for Java

String[] tokens = expression.split(" ");

for (String token : tokens) {

if (token.equals("+") || token.equals("-")) {

while (!ops.isEmpty() && (ops.peek() == '+' || ops.peek() == '-' || ops.peek() == '*' || ops.peek() == '/')) {

   applyOp(values, ops);

}

What does the for (String tokens : token) mean?

What are the different ways to write this for loop?

Solutions

Expert Solution

String[] tokens = expression.split(" ");
for (String token : tokens) {
if (token.equals("+") || token.equals("-")) {
while (!ops.isEmpty() && (ops.peek() == '+' || ops.peek() == '-' || ops.peek() == '*' || ops.peek() == '/')) {
   applyOp(values, ops);
}

What does the for (String tokens : tokens) mean?
Answer:
--------
tokens is an array
so, with "for (String tokens : tokens)"
we iterate through all Strings in tokens array and variable token contains the string in tokens array

What are the different ways to write this for loop?
Answer:
---------
String token;
for (int i = 0; i < tokens.length; i++) {
    token = tokens[i];
    if (token.equals("+") || token.equals("-")) {
        while (!ops.isEmpty() && (ops.peek() == '+' || ops.peek() == '-' || ops.peek() == '*' || ops.peek() == '/')) {
           applyOp(values, ops);
    }
}

// This code is exactly as same as given code but uses a traditional for loop.

Related Solutions

In Java: Design a class that checks if a String is made of tokens of the...
In Java: Design a class that checks if a String is made of tokens of the same data type (for this, you may only consider four data types: boolean, int, double, or char). This class has two instance variables: the String of data and its delimiter. Other than the constructor, you should include a method, checkTokens, that takes one parameter, representing the data type expected (for example, 0 could represent boolean, 1 could represent int, 2 could represent double, and...
JAVA programming language: For refrence: nextInt ( ) Scans the next token of the input as...
JAVA programming language: For refrence: nextInt ( ) Scans the next token of the input as an int. How many times does the while loop execute, if the input is 38 20 4 0? Assume a Scanner object scnr has been instantiated, and that the tokens in the input are delimited by the space character. That means there are 4 tokens in the input. int num = 2000; while (num >= 20) { //Do something num = scnr.nextInt(); } select...
In java please Question: You are given a string s. Your task is to count the...
In java please Question: You are given a string s. Your task is to count the number of ways of splitting s into three non-empty parts a, b and c (s = a + b + c) in such a way that a + b, b + c and c + a are all different strings. For s = "xzxzx", the output should be countWaysToSplit(s) = 5. Consider all the ways to split s into three non-empty parts:
* the Algorithm: * write a java program to convert infix to postfix create(OpStk) OpStk.push("#") token...
* the Algorithm: * write a java program to convert infix to postfix create(OpStk) OpStk.push("#") token = nextToken() while (token != "#") if (token is an operand) append token to postfix expression else if (token is "(") // Left paren - Start of sub-expr OpStk.push( token ) else if (token is ")") // Right paren - End of sub-expr pop operators off the stack and append to the postfix expression - stop when you've popped a "(" else (token is...
Finish the following java question: Consider the following interface: interface Duty { public String getDuty(); }...
Finish the following java question: Consider the following interface: interface Duty { public String getDuty(); } Write a class called Student which implements Duty. Class Student adds 1 data field, id, and 2 methods, getId and setId, along with a 1-argument constructor. The duty of a Student is to study 40 hours a week. Write a class called Professor which implements Duty. Class Professor adds 1 data field, name, and 2 methods, getName and setName, along with a 1-argument constructor....
Write in Java: Write a method called: public static String[] noIdenticalCombine(String[] array1, String[] array2) { //...
Write in Java: Write a method called: public static String[] noIdenticalCombine(String[] array1, String[] array2) { // instructions: returns an array that contains all the Strings in array1 and array2 but without repetition. order does not matter, but it will return array1's elements and then array2's element that are not in array1. Assume there are no duplicates are in array1 and array2. Could use count which is how many str there are in array2, where !contains(array1, str). May an array of...
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String...
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns the ArrayList in reserve order in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the...
Java RECURSIVE methods: => intersection(String s1, String s2): takes two strings and returns the string consisting...
Java RECURSIVE methods: => intersection(String s1, String s2): takes two strings and returns the string consisting of all letters that appear in both s1 and s2. => union(String s1, String s2): takes two strings and returns the string consisting of all letters that appear in either s1 or s2. =>difference(String s1, String s2): takes two strings and returns the string consisting of all letters that appear only in s1.
This is my java method that is suppose to take data from a file(String, String, Double,...
This is my java method that is suppose to take data from a file(String, String, Double, Int ) and load that data into an object array. This is what I have and when I try to display the contents of this array it prints a "@" then some numbers and letters. which is not the correct output. any help correcting my method would be much appreciated. public void loadInventory(String fileName) { String inFileName = fileName; String id = null; String...
Question 4: What is meant by “token money”? Question 5: What is meant by legal tender?...
Question 4: What is meant by “token money”? Question 5: What is meant by legal tender? Question 6: What are the shortcomings of a barter system? Question 7: How do chartered banks create money? Question 8: What is meant by a bank run? Question 9: What is the difference between treasury bills and bonds? Question 10: Are credit cards money? Question 11: Why are there different definitions for the Canadian money supply? Question 12: What are Chartered banks? Question 13:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT