Sorting With Two Stacks

Oct 16, 2014

So we have to ask the user to put in a string of letters, and bring those letters in as cars to where there is a storage area and an assembly area, and we have to sort them from there into the assembly area with the smallest (A) at the head. I think I set up my code pretty well, but when I run it, no matter what I put in it returns CBAo. Say I input KATE, it should return TKEA but instead CBAo or if I input JANICE it should return NJIECA but it just returns EDCBAo. Here's my code:

import java.util.Scanner;
import java.util.Stack;
public class carStacksDessart
{
public static void main(String[] args) {
Stack<Integer> storage = new Stack();

[Code] ....

View Replies


ADVERTISEMENT

Write A Code Using The Stacks?

May 15, 2014

write a code using the stacks?i want the output to produce my name.

View Replies View Related

Printing 8 Queens Using Stacks

Nov 16, 2014

I am having some trouble with this program. I am getting only one result to print when it should show all the solutions. Also the 1 solution I am getting is only printing 7 queens not 8.

import java.util.Stack;
public class Queen1 {
boolean conflict, complete = false;
public int solve(int n) {
//create a stack
//each element stores the position of the queen on a different row
Stack<Integer> s = new Stack<Integer> ();

[Code] ......

View Replies View Related

How To Make Calculator Using Stacks

Feb 5, 2014

I got to make a java code for my class. I must make a calculator using stacks. I have a code but i got some mistakes. Here is the code

Java Code:

package stackscalc;
import java.util.Scanner;
import java.util.Stack;
import java.util.EmptyStackException;
class Arithmetic {
int length;
Stack stk;
String exp,

[Code] .....

View Replies View Related

Make A Calculator Using Stacks

Feb 4, 2014

i got a problem with this program.I am supposed to make a calculator using stacks but it seems that i m stack. i can t get the right recognition

for () and {} []

its not necessary to use all these symbols.just a simple () will do .here s the program

package stackscalc;
 
import java.util.Scanner;
import java.util.Stack;
import java.util.EmptyStackException;
class Arithmetic
{
int length;
Stack stk;
String exp,
postfix;
 
[code]....

View Replies View Related

Make Array With Three Stacks

May 17, 2014

public class ThreeStackArray
{
static int row = 10;
static int col = 2;
int[][] stack = new int[row][col];
//place numbers into the first row of array
public void push(int num)

[Code] .....

I found this program idea online. Make a array with three stacks. Either I jump out of bounds if I use "continue" instead of "break" in my loop in the push method. If I use break the output looks like:

12900
0000
0000
0000
0000

if I use continue the output looks like:

An error has occured java.lang.ArrayIndexOutOfBoundsException: 2
1212
1212
1212
1212
1210

View Replies View Related

Exception In Matching Parentheses Using Stacks

May 11, 2014

I am trying to write a program that checks for parentheses matching using stacks.This is my ArrayStack class.

public class ArrayStack{
private final int DEFAULT_SIZE=10;
public int tos;
Object[] array;
public ArrayStack(){
array=new Object[DEFAULT_SIZE];

[Code] ....

But the problem is when I compile matching I get an error as unreported exception EmptyStackException.must be caught or declared to be thrown. I think the problem is with exceptions which I don't have a good knowledge of.

View Replies View Related

Check If A String Is A Palindrome By Using Stacks

Oct 23, 2014

I'm supposed to use stacks (implemented with an array) to check to see if a string is a palindrome. I've finished all my classes and methods, but I'm getting an ArrayIndexOutOfBoundsException when I try to run my demo program.Here are my classes:

public interface Stack {
// Creates an empty stack
public void initializeStack()
// Returns true if the stack is empty, returns false otherwise
public boolean isEmpty();
// The stack can never be full, so always return false
public boolean isFullStack();

[code]...

View Replies View Related

Stacks And Priority Queues Project

Feb 21, 2014

So I have 3 classes in this project, and When it compiles, I get a null pointer.

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class Project2 {
 
[Code] .....

View Replies View Related

Error On Import Statement For Stacks

Apr 20, 2015

I'm getting errors on all the exceptions called EmptyCollectionException. I think this is because the import statement has a error on it but I'm not sure. I'm suppose to add methods for peek, isEmpty, size, and toString methods. I only started isEmpty also am wondering what I have to change from peek method if anything at all.

import jsjf.exceptions.*;//Error on jsjf
import java.util.Arrays;
public class ArrayStack<T> implements StackADT <T> { //Error on StackADT
private final static int DEFAULT_CAPACITY = 100;
private int top;
private T[] stack;
 
[Code] .....

View Replies View Related

How To Perform Calculations Using Stacks Java

Nov 23, 2014

I am doing a calculator using stacks but when i try to calculate I getting the wrong data example stack contains 8 and user enter -3 stack should change to 5.

package comp10152.lab5;
import java.util.Scanner;
import java.util.Stack;

[Code].....

View Replies View Related

Evaluating Infix Expressions Using Generic Stacks

May 12, 2014

I am given the task to create a program that evaluates infix expressions using two generic stacks, one operator stack and one value stack.

This is my GenStack.java file:

import java.util.*;
public class GenStack<T>{//T is the type parameter
private Node top;//top of stack
public class Node {//defines each node of stack
T value;
Node next;

[Code] ....

I'm having trouble with the eval and apply methods. The eval method doesn't appear to pickup ')' characters, like it doesn't even see them.

View Replies View Related

Changing Infix To Postfix Notation Using Stacks - How To Utilize Hash Maps

Apr 7, 2014

So I am supposed to be changing infix notation to postfix notation using stacks. This is simply taking a string "3 + 5 * 6" (infix) and turning it into (3 5 6 * +" (postfix).

To do this, we are to scan the string from left to right and when we encounter a number, we just add it to the final string, but when we encounter an operand, we throw it on the stack. Then if the next operand has a higher input precedence than the stack precedence of the operator on the top of the stack, we add that operator to the stack too, otherwise we pop from the stack THEN add the new operator.

I am supposed to be utilizing a hash map but I don't see how you would go about doing this. We are supposed to store operators on the hash map but operators need their own character, input precedence, stack precedence, and rank. How do you use a hash map when you need to tie a character to 3 values instead of just 1? I just don't get it.

The following is our Operator class that we are to use. Another problem is this isn't really supposed to be modified, yet we were given two important variables (inputPrecedence and outputPrecedence) that we can't have nothing to be initialized to and no way of accessing? So that might be where a hash map comes in but I am not sure. I am not very sure on how they exactly work anyway...

public class Operator implements Comparable<Operator>
{
public char operator; // operator
privateint inputPrecedence; // input precedence of operator in the range [0, 5]
privateint stackPrecedence; // stack precedence of operator in the range [-1, 3]

[Code] ....

So my question mostly revolves around how I tie an Operator character to its required values, so I can use it in my code to test two operators precedence values.

My original thought was turn string into character array, but then I would need nested for/while loops to check if it is a number or letter, or if it is an operator and thus result in O(n^2) time

View Replies View Related

Sorting With A Comparator

Mar 26, 2014

I am trying to sort an ArrayList of objects with the comparator as I want to sort based on a certain value for each object. I understand I would need to override compareTo() in the objects class, is there any way I can get around also needing to override for all subclasses of the object?

View Replies View Related

Sorting A Map Alphabetically

Nov 30, 2014

I'm trying to create a method that takes a map and sorts it alphabetically. I think I have the logic correct but I'm getting errors when I run it:

private static void sortMapAlphabetically(
Map<String, String> termsAndDefinitions) {
Map<String, String> tempMap = new Map2<String, String>();

[Code].....

View Replies View Related

Sorting Out ArrayList

Nov 26, 2014

I have to sort out my ArrayList but cannot make it work. Here is

Java Code:

Collections.sort(this.users);
User poor = this.users.get(0);
User ritch = this.users.get((this.users.size()-1));
System.out.println("Poorest user has" + poor.getUsername() + poor.getBalance() );
System.out.println("Ritches user has " + ritch.getUsername() + poor.getBalance() + "
"); mh_sh_highlight_all('java');

The Collections.sort(this.users); does not work.

View Replies View Related

JSP :: Sorting Query In JSTL?

Jun 21, 2006

i am doing a code using JSTL to fire a query. everything come fine except the resule is not sorted as desired. i am putting the code below--

String sort_order=(String)request.getAttribute("sort_order");
request.setAttribute("sort_order",sort_order);
<sql:query var="viewQueryj" sql= "select USER_ID, PERMISSION_ID, USER_NAME from administrator order by ?">
<sql:param value="${sort_order}"/>
</sql:query>

now the resule is always sorted by USER_ID. if i want to sort it using USER_NAME i pass parameter from controller to this page in sort_oredr variable which comes fine but the result doesn't sort by name, only by id. if i hardcode USER_NAME in query then the result is as desired.

View Replies View Related

Sorting 2D Java Array

Sep 16, 2014

I need to sort a 2D array to look like this : [URL] ....

Here is the code that I have.

File 1:
import java.text.DecimalFormat;
public class Assignment1 {
public static void main(String[] args) {
String[][] rainArray
= {
{"January", "3.03"},

[Code] ....

View Replies View Related

Array Sorting Using Two Different Methods

Nov 18, 2014

The following code is supposed to generate random integers and sort array1 and array2 using two different sorting methods. array1 is to be sorted with a "selection sort" method and array2 is to be sorted with the built-in Arrays.sort() method. However, array1 is the one that has a problem. It does not appear to output any values at all for array size of 4000 or more, such as array1[10000]. The assignment is to generate random integers, sort and benchmark the speeds at which array1 and array2 can generate and sort ints at array1[1000] array2[1000] array1[10000] array2[10000] array1[100000] array2[100000]

/**
*
* The following is a sorting and benchmarking program to sort
* array1 and array2 with 1,000 , 10,000 and 100,000 array sizes.
* array1 uses selection sort from section 7.4 of the book
* and array2 uses the built in Arrays.sort() method.

[code]....

I cannot post the output because the amount of data seems to have crashed the two previous posts I made on this topic due to the size of the problem.

View Replies View Related

Sorting A List Error?

Jun 12, 2014

I am reading a file and sorting a list, and I cannot figure out why I am getting an error on line 15 that contains the following code

Collections.sort(sortedContributorList, new Contributor());

This is the error I keep getting:

The method sort(List<T>, Comparator<? super T>) in the type Collections is not applicable for the arguments (LinkedList<Contributor>, Contributor)

import java.util.*;
import java.io.*;
public class myhashTable {
  public static LinkedList<Contributor> sortedContributorList = new LinkedList<Contributor>();
 public myhashTable(){

[code]....

View Replies View Related

Searching And Sorting Algorithm

Jan 2, 2015

I have a code that is meant to read a file and organize all the names from least to greatest salaries. It also allows the user to enter a name to find from the file, and the program finds the name and displays it. I have two errors, and I will show the error in my code

View Replies View Related

Sorting From Lowest To Highest

Feb 23, 2014

I'm very new to Java and ran into a problem. My results are not in order and I'm not sure what I'm doing wrong.

My results come out like this instead of being in order from lowest to highest: "77 99 44 55 22 88 11 0 66 33"

Here's what I have:

class ArrayIns {
private long[] a; // ref to array a
private int nElems; // number of data items
//--------------------------------------------------------------
public ArrayIns(int max) // constructor
{
a = new long[max]; // create the array
nElems = 0; // no items yet

[Code] .....

View Replies View Related

Sorting Arraylist By Quantity

Apr 22, 2015

I'm getting this error:

The operator < is undefined for the argument type(s) java.util.ArrayList<FacebookUser>, java.util.ArrayList<FacebookUser>

For this:

class FriendsComparator implements Comparator<FacebookUser>
{
@Override
public int compare(FacebookUser o1, FacebookUser o2)
{
int returnValue = 0;
if (o1.friends < o2.friends)
returnValue = -1;
[Code] ....

Same as for the second if condition. How can I fix this exactly? What I'm doing is trying to sort Facebook users by the most to least amount of friends.

Here's the Driver:

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Scanner;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;

[Code] .....

View Replies View Related

Linked List Sorting

Apr 20, 2014

I have made a node class and im trying to implement a sorting method. I must use a selection sort but with specific instructions: "Your method should not need to use the new operator since it is just moving nodes from one list to another( not creating new nodes)

this is my current implementation ..but i am instantiating new object..

public class NodeInt
{
private int data;
private NodeInt next = null;
public NodeInt(){}
//precondition:
//postcondition:
public NodeInt(int data, NodeInt next)
{
this.data = data;
this.next = next;

[code]....

edit: this is the part that worked but i had it commented out so i have the previous and current declared above but didnt copy.

View Replies View Related

Sorting 10 Double Arrays

Sep 17, 2014

I'm trying to sort 10 inputted numbers (double precision) using the Array.sort() method. I can get the 10 numbers inputted, but the output is ten 0.0s; now (and this how I know I am learning some things) I'm fairly certain that the variable number is not storing the numbers inputted by the user otherwise I wild be seeing the program work correctly.

So my question is why isn't number storing the inputs?

import javax.swing.JOptionPane;
import java.util.Arrays;
public class KrisFrench3 {
public static void main(String[] args) {
double[] number = new double[10];
for(int i = 1; i <= i; i++) {

[Code] .....

View Replies View Related

Stuck Sorting Arrays

Jul 1, 2014

Write a method called isSorted that accepts an array of real numbers as a parameter and returns true if the list is in sorted (nondecreasing) order and false otherwise. For example, if arrays named list1 and list2 store {16.1, 12.3,22.2, 14.4} and {1.5, 4.3, 7.0, 19.5, 25.1, 46.2} respectively, the calls is Sorted(list1) and isSorted(list2)should return false and true respectively. Assume the array has at least one element. A one-element array is considered
to be sorted. public class thirfd {

public static void main(String[] args) {
double[] arr1 = {16.1, 12.3,22.2, 14.4};
double[] arr2 = {1.5, 0.3, 7.0, 19.5, 25.1, 46.2};
isSorted(arr2);
System.out.println(isSorted(arr2));

[code]...

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved