Results 1 to 7 of 7

Thread: stacks

  1. #1
    nevets93 is offline Member
    Join Date
    Oct 2010
    Posts
    9
    Rep Power
    0

    Default stacks

    i have a working program that prints ints from a list in reverse using a stack, but I need this to work if i was working with floating point in my list as well. I still have to use a stack though. Here is my code so far:


    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Stack;

    class Numlistclass
    {
    final int MAXCOUNT = 50;
    private int count;
    private int[] nums = new int[MAXCOUNT];

    public Numlistclass()
    {
    int i;

    count = 0;
    for (i = 0; i < MAXCOUNT; i++) nums[i] = 0;
    } // Numlistclass
    public void addnum(int num)
    {
    if (count == MAXCOUNT)
    System.out.println("List is full -- unable to store: "+num);
    else
    nums[count++] = num;
    } // addnum
    public void printnums(PrintWriter pw)
    {
    int i;

    pw.println("Numbers in list:");
    for (i = 0; i < count; i++) pw.println(nums[i]);
    } // printnums
    public void reversestack(PrintWriter pw)
    {
    int i;

    Stack lifo = new Stack();

    for (i = 0; i < count; i++)
    {
    lifo.push (nums[i]);
    }

    pw.println("Numbers in reverse: ");

    while ( !lifo.empty() )
    {
    pw.println (lifo.pop());
    }
    }
    } // class Numlistclass

    public class prog05
    {
    public static void main(String args[]) throws IOException
    {
    int num;
    String line;
    Numlistclass numlist = new Numlistclass();
    BufferedReader br = new BufferedReader(new FileReader("prog5i.dat"));
    PrintWriter pw = new PrintWriter(new FileWriter("prog5.out"));

    // store numbers in list
    while ((line = br.readLine()) != null)
    {
    num = Integer.parseInt(line);// convert line to numeric value
    numlist.addnum(num);
    }

    // write output
    numlist.printnums(pw);
    numlist.reversestack(pw);


    // finalize
    br.close();
    pw.close();
    }
    } // class prog05

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Are you asking how can I put ints AND floats in the Stack, then you can as your Stack currently can store any Object.

    Or are you asking how can you make the Stack hold ints OR floats then you will need to make a decision.
    Java Code:
    if blah {
        do int stack
    } else {
        do float stack
    }
    Or if you want something else then please provide a better explanation.

  3. #3
    nevets93 is offline Member
    Join Date
    Oct 2010
    Posts
    9
    Rep Power
    0

    Default

    i need the second one i think, how do i do that?
    Last edited by nevets93; 02-25-2011 at 02:50 AM.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Why not just assume that all inputs are floats. So if user enters 1 it really is 1.0. This means you cannot use Integer.parseInt. You will need to use the float equivalent.

  5. #5
    nevets93 is offline Member
    Join Date
    Oct 2010
    Posts
    9
    Rep Power
    0

    Default

    to make things more clear, here were my instructions:

    Your task is to write a program (Prog5.java) which utilizes a stack to reverse
    the order of a sequence of input integers. You will need to prepare a data file
    (prog5i.dat) which contains a dozen or so integers (it might be good to arrange
    them in ascending order). The program should print the original sequence of
    numbers, and then print the numbers in reverse order (the output should be
    placed in a file named prog5.out).

    After the program is working for input integers, prepare a second data file
    (prog5f.dat) that contains floating point numbers (numbers with a decimal
    point). Repeat the same task as above, using the second file and printing the
    floating point outputs. (Add the new task into the same program -- Prog5.java).

    Part of the challenge in this program is to make the second task as easy as
    possible by making the first solution as generic as possible. You should
    implement your stack as a Java class.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    A few points:

    1. for (i = 0; i < MAXCOUNT; i++) nums[i] = 0;
    When you create an array it will be filled with the default value. For int arrays the default is zero. Therefore the above is unnecessary.

    2. Why are you using an array at all. You are supposed to be using a Stack. Add the numbers directly to your Stack and not the array.

    3. I would pass the user input to the addNum method as a String. Then inside that method use an if statement to decide if to parse input as an int or float.

  7. #7
    nevets93 is offline Member
    Join Date
    Oct 2010
    Posts
    9
    Rep Power
    0

Similar Threads

  1. Stuck with stacks
    By Yakg in forum New To Java
    Replies: 2
    Last Post: 01-30-2011, 08:34 PM
  2. Help with stacks
    By kMel90 in forum New To Java
    Replies: 3
    Last Post: 12-05-2010, 01:02 AM
  3. Help with stacks
    By Srcee in forum New To Java
    Replies: 5
    Last Post: 11-01-2009, 11:23 AM
  4. Stacks
    By Zosden in forum Advanced Java
    Replies: 15
    Last Post: 05-05-2008, 08:16 AM
  5. Using Stacks
    By ravian in forum New To Java
    Replies: 7
    Last Post: 11-28-2007, 09:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •