My program is supposed to take in a single line of integers and characters, separate the ints and chars, store them in two different linked lists, and run calculations based on the input. My code for the compute method:
public static float ComputeResults(LinkedList<Integer> ints, LinkedList<Character> chars){
//iterate through elements from the int linkedList
result = 0;
for (int counter_int = 0; counter_int < ints.size(); counter_int ){
for (int counter_char = 0; counter_char < chars.size(); counter_char ){
if (chars.get(counter_char).equals(" ") ){
result = result ints.get(counter_int);
}
else if (chars.get(counter_char).equals("-")){
result = result - ints.get(counter_int 1);
}
else if (chars.get(counter_char).equals("*")){
result = result * ints.get(counter_int 1);
}
else if (chars.get(counter_char).equals("/")){
result = result / ints.get(counter_int 1);
}
else {
System.out.println("no if statement is valid");
}
}
}
return result;
}
I understood despite what the user inputs, the code does not enter any of the if statements (because the character is never found equal to " ", "-", "*" or "/")? What am I doing wrong in my if condition?
My code for all other class:
UserInterface Class:
import java.util.*;
public class UserInterface {
//create an arraylist to store the numbers
//create another arraylist to store the characters
public static ArrayList<Integer> integers = new ArrayList<Integer>();
public static ArrayList<Character> characters = new ArrayList<Character>();
public static void PrintMenu(){
//ask the user to enter a single line of integers and characters
//ask the user to enter a formula with spaces between the ints and characters
Scanner input = new Scanner (System.in);
System.out.println("Please enter a single line of integers and operators for calculation");
System.out.println("put one space between the integers and characters");
System.out.println("Example of a correctly entered line for calculation: ");
System.out.println("23 4 78 / 31 - 2");
System.out.println("");
//take the input in as a string
String line;
line = input.nextLine();
//parse the line to separate each element of the line
//store them in an arraylist of string
ArrayList<String> elements = new ArrayList();
Scanner scanner = new Scanner(line);
scanner.useDelimiter(" ");
while (scanner.hasNext()){
elements.add(scanner.next());
}
// in the elements arraylist
//if the index of the element is even
//add to list of ints
//if the index of the element is odd
//add to list of chars
for (int i = 0; i<elements.size(); i ){
if (i % 2 == 0){
integers.add(Integer.parseInt(elements.get(i)));
}
else if (i % 2 == 1){
characters.add(elements.get(i).charAt(0));
}
}
}
public static void OutputResults(){
}
public UserInterface(){
}
}
Code for LListCalculator class:
import java.util.*;
public class LListCalculator {
static float result;
public static LinkedList LinkedListMaintenance(ArrayList list){
LinkedList linkedList = new LinkedList();
for (int i = 0; i<list.size(); i ){
linkedList.add(list.get(i));
}
return linkedList;
}
public static float ComputeResults(LinkedList<Integer> ints, LinkedList<Character> chars){
//as mentioned before
}
public LListCalculator() {
}
}
Code for main class:
import java.util.*;
public class LListDriver {
public static void main(String[] args)
{
UserInterface.PrintMenu();
LinkedList ints;
LinkedList chars;
ints = LListCalculator.LinkedListMaintenance(UserInterface.integers);
chars = LListCalculator.LinkedListMaintenance(UserInterface.characters);
System.out.println(chars);
float result;
result = LListCalculator.ComputeResults(ints, chars);
System.out.println(result);
}
}
CodePudding user response:
If your code is about to enter a condition - please check if you're comparing it to string or actual character:
For example:
@Test
public void test() {
Character theChar = ' ';
if (theChar.equals(" ")) {
System.out.println("Fine");
} else {
System.out.println("Wrong");
}
}
Will output "Wrong".
And the following will output "Fine":
@Test
public void test() {
Character theChar = ' ';
if (theChar.equals(' ')) {
System.out.println("Fine");
} else {
System.out.println("Wrong");
}
}
CodePudding user response:
equals method in any Java class is an overridden implementation of the equals method of Object class.
This means the signature of equals method is public boolean equals(Object obj)
In the implementation above, a Character instance is being compared to a String instance chars.get(counter_char).equals(" ").
This does not result in a compilation error as the input parameter type of equals method is Object(parent class of all other types in java, hence able to point to reference of any java type, in this case String type), but the equality check will fail at runtime as the types of LHS and RHS do not match.
To fix this simply update the equals to compare Character to character.
chars.get(counter_char).equals(' ')
Note: In Java, values within double quotes represent a sring constant, and values within single qoutes reprsent a character constant.
References:
