2 Replies - 181 Views - Last Post: 05 April 2013 - 02:30 PM Rate Topic: -----

#1 TheBearKing  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 11-February 11

Increment not working and method reloading

Posted 05 April 2013 - 12:49 PM

Hello all,

So the program I was tasked with writing is as follows: Create an array and have it behave a certain way. Long story short, I have the whole program working, except a few issues:

Issue 1: One of my methods is (obvious) adding elements to said array. The issue is when I call another method, it runs properly, but then if I try to call another method, it returns " '' added to list".

Issue 2: There is a variable called "numItems" that keeps track of how many items are currently stored in the array. When you add or delete an element from the array, it is either increment or decrement the numItems value. Also, when the method to empty the array is called, it should change the item count to 0. I can't seem to get them working within their respective methods. Right now, I have the numItems++, numItems--, and numItems = 0 in the case statement of the main method.

I'm sorry if I've done a crud job describing the issues I'm having. Here is the code.

Main Class
import java.util.*;

public class ListOMatic {
    public static void main(String[]args) {
        String[] RA = new String[5]; // Declare the array
        char user = '0'; // Declare char at 0
        int numItems = 0;
        Scanner input = new Scanner(System.in);
        Scanner secEntry = new Scanner(System.in);

        System.out.println("Welcome to Rusty's List-O-Matic");
        System.out.println("Valid Operations are: \n " +
        " + -> add string \n # -> display location of string \n " +
        "@ -> display string at location number \n " +
        "- -> remove string \n ? -> isEmpty check \n % -> empty list" + 
        "\n = -> print list \n $ -> quit");
        System.out.println();
        do {
            System.out.print("");
            user = input.next().charAt(0);
            switch(user) {
                /** Operation to add items to list */
                case '+':
                if (RA[4] != null) {
                    System.out.println("List full. Sorry...");
                    break;
                }
                System.out.print("");
                String newItem = secEntry.nextLine();
                List.addString(RA, newItem, numItems);
                numItems++;
                break;
                
                /** Operation to display location of string in list */
                case '#':
                int displayLocation = secEntry.nextInt();
                List.disStrLoc(RA, displayLocation);
                break;
                
                /** Operation to display string at location number */
                case '@':
                String displayString = secEntry.nextLine();
                List.findStrAtLoc(RA, displayString);
                break;
                
                /** Operation to delete "front" of array */
                case '-':
                if (numItems == 0){
                    System.out.println("List empty. Sorry...");
                    break;
                }
                System.out.print("");
                int userRem = secEntry.nextInt();
                List.removeString(RA, userRem, numItems);
                numItems--;
                break;
                
                /** Operation to check if array is empty */
                case '?':
                List.isEmpty(numItems);
                break;
                
                /** Operation to delete all items from array */
                case '%':
                List.emptyList(RA, numItems);
                numItems = 0;
                break;
                
                case '=':
                List.printList(RA, numItems);
                break;
                
                /** Operation to the programm program */
                case '$':
                System.out.println();
                System.out.println("Thank you for using Rusty's List-O-Matic!");
                break;
                
                /** Default message if operation is invalid */
                default:
                System.out.println("Not a valid selection!");
                break;
            }
        } while (user != '$');
    }
}


List class [contains all of the methods]
/** List Project, v1 (Array Based List)
 * List Class of List-O-Matic
 * Documentation in handout
 * 
 * Russell Wills
 * CIS 261: Data Structures Programming
 * List-O-Matic Program
 * Mr. Collard
 * 4/9/2013
 */

import java.util.*;

public class List {
    public static void addString(String[] RA, String addItem, int numItems) {
        for (int i = 0; i < RA.length; i++){
            if(RA[i] == null){
                RA[i] = addItem;
                break;
            }
        }
        System.out.println("'" + addItem + "' added to list.");
    }

    public static void disStrLoc(String[] RA, int displayLocation) {
        for (int i = 0; i < RA.length; i++){
            if(RA[i] != null) {
                if(displayLocation == i) {
                    System.out.println("Index " + i + " contains '" + RA[i] + "'");
                    break;
                }
            }
            System.out.println("Index " + i + " is empty.");
        }
    }

    public static void findStrAtLoc(String[] RA, String displayString){
        for (int i = 0; i < RA.length; i++) {
            if (RA[i] == null) {
                System.out.println(displayString + " cannot be found.");
                break;
            }
            else if(RA[i].equals(displayString)){
                System.out.println(displayString + " is at index " + i);
            }
        }
    }

    public static void removeString(String[] RA, int userRem, int numItems) {
        for (int i = 0; i < RA.length; i++){
            if (userRem >= 0 && userRem < 5){
                System.out.println("'" + RA[userRem] + "' has been removed from list");
                RA[userRem] = RA[numItems - 1];
                RA[numItems - 1] = null;
                break;
            }
            else {
                System.out.println("Out of bounds!");
                break;
            }
        }
    }

    public static void isEmpty(int numItems) {
        if (numItems == 0) {
            System.out.println("List is empty");
        }
        else
            System.out.println("List is NOT empty");
    }

    public static void emptyList(String[] RA, int numItems) {
        Arrays.fill(RA, null);
        System.out.println("List has been emptied");
    }

    public static void printList(String[] RA, int numItems) {
        for(int i = 0; i < RA.length; i++){
            if (numItems == 0) {
                System.out.println("List is empty.");
                break;
            }   
        }
        System.out.println("The number of items is " + numItems + ":");
        System.out.println();
        for (int j = 0; j < RA.length; j++)
        {
            System.out.println(RA[j]);
        }
    }   
}


Is This A Good Question/Topic? 0
  • +

Replies To: Increment not working and method reloading

#2 TheBearKing  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 11-February 11

Re: Increment not working and method reloading

Posted 05 April 2013 - 01:15 PM

Sorry for double posting, but it wouldn't let me edit the post. I got the first issue resolved. I changed line 42 to String newItem = secEntry.next();, and that seemed to take care of it. Second issue still up for grabs though.
Was This Post Helpful? 0
  • +
  • -

#3 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: Increment not working and method reloading

Posted 05 April 2013 - 02:30 PM

Give a sample run: Show your inputs, the actual output, and the desired output.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1