Stack - For Loop Not Returning Proper Result

Jul 18, 2014

Below is a method that is suppose to insert a value inside a stack based on the index. I thought it would be easier to copy the value, if any, that was in the index into the variable holder and replace it with the input value. After in which it would copy the holder value and place it at the top of the stack. (topOfStack variable is what the holder is copying too.)

public void pushExact (int index, String input) {
String holder = "";
if (maxSize == 0) {
theStack[topOfStack] = input;
topOfStack++;
} else if (topOfStack + 1 < maxSize) {
for (int n= maxSize - 1;n >= 0;n--) {

[Code] ....

View Replies


ADVERTISEMENT

Output Is Not Giving Proper Result - Semantic Error?

Oct 5, 2014

why the output is not giving the proper result? Is this a semantic error?

public class Programming1Practice {
/* testing out instantiation and object oriented programming */
public static class staticValues {

[Code]....

View Replies View Related

Finding Longest Increasing Sequence Not Giving Proper Result

Jan 28, 2015

Okay so I need to be able to read a file and then be able to find the longest sequence of increasing numbers.

So if the file was this, (this is Ass1Q2_test4.txt)

97 47 56 36 60 31 57 54 12 55
35 57 41 13 82 80 71 93 31 62
89 36 98 75 91 46 95 53 37 99
25 45 26 17 15 82 80 73 96 17
75 22 63 96 96 36 64 31 99 86
12 80 42 74 54 14 93 17 14 55
14 15 20 71 34 50 22 60 32 41
90 69 44 52 54 73 20 12 55 52
39 33 25 31 76 45 44 84 90 52
94 35 55 24 41 63 87 93 79 24

the output should be,

(5,0) with cost 12
(6,0) with cost 14
(6,1) with cost 15
(6,2) with cost 20
(7,2) with cost 44
(7,3) with cost 52
(7,4) with cost 54
(6,3) with cost 71
(5,3) with cost 74
(4,3) with cost 96

Greatest path is of length 10.

Now, the code that I have works, kind of. Instead of recurring several times at each point, it only recurs once.

So say I'm looking at (1,1). With (1,1) being 57. The area around it looks like this.

97 47 56
35 57 41
89 36 98

Now when I look at it, there are several paths it can take. It can go 57, 97 or 57, 89 or 57, 98. However, I'm pretty sure that it just uses the first one that corresponds with the first if statement that is valid. So I start checking north of the value, then northeast, then east, then southeast, which at southeast is where I find my first greater than value. After it finds it's first valid number, it then continues from that number, instead of checking if there are other longer paths stemming from the original value.

In conjunction with that, you can see that the printout just returns all paths from each value. Which isn't what I want. I need a way to store the longest current path, then check each path after to see if it's longer. If it is, it's replaced, if not, it stays the same.

I've also attached Ass1Q2_test4.txt

import java.util.*;
import java.io.*;
public class MaxIncreasingSub {

[Code].....

View Replies View Related

While Loop Inside A For Loop To Determine Proper Length Of Variable

Feb 25, 2014

Here's the code: it's while loop inside a for loop to determine the proper length of a variable:

for (int i = 0; i < num; i++) {
horse[i]=new thoroughbred();
boolean propernamelength = false;
while (propernamelength==false){
String name = entry.getUserInput("Enter the name of horse "

[code]....

I was just wondering what was going on here -- I've initialized the variable, so why do I get this message? (actually the carat was under the variable name inside the parentheses.

View Replies View Related

Returning A Result And Then Using It In Main

Jan 4, 2015

I've made a class called Car with a method which will tell me a category for the engine size regarding the actual size (which I've included in the main just so I could see if it works) but everytime I test it I get an error.

public class Car {
public String b;
public String c;
public double es;
public double cs; 
public String getCategory() {
if (es < 1.3)

[Code] ....

Figured it out. Was missing parenthesis on audiCar.getCategory();

View Replies View Related

Adding Characters To Result Stack

Feb 21, 2014

Right now I have three stacks. Each stack holds characters, with the normal operations of pushing and popping. I am trying to compare two characters from stack1 and stack2(each character is a digit character). I would like to be able to do arithmetic on the digit characters and the push the answer on to a result stack.

Ex.) I pop character '3' from stack1 and '5' from stack2. Then I add the two. equaling '8' and pushing that character on to the result stack. This will hopefully in turn allow me to add arbitrary large numbers that integers will not support.

I am having trouble because I believe I am getting some ascii character values when I pop off the result stack. here is a small piece of my code

public void addition() {
char temp1 ,temp2;
int i = s1.getSize();
for(int j= 0;j<i;j++) {
temp1 = s1.pop();
temp2 = s2.pop();
if(temp1+temp2>=10)

[Code] .....

View Replies View Related

Basic Stack Hanging - Program Kept Running With No Result

Nov 16, 2014

I was doing some exercises of a java tutorial website, and realized when I ran the code that they had , my program just kept running with no result, why is this? It just hangs.

package mystack;
public class MyStack {
private int maxSize;
private int[] stackArray;
private int top;
public MyStack(int s)

[Code] ....

View Replies View Related

Returning Total From Loop

Jan 7, 2014

I have a for each loop that outputs the Mechanics salary for the first team in the arraylist, how can I add these figures together instead of having separate value for each?The code in my testing class to get the salary for mechanics:

Java Code: for (Mechanic str1: formula1.get(0)){
System.out.println(str1.getSalary());
} mh_sh_highlight_all('java');

How can I also get the salary for the driver 1 + 2 in the same team and add their salary to this? I have attaached an image showing the classes/fields created

View Replies View Related

Loop Not Popping Off All Item From Stack

Sep 30, 2014

When I try popping everything off my stack, it leaves the last item alone. The size comes out correct when I print it out, so that cant be the issue, I think. Also, it doesnt even print it out if I do

for(int i = 0; i <= s1.Size() + 1; i++)

it still leaves one value left. What am I doing wrong?

public class Test {
public static void main(String[] args) {
Stack<String> s1 = new Stack<String>();

s1.Push("first");
s1.Push("2nd");
s1.Push("3rd");
s1.Push("4th");
System.out.println(s1);
try {
System.out.println("The top item is: " + s1.Peek());

[code]....

View Replies View Related

Loop That Stops Printing A File And Adding To Stack

May 6, 2015

I'm supposed to add characters to a stack and pop them once the adjacent delimiter is read in from a text file. Moreover, program is supposed to print out the incoming text from the file, but stop when the applicable delimiter is not on top of the stack. As in, a '[' doesn't have a ']'.

I've got the program so it can pop and add to the stack correctly, and stops adding at the correct time, but I cant seem to get it to stop printing. I know a switch statement method in another class seems obvious, but I was trying to practice nested loops.

This is the main program:

import java.io.*;
import java.util.Scanner;
import java.io.IOException;
import java.util.Stack;
public class DelimiterChecker {
public static void main(String [] args) throws IOException {

[Code] ......

Moreover, the file I'm reading for is simply:

{
System.out.println(1)
System.out.println(2)
System.out.println(6 <-------------missing delimiter
System.out.println(7)
}

View Replies View Related

While Loop - How Result Being Replace If It Only Equals To 1

Jul 6, 2014

I was reading a book and came across this while loop.

public class Powers {
public static void main (String [] args){
int e;
int result;
for(int i = 0; i < 10; i++){

[Code] .....

This loop presents the following (I'm sure it's not necessary):

2 to the 0 power is 1
2 to the 1 power is 2
2 to the 2 power is 4
2 to the 3 power is 8
2 to the 4 power is 16
2 to the 5 power is 32
2 to the 6 power is 64
2 to the 7 power is 128
2 to the 8 power is 256
2 to the 9 power is 512

I am just having a difficult time understand and grasping this concept. My main issue is result *=2; this is making it very difficult to understand. How is result being replace if it only equals to 1.

View Replies View Related

For Loop To Count Values Of Stack Of Quarters From 1.00 To 3.00 And Print Value Of Each Iteration

Nov 2, 2014

I had to write a foor loop to count the values of a stack of quarters from 1.00 to 3.00 and I had to print the values, that I understood and got it working so I taught the next assignment was going to be easier but I am having a hard time with it. For this one I have to write a for loop to print all the positive integer factors of 144 and I am supposed to print of factor per line but I tried doing that but it doesn't work it just prints out 144.

This is my code. The quarter assignment is also in there because it is part of a lab so just ignore that part since it is working correctly.

public class ForLoopPractice
{
public static void main(String [] args)
{
// Write a for loop to count out the values of a stack of quarters from $1.00 to $3.00
// Print the value of each iteration. Print this all on one line, rounded to the nearest cent.
// To print rounded, use printf, with a placeholder of %.2f
// (%f is the floating-point placeholder; the .2 indicates the number of decimal places)
/* YOUR CODE HERE */
for (double q = 1.00; q <= 3.00; q += .25)

[Code] ....

View Replies View Related

Not Getting A Proper Shape Of Leftarrow

Apr 22, 2014

why am I not getting a proper shape of "Leftarrow"? The result should look like the following (the system draws the arrow abnormally, so the beginning should a real ARROW:

*

* *

* *

* * * * * * * * *

* *

* *

*

here is the class:

Java Code: public class LeftArrow extends ShapeBase {
private int lengthOfTail;
private int widthOfArrowHead;

[Code]....

View Replies View Related

Could Not Find Proper Position In Print

Aug 3, 2014

So I need to print out the table of conversions from kilogram to pound and from pounds to kilograms. I think I have done a while loop correctly, but it is hard to actually check it since I do not have proper output format. I have tried also %4.2f format option however could not find the proper position in the print.

public static void main (String[] args){
System.out.printf("%10s%10s | %10s%10s
", "Kilograms", "Pounds",
"Kilograms", "Pounds");
System.out.println("---------------------------------------------");

[code]....

View Replies View Related

Proper Way To Organize Code Into Blocks

Feb 16, 2015

When I run this (entering 12 for both questions) I get this error:

java.lang.ArrayIndexOutOfBoundsException: 12
at MathTablesTwo.main(MathTablesTwo.java:22)

Also, what would be the proper way to organize my code into blocks?

import java.util.Scanner;
public class MathTablesTwo {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);

[code]....

View Replies View Related

JRE :: How To Create Proper Deployment Rule Set

Dec 2, 2014

I have a signed certificate from Entrust which I used to sign a DeploymentRuleSet.xml file.  I placed the DeploymentRuleSet.jar in the proper location C:WindowsSunJavaDeployment, afterward the java control panel's security tab shows a link to "show the active deployment rule set" which did not exist prior to coping the file to the directory.  When I click on the link a new window opens and says "Rule Set not found" ....

View Replies View Related

Physical Print Out For Bill In Proper Format

Sep 23, 2014

I am trying to take physical print out for bill, But i am receiving some problems:

Java Code:

import java.awt.Color;
import java.awt.print.PrinterException;
import java.util.ArrayList;
import javax.print.attribute.AttributeSet;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
public class Printme {

[Code] .....

But My Output is

What i have to do / learn to get the proper bill format ....

View Replies View Related

I/O Streams :: Insert Space At Proper Place By Breaking Sentence Into Correct Words

Nov 27, 2014

You are given 2 files - "testcase.txt" and "validwords.txt".In testcase.txt, the sentences are written without any space inbetween/for ex: this is a sentence which looks ambigous.You are to insert a space at the proper place by breaking the sentence into correct words, ex o/p: this is a sentence which looks ambigous.validwords.txt contains all valid words (as 1 word per line). If you find a word that does not match, replace it with ####. After correcting

View Replies View Related

Unable To Print A Loop Inside Array Of Greater Size Than Loop Itself

Jan 27, 2015

I am trying to print a loop inside an array of a greater size than the loop itself. Like for example I have an array of size 7 but it has only 3 elements.

int[] array=new int[7]
array[0]=2;
array[1]=3;
array[2]=4;

now what I want to do is print these three numbers in a loop so that my array[3]=2;array[4]=3;array[5]=4 ...... till the last one. Also the array could be any size and not just 7.

View Replies View Related

When Type Quit To Close Outer Loop / It Still Runs Inner Loop

Nov 2, 2014

I have everything else working. My problem is that when i type "quit" to close the outer loop. It still runs the inner loop. The National Bank manager wants you to code a program that reads each clients charges to their Credit Card account and outputs the average charge per client and the overall average for the total number of clients in the Bank.

Hint: The OUTER LOOP in your program should allow the user the name of the client and end the program when the name entered is QUIT.In addition to the outer loop, you need AN INNER LOOP that will allow the user to input the clients charge to his/her account by entering one charge at a time and end inputting the charges whenever she/he enters -1 for a value. This INNER LOOP will performed the Add for all the charges entered for a client and count the number of charges entered.

After INNER LOOP ends, the program calculates an average for this student. The average is calculated by dividing the Total into the number of charges entered. The program prints the average charge for that client, adds the Total to a GrandTotal, assigns zero to the Total and counter variables before it loops back to get the grade for another client.Use DecimalFormat or NumberFormat to format the numeric output to dollar amounts.

The output of the program should something like this:

John Smith average $850.00
Maria Gonzalez average $90.67
Terry Lucas average $959.00
Mickey Mouse course average $6,050.89
National Bank client average $1,987.67

Code:

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String name = "";
int charge = 0;
int count = -1;
int total = 1;
int grandtotal = 0;
int average = 0;
 
[code]....

View Replies View Related

Why Method Is Not Returning A Value

Apr 29, 2014

I'm new to Java and I have a problem with a method, I can't see the code of the method, I just have a jar, but it should return a boolean, something like this:

boolean band = false;
band = TestClass.testMethod("blabla");
// band = false

The problem is that the method seems that is returning nothing (band remain false), and if I initialize band to true:

boolean band = false;
band = TestClass.testMethod("blabla");
// band = true

band remain true, in other words, the value of band is never modified, the question is, how is this possible? because it should return the same value on both calls, true or false, no matter the initial value of the variable that is receiving the returning value of the method.

View Replies View Related

Implementing Two Stack In One Array?

Jan 5, 2015

is this program correct? for Implementing two Stack in one array. how can i solve it ?

public class Stackation11 {
int max = 10;
int [] array = new int[max];
int top;
int top1;
Stackation11(){

[code]....

View Replies View Related

Postfix Evaluation Using Stack?

Apr 24, 2014

after i am done calculating everything from numbers stack, i pop the last number and return it... my question is how can i catch an exception if the size of my numbers stack is greater than 1;

public static String evaluate(String input) {
char[] a = input.toCharArray();
if (input.isEmpty())
return "No input";
else if (input.equals(" "))
return "No input";
else if (input.equals(" "))

[code]....

View Replies View Related

Counting Duplicates In A Stack

Apr 11, 2014

Write a method compressDuplicates that accepts a stack of integers as a parameter and that replaces each sequence of duplicates with a pair of values: a count of the number of duplicates, followed by the actual duplicated number. For example, suppose a variable called s stores the following sequence of values:

bottom [2, 2, 2, 2, 2, -5, -5, 3, 3, 3, 3, 4, 4, 1, 0, 17, 17] top

If we make the call of compressDuplicates(s);, after the call s should store the following values:

bottom [5, 2, 2, -5, 4, 3, 2, 4, 1, 1, 1, 0, 2, 17] top

This new stack indicates that the original had 5 occurrences of 2 at the bottom of the stack followed by 2 occurrences of -5 followed by 4 occurrences of 3, and so on. This process works best when there are many duplicates in a row. For example, if the stack instead had stored:

bottom [10, 20, 10, 20, 20, 10] top

Then the resulting stack after the call ends up being longer than the original:

bottom [1, 10, 1, 20, 1, 10, 2, 20, 1, 10] top

If the stack is empty, your method should not change it. You may use one queue as auxiliary storage to solve this problem. You may not use any other auxiliary data structures to solve this problem, although you can have as many simple variables as you like. You may not use recursion to solve this problem. For full credit your code must run in O(n) time where n is the number of elements of the original stack.

I wrote a code but still having a problem with it , am I allowed to use 3 while loops ?

public void compressDuplicates(Stack<Integer> s ){
Stack<Integer> backup= new Stack<Integer>();
int count = 1;
while(!s.isEmpty()){
int temp = s.pop();

[Code] .....
 
// example 1

Expected output : bottom [5, 2, 2, -5, 4, 3, 2, 4, 1, 1, 1, 0, 2, 17] top

My output: //bottom [17, 2, 2, 2, 2, 2, -5, -5, 3, 3, 3, 3, 4, 4, 1, 0, 17, 17] top

View Replies View Related

How To Take Top Of Stack And Convert It To String

May 2, 2015

For my classes I wrote I have puts strings into a stack and also a queue and am wondering how to take the top of the stack and the front of the queue and turn those into strings in my main class and run them through while loops that will detect if they are palindromes or not. Right now I am trying to peek and use first to put in my while loop but they don't work with the .charAt because they are not considered strings I think.

import java.util.Stack;
public class Palindrome {
public static void main(String[] args) {
// TODO Auto-generated method stub
String enteredLine;
int leftStack, rightStack;
int leftQueue, rightQueue;

[Code] .....

View Replies View Related

Stack Is Adding Same Values

Jun 16, 2014

In my application a series of inputs and a calculated result. At the end of each loop these inputs and calculations are displayed. After the loop is over with the user does not enter a string that is "y" or "Y", I want these inputs and the calculation to be displayed in a First in First Out format or a stack. I am using a LinkedList that is used in a class creating a stack.

Here is the code for my stack.

Java Code:

import java.util.LinkedList;
public class GenericStack<E> {
LinkedList<E> stack = new LinkedList<>();
public void Push(E element) {
stack.addFirst(element);

[Code] ....

Here is the code containing the main method. The methods other than the main method are probably not relevant to the problem, but take a look if you like.

Java Code:

import java.util.*;
import java.text.*;
public class FutureValueApp
{
public static void main(String[] args) {
GenericStack<String> stack = new GenericStack<>();

[Code] ....

The stack seems to be adding the same inputs and the same calculation from the first loop, even when it is on it's 2nd or third loop. I am getting this output.

Java Code:

Monthly Inv.Int. RateYearsFuture Value
$5.002.0%5$315.76
$5.002.0%5$315.76 mh_sh_highlight_all('java');

View Replies View Related







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