I have to evaluate a prefix expression using stacks, I did it but I don't understand why the code doesn't work properly, it marks 2 bugs when I compile the code, they are:
Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
at evaluationprefix.EvaluationPreFix.EvaluationPrefix(EvaluationPreFix.java:56)
at evaluationprefix.EvaluationPreFix.main(EvaluationPreFix.java:25)
public class EvaluationPreFix {
public static void main(String[] args) {
Stack st = new Stack();
Scanner sc = new Scanner(System.in);
System.out.println("enter the size of expression");
int t = sc.nextInt();
sc.nextLine();
for (int i = 0; i < t; i++) {
System.out.println("enter an element");
String element = sc.nextLine();
st.push(element);
}
int r = EvaluationPrefix(st); //marks an Error here
System.out.println("Result: " + r);
}
public static int EvaluationPrefix(Stack st) {
Stack st2 = new Stack();
while (!st.isEmpty()) {
Object e = st.pop();
if (e.equals('+')) {
st2.push((Integer) st2.pop() + (Integer) st2.pop());
} else if (e.equals('-')) {
st2.push((Integer) st2.pop() - (Integer) st2.pop());
} else if (e.equals('*')) {
st2.push((Integer) st2.pop() * (Integer) st2.pop());
} else if (e.equals('/')) {
st2.push((Integer) st2.pop() / (Integer) st2.pop());
} else {
st2.push(e);
}
}
return (Integer) st2.pop();//marks an error here
}
}
Changes made:
In the main method, changed the stack, st, to String type.
In the EvaluationPrefix method,
changed the parameter stack to String type.
changed the stack, st2, to Integer type.
changed the arithmetical operators within equals to String.
Here you go,
public class EvaluationPreFix {
public static void main(String[] args) {
//1. parameterized with String
Stack<String> st = new Stack();
Scanner sc = new Scanner(System.in);
System.out.println("enter the size of expression");
int t = sc.nextInt();
sc.nextLine();
for (int i = 0; i < t; i++) {
System.out.println("enter an element");
String element = sc.nextLine();
st.push(element);
}
int r = EvaluationPrefix(st); //marks an Error here
System.out.println("Result: " + r);
}
//2. parameterized with String
public static int EvaluationPrefix(Stack<String> st) {
//3. parameterized with Integer
Stack<Integer> st2 = new Stack();
while (!st.isEmpty()) {
String e = st.pop();
//4. arithmetic sign comparison to string instead
//of character
if (e.equals("+")) {
st2.push(st2.pop() + st2.pop());
} else if (e.equals("-")) {
st2.push(st2.pop() - st2.pop());
} else if (e.equals("*")) {
st2.push(st2.pop() * st2.pop());
} else if (e.equals("/")) {
st2.push(st2.pop() / st2.pop());
} else {
st2.push(Integer.valueOf(e));
}
}
return st2.pop();
}
}
Assuming we are talking about java.util.stack - this is just a Collection storing what you push into it, and you are using it as raw type.
Stack st = new Stack();
That means you can push objects of any type onto this stack. It seems that you only want to store Integers - tell the compiler about that by using generics.
Stack<Integer> st = new Stack<>();
This will tell you that the problem is wherever you try to convert e to an Ìntegerby casting, because in your case, the values ofeare theStrings you pused intostinmain()`.
You should also replace the declaration of st in main with
Stack<String> st = new Stack<>();
and the method declaration to
public static int EvaluationPrefix(Stack<String> st)
to highlight the problem.
When you have a String and want to convert it to an Integer, you need to parse it, for example using Integer.parseInt. But you need to be aware that this method will throw a NumberFormatException if the String is not a number. You will have to handle this exception, for example by catching it and printing a helpful error message.
Related
The question is asking us to find whether a connection between two airlines are possible. For the input code, they give us the list of airlines and then which airlines are connected to which. I was able to read everything into the scanner and make a Hashmap of String values connecting to a String ArrayList, in order to compensate for duplicating keys. There is no issues with the Hashmap, as after reading out all of the keys and the corresponding values in their ArrayList, everything was matching up.
Here is the printed Hashmap below(some airlines are not listed below because they have no connections, ie: southwest):
continental, [america_west, air_china, alaska, air_france, virgin_atlantic]
austrian_airways, [delta]
virgin_atlantic, [air_france]
delta, [swiss_air, austrian_airways, air_canada]
america_west, [mesa, alaska, twa]
The issue is calling the recursion in which, given two Strings of Airlines, find if there is a connection. My recursion below is checking whether there is an initial connection between the Airlines, and it not, recursively go inwards. However the error seems to be a Stack Overflow Error. I do not understand why it would overflow
if the test case gives Airlines {"delta" "america_west"},{"continental" "mesa"},{"southwest" "delta"},{"twa" "air_france"}.
public static boolean recur(String a, String b) {
boolean connection = false;
if(partners.containsKey(a)&&partners.get(a).contains(b)) {return true;}
for(String airlines: (ArrayList<String>)partners.get(a)) {
if(partners.containsKey(airlines))
recur(airlines,b);
}
return connection;
}
Edit:
I'm sorry I didn't include all of the code, it's my first time uploading, and I felt that it was a bit long.
public class airlinePartners{
static Map<String, ArrayList> partners = new HashMap<String, ArrayList>();
static String returnT = "PARTNERS\n";
static String returnF = "No miles for you\n";
int number = 0;
public static void readIn() throws Exception{
Scanner sc = new Scanner(new File("partners.dat"));
String[] temp = new String[2];
int airline = sc.nextInt();
for(int i = 0; i < airline; i++) {
sc.next();
}
int partner = sc.nextInt();
sc.nextLine();
for(int i = 0; i < partner; i++) {
temp = sc.nextLine().split(" ");
ArrayList<String> temps = new ArrayList<String>();
if(partners.containsKey(temp[0])) {
partners.get(temp[0]).add(temp[1]);
}
else {
temps.add(temp[1]);
partners.put(temp[0], temps);
}
}
Iterator<Map.Entry<String, ArrayList>> i = partners.entrySet().iterator();
while(i.hasNext()){String key = i.next().getKey();
System.out.println(key+", "+partners.get(key));
}
int repeat = sc.nextInt();
sc.nextLine();
for(int m = 0; m < repeat;m++) {
temp= sc.nextLine().split(" ");
//System.out.println(temp[0] + " "+ temp[1]);
if(recur(temp[0], temp[1])) {
System.out.println(returnT);
}
else {
System.out.println(returnF);
}
}
}
public static boolean recur(String a, String b) {
boolean connection = false;
if(partners.containsKey(a)&&partners.get(a).contains(b)) {return true;}
for(String airlines: (ArrayList<String>)partners.get(a)) {
if(partners.containsKey(airlines))
recur(airlines,b);
}
return connection;
}
public static void main(String args[]) throws Exception
{
new airlinePartners().readIn();
}
}
I read Bert Bates and Katie Sierra's book Java and have a problem.
The Task: to make the game "Battleship" with 3 classes via using ArrayList.
Error: the method setLocationCells(ArrayList < String >) in the type
SimpleDotCom is not applicable for the arguments (int[])
I understand that ArrayList only will hold objects and never primatives. So handing over the list of locations (which are int's) to the ArrayList won't work because they are primatives. But how can I fix it?
Code:
public class SimpleDotComTestDrive {
public static void main(String[] args) {
int numOfGuesses = 0;
GameHelper helper = new GameHelper();
SimpleDotCom theDotCom = new SimpleDotCom();
int randomNum = (int) (Math.random() * 5);
int[] locations = {randomNum, randomNum+1, randomNum+2};
theDotCom.setLocationCells(locations);
boolean isAlive = true;
while(isAlive) {
String guess = helper.getUserInput("Enter the number");
String result = theDotCom.checkYourself(guess);
numOfGuesses++;
if (result.equals("Kill")) {
isAlive = false;
System.out.println("You took " + numOfGuesses + " guesses");
}
}
}
}
public class SimpleDotCom {
private ArrayList<String> locationCells;
public void setLocationCells(ArrayList<String> loc) {
locationCells = loc;
}
public String checkYourself(String stringGuess) {
String result = "Miss";
int index = locationCells.indexOf(stringGuess);
if (index >= 0) {
locationCells.remove(index);
if(locationCells.isEmpty()) {
result = "Kill";
} else {
result = "Hit";
}
}
return result;
}
}
public class GameHelper {
public String getUserInput(String prompt) {
String inputLine = null;
System.out.print(prompt + " ");
try {
BufferedReader is = new BufferedReader(new InputStreamReader(System.in));
inputLine = is.readLine();
if (inputLine.length() == 0)
return null;
} catch (IOException e) {
System.out.println("IOException:" + e);
}
return inputLine;
}
}
convert ArrayList to int[] in Java
Reason for Basic Solution
Here's a simple example of converting ArrayList<String> to int[] in Java. I think it's better to give you an example not specific to your question, so you can observe the concept and learn.
Step by Step
If we have an ArrayList<String> defined below
List<String> numbersInAList = Arrays.asList("1", "2", "-3");
Then the easiest solution for a beginner would be to loop through each list item and add to a new array. This is because the elements of the list are type String, but you need type int.
We start by creating a new array of the same size as the List
int[] numbers = new int[numbersInAList.size()];
We then iterate through the list
for (int ndx = 0; ndx < numbersInAList.size(); ndx++) {
Then inside the loop we start by casting the String to int
int num = Integer.parseInt(numbersInAList.get(ndx));
But there's a problem. We don't always know the String will contain a numeric value. Integer.parseInt throws an exception for this reason, so we need to handle this case. For our example we'll just print a message and skip the value.
try {
int num = Integer.parseInt(numbersInAList.get(ndx));
} catch (NumberFormatException formatException) {
System.out.println("Oops, that's not a number");
}
We want this new num to be placed in an array, so we'll place it inside the array we defined
numbers[ndx] = num;
or combine the last two steps
numbers[ndx] = Integer.parseInt(numbersInAList.get(ndx));
Final Result
If we combine all of the code from "Step by Step", we get the following
List<String> numbersInAList = Arrays.asList("1", "2", "-3");
int[] numbers = new int[numbersInAList.size()];
for (int ndx = 0; ndx < numbersInAList.size(); ndx++) {
try {
numbers[ndx] = Integer.parseInt(numbersInAList.get(ndx));
} catch (NumberFormatException formatException) {
System.out.println("Oops, that's not a number");
}
}
Important Considerations
Note there are more elegant solutions, such as using Java 8 streams. Also, it's typically discouraged to store ints as Strings, but it can happen, such as reading input.
I can't see where you call setLocationCells(ArrayList<String>) in your code, but if the only problem is storing integers into an ArrayList there is a solution:
ArrayList<Integer> myArray = new ArrayList<Integer>();
myArray.add(1);
myArray.add(2);
It is true that you can't use primitive types as generics, but you can use the Java wrapper types (in this case, java.lang.Integer).
I am writing a java program to compare two numbers using nesting methods but receiving the error`
class HelloWorld {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter First Number");
int X = s.nextInt();
System.out.println("Enter Second Number");
int y = s.nextInt();
Nesting nest = new Nesting(int, int);
nest.disp();
}
}
class Nesting {
int m, n;
Nesting(int X, int y) {
m = X;
n = y;
}
int largest() {
if (m > n) {
return m;
} else {
return n;
}
}
void disp() {
int ans = largest();
System.out.println("My Result is " + ans);
}
}
While compiling receiving the following error
Line: 11
'.class' expected
Line: 11
'.class' expected
When you call a method or constructor, you should not pass the type, instead you have to pass the values, you have to change :
Nesting nest = new Nesting(int, int);
To this :
Nesting nest = new Nesting(X, y);
It seems that when I create my scanner I get this error. I have tried to solve this by searching the error name, but have so far been unsuccessful in getting the message to stop appearing.
Code:
import java.util.Scanner;
public class PrintQueue {
//Instance variables
private Queue<Job> pq;
//Constructor
public PrintQueue() {
pq = new Queue<Job>();
}
//Adds a job object to the end of the queue
public void lpr(String owner, int jobId) {
Job j = new Job(owner, jobId);
pq.enqueue(j);
}
//Enumerates the queue
public void lpq() {
Job curr = pq.first();
for (int i = 0; i < pq.size(); i++) {
System.out.println(curr);
curr = pq.next();
}
}
//Removes the first entry in the queue if the input integer matches the integer contained within the job object
public void lprm(int jobId) {
if (pq.first().getJobId() == (jobId))
pq.dequeue();
else
System.out.println("Unable to find jobId.");
}
//Removes all objects that contain the input String
public void lprmAll(String owner) {
Job curr = pq.first();
for (int i = 0; i < pq.size(); i++) {
if (curr.getOwner().equals(owner))
pq.dequeue();
curr = pq.next();
}
}
//Demo
public static void main(String[] args) {
Scanner k = new Scanner(System.in);
PrintQueue myPQ = new PrintQueue();
String name;
int id;
for (int i = 1; i <= 5; i++) {
System.out.print("Enter owner and id: ");
name = k.next();
id = k.nextInt();
myPQ.lpr(name, id);
}
System.out.println("Print Queue");
myPQ.lpq();
myPQ.lprm(101);
myPQ.lprmAll("ronaldinho");
System.out.println("Print Queue");
System.out.println("\n\n");
myPQ.lpq();
}
}
Part where I get the error:
Scanner k = new Scanner(System.in);
That's because you're never closing the Scanner. Change your code to:
Scanner k = null;
try {
k = new Scanner(System.in);
//do stuff with k here...
} finally {
if( k != null )
k.close();
}
It seems that it is rather warning than error. However it is good practice to solve it.
Actually you just have to call k.close(); in the end of your method.
The best practice is to call close in finally block: this guarantees that the resource is closed whenever exception is thrown or not;
Scanner k = null;
try {
k = new Scanner(System.in);
........
} finally {
if (k != null) {
k.close();
}
}
Fortunately java 7 provides makes this syntax less verbose:
try (
Scanner k = new Scanner(System.in);
) {
.... // use k
}
When object of any class that implements Closable is created in special section of try block that marked with regular brackets () you do not have to write finally block: it is added by compiler.
I'm a computer science student that just started my sophomore programming class and I'm having some real issues with a project that deals with Stacks and collections.
Basically, this is a project that relies on the ArrayStack Class(ArrayStack, to be specific) to convert mathematical expressions between postfix and infix forms.
Basically, ArrayStack is used to take in an expression like 45 * (26 - 5) / 54, turn it into a collection, then rewrite in a postfix form like 45 26 5 - * 54 /
The problem is, first of all, whenever I try to substitute with Character in the main method(since the stack needs to store both operators and operands, maybe there's a better data type I'm missing here), I get some strange syntax error, usually involving the program thinking that ArrayStack.System is somehow a statement(System.out.println is right below an ArrayStack statement, which suggests there's some kind of syntax problem)
Here's the code I have so far:
public class ArrayStack<T> implements StackADT<T>
{
private static final int DEFAULT_CAPACITY = 100;
private int top;
private T[] stack;
public ArrayStack() {
top = -1;
stack = (T[]) (new Object[DEFAULT_CAPACITY]);
}
public void push(T element) {
stack[top+1] = element;
top++;
}
public T pop() {
T element = stack[top];
stack[top] = null;
top--;
return element;
}
public T peek() {
return stack[top];
}
public boolean isEmpty() {
if(stack[0]==null)
return true;
else{
return false;
}
}
public int size() {
int length = 0;
for(int count=0;count<stack.length;count++) {
if(stack[count]!=null) {
length++;
}
else if(stack[count]==null) {
break;
}
}
return length;
}
public String toString() {
String array = "";
for(int count=0;count<stack.length;count++) {
array = array+stack[count]+" ";
}
return array;
}
}
And for the main method:
public class StackTester {
public static void main(String[] args) {
boolean quit = false;
int input;
String expression;
do {
System.out.println("1. Convert infix to postfix");
System.out.println("2. Convert postfix to infix");
System.out.println("3. Exit.");
java.util.Scanner keyboard = new java.util.Scanner(System.in);
input = keyboard.nextInt();
switch(input) {
case 1:
//ArrayStack stack = new ArrayStack();
//System.out.println("Enter an infix expression: ");
expression = keyboard.next();
for(int count=0;count<expression.length();count++) {
Character a = expression.charAt(count);
stack.push(a);
}
for(int count=stack.size()-1;count>=0;count--) {
if(stack.peek()=='+') {
}
}
}
break;
}
while(!quit);
}
}
The error usually occurs at the lines marked with //, every time I try to insert something like or otherwise, the program gives some weird syntax error like its trying to read it together with the line below it. Any ideas what's going on here?