Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: adding and removing from an array while conditions are met.

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy adding and removing from an array while conditions are met.

    My problems/needs are commented in my code. Any help would be greatly appreciated. Thank you in advance. My existing code may even be off. I feel like there are so many variables. I just need help lol ... its getting frustrating at this point and my deadline is quickly approaching. I don't need EXACT code, I just need a kick in the ass in the right direction.

    The project prompt:

    The number of rows they have to stand on. The maximum number of rows is 10. The rows are labelled with capital letters, 'A', 'B', 'C', etc.
    For each row, the number of positions in the row. The maximum number of positions is 8. The positions are numbered with integers, 1, 2, 3, etc.
    The conductor then starts assigning people to positions, but is constrained by weight limits: Musicians, fully clothed and holding their instruments, weigh from 45kg to 200kg, and the total weight of a row may not exceed 100kg per position (e.g., a row with 5 positions may not have more than 500kg of musicians on it). The conductor wants a program that allows musicians to be added and removed from positions, while ensuring the constraints are all met. At any stage the conductor wants to be able to see the current assignment - the weight in each position (0kg for vacant positions) and the total & average weight for each row.
    The program must be menu driven, with options to:

    Add a musician (by weight) to a vacant position.
    Remove a musician from an occupied position.
    Print the current assignment.
    Exit (so the musicians can start playing)
    The program must be reasonably idiot proof:
    Menu options must be accepted in upper and lower case.
    Row letters must be accepted in upper and lower case.
    All input must be checked to be in range, and if not the user must be asked to input again.
    You may assume that numeric input will be syntactically correct.
    Here's what a sample run should look like (with the keyboard input shown in italics) ...

    Welcome to the Band of the Hour
    -------------------------------
    Please enter number of rows : 11
    ERROR: Out of range, try again : 3
    Please enter number of positions in row A : -4
    ERROR: Out of range, try again : 3
    Please enter number of positions in row B : 4
    Please enter number of positions in row C : 5

    (A)dd, (R)emove, (P)rint, e(X)it : p

    A: 0.0 0.0 0.0 [ 0.0, 0.0]
    B: 0.0 0.0 0.0 0.0 [ 0.0, 0.0]
    C: 0.0 0.0 0.0 0.0 0.0 [ 0.0, 0.0]


    import java.util.Scanner;
     
     
    public class project {
        private static int rowSelect;
        private static int positionRow;
        private static int rowSize;
        private static double musicianWeight;
     
        int totalWeight;
     
    private static Scanner keyboard = new Scanner(System.in);
     
    public static void main() {
    double[][] row ;
    double average, total,weight;
    double MAX_ROW_WEIGHT = 0.0; 
    int positionInRow, index, numberOfRows;
     
    System.out.println("Welcome to the Band of the Hour");
    System.out.println("-------------------------------");
    System.out.print("Please enter number of rows : ");
     
    numberOfRows = keyboard.nextInt();
    row = new double [numberOfRows][];
     
     
     
    //Add number of positions
    for (index = 0; index < row.length; index++) {
        System.out.print("Please enter number of positions in row " + (char)((int) 'A' + index) + ":");
        rowSize = keyboard.nextInt();
        row [index] = new double [rowSize];
     
     
        for(positionInRow = 0; positionInRow < row.length; positionInRow++){
            row[index][positionInRow]= 0.0;
            }
    displayMenu (row);
        }
    }
    //function for displaying menu to the user
    private static void displayMenu (double[][] row) {
    char command;
        do {
            System.out.println("(A)dd, (R)emove, (P)rint, e(X)it (Not case sensitive):  ");
            command = keyboard.nextLine().charAt(0);
     
            switch (command){
     
                case 'A'|'a':
                addMusician(row);
                break;
     
                case 'R'|'r':
                removeMusician(row);
                break;
     
                case 'P'|'p':
                printBandstand(row);
                break;
     
                case 'X'|'x':
                System.out.println("Thank you for using my program!");
                break;
     
                default:
                System.out.println("ERROR: Invalid option, enter an acceptable value :");
        }
    } 
        while 
                (command != 'X'); 
     
    }
     
     
     
    private static void addMusician(double[][] row){
     double MAX_WEIGHT = 200.0;
     double MIN_WEIGHT = 45.0;
        System.out.println("Please select Row to ADD musician: ");
        rowSelect = keyboard. nextInt();
     
        if (rowSelect > 10 | rowSelect < 0)
            System.out.println("Error, please enter a valid row ");
     
            else 
                System.out.println("Please enter position in row to ADD musician: ");
                positionRow = keyboard.nextInt();
                //I want to display the amount of positions in the row next to this question (1-?)
                // it would be "Active" displaying whatever the user inputed as the total number of spaces in this row.
                //This will help the user know how many positions are in the row,
                //instead of selecting something that doesnt work
        if (positionRow < rowSize){
            System.out.println("Please enter weight of musician: ");
            musicianWeight = keyboard.nextDouble();
     
            //I want to check if musicianWeight is below and above the min and max
            //weight allowed, before adding it
            //add user weight to the postion 
     
            System.out.println("*****Musician Added*****");
        }
            else 
                System.out.println("Invalid position");
     
                //I also want to make sure that the position in the array is empty (0.0).
                //I dont want to overwrite a value,just add to only blank spaces. 
                // I want to keep a running total of the current weight in the numeric portion of the 2nd array,
                //I want to display that total alongside "Please enter weight of musician  (currentweight in the array)"
                //This would be important so the user knows how much more they can add before reachhing the limit
     
     
    }
    //method for removing musician from a position
    private static void removeMusician(double[][] row){
     
     
     
        System.out.println("Please select Row to REMOVE musician: ");
        rowSelect = keyboard. nextInt();
     
        if (rowSelect > 10 | rowSelect < 0){
            System.out.println("Error, please enter a valid row ");
        }
        else 
            //select row to begin removing musician  
        System.out.println("Please enter POSITION in row to REMOVE musician: ");
        positionRow = keyboard.nextInt();
     
     
        if (positionRow <= rowSize){
            //must make the array position 0.0
            double[rowSelect][positionRow] = 0.0; //getting error to remname the local variable.
               // I need all of this to keep looping until a valid position is entered. 
            }
        else {
     
                System.out.println(" Error, please enter a valid position in row!");
            }
     
     
    }
     
     
     
     
    //method for printing the entire bandstand       
    private static void printBandstand(double[][] row){
        System.out.println("Band Chart");
        System.out.println("------------");
    //println all code needed to output the array 
    //print average + total of the weight in each array position (Average, total)
        System.out.println("¸.•*¨*•♫♪¸¸.•*¨*•♫♪¸¸.•*¨*•♫♪¸¸.•*¨*•♫♪¸¸¸.•*¨*•♫♪");
     
     
     
     
     
    } //reaching end of file while parsing error, just saying...


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: adding and removing from an array while conditions are met.

    Here's your first kick: Don't ask us to read through 160 lines of code to find what you need help with. It you're getting errors, post them exactly as they appear at your end, stack trace and all. Ask specific questions, describe the problem(s) you're having, and ask for help to fix specific problems. Please don't make us hunt for or find the problem ourselves.

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: adding and removing from an array while conditions are met.

    You can append numbers to the end of Strings. So, you can say something like:
    int someNumber = 5;
    System.out.println("Please enter position in row to ADD musician: "+someNumber);
    Which would print out:
    Please enter position in row to ADD musician: 5
    I want to check if musicianWeight is below and above the min and max weight allowed, before adding it add user weight to the postion
    Have you tried anything for this one? Based on your code, you seem to understand how to use if/else statements, so I'm not sure why you are being tripped up on this one.

    As for checking if an array spot is empty, you need to check for the default value of your array's type. In your case, you have an array of doubles, so you want to check if the value in a certain index is equal to double's default value, which is 0. So if an index has the value of 0, you can assume it is empty (unless 0 is a valid value to insert in your array, in which you would need to manually set each value of your array to some invalid value upon initialization).

    getting error to remname the local variable. I need all of this to keep looping until a valid position is entered.
    What error?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  4. #4
    Junior Member
    Join Date
    Nov 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: adding and removing from an array while conditions are met.

    ok , I have updated my code a little but for some reason my code is stopping after asking the amount for ROW A , What am I missing?
    /*
     
    package athinaband;
     
    import java.util.Scanner;
     
     
    public class Athinaband {
        private static int rowSelect;
        private static int positionRow;
        private static int rowSize;
        private static double musicianWeight;
        private static double totalWeight;
     
    private static Scanner keyboard = new Scanner(System.in);
     
    public static void main(String[] args) {
    double[][] row ;
    double average, total,weight;
    double MAX_ROW_WEIGHT = 0.0; 
    int positionInRow, index, numberOfRows;
     
    System.out.println("Welcome to the Band of the Hour");
    System.out.println("-------------------------------");
    System.out.print("Please enter number of rows : ");
     
    numberOfRows = keyboard.nextInt();
    row = new double [numberOfRows][];
     
     
     
    do 
    //Add number of positions
    for (index = 0; index < row.length; index++) {
        System.out.print("Please enter number of positions in row " + (char)((int) 'A' + index) + ":");
        rowSize = keyboard.nextInt();
        row [index] = new double [rowSize];
     
        if (rowSize < 8 || rowSize >= 0 );{
            for(positionInRow = 0; positionInRow < row.length; positionInRow++){
            row[index][positionInRow]= 0.0;
            }
    displayMenu (row);
        }
     
      while ( index <row.length)  
     
    //function for displaying menu to the user
    private static void displayMenu (double[][] row) {
    char command;
        do {
            System.out.println("(A)dd, (R)emove, (P)rint, e(X)it (Not case sensitive):  ");
            command = keyboard.nextLine().charAt(0);
     
            switch (command){
     
                case 'A'|'a':
                addMusician(row);
                break;
     
                case 'R'|'r':
                removeMusician(row);
                break;
     
                case 'P'|'p':
     
                break;
     
                case 'X'|'x':
                System.out.println("¸.•*¨*•♫♪¸¸.•*¨*•♫♪¸Enjoy The Music¸.•*¨*•♫♪¸¸.•*¨*•♫♪¸¸¸.•*¨*•♫♪");
                break;
     
                default:
                System.out.println("ERROR: Invalid option, enter an acceptable value :");
        }
    } 
        while 
                (command != 'X'); 
     
    }
     
    public static int getNumAvailablePositions(int rowSelect, double[][] row) {
     
        int sum = 0;
        for (int i = 0; i < row[rowSelect].length; i ++) {
            if (row[rowSelect][i] == 0.0) {
                sum++;
     
                // 
            }
        }
        return sum;
    }
     
    private static void addMusician(double[][] row){
     double MAX_WEIGHT = 200.0;
     double MIN_WEIGHT = 45.0;
        System.out.println("Please select Row to ADD musician: ");
        rowSelect = keyboard. nextInt();
     
        if (rowSelect > 10 || rowSelect < 0) {
            System.out.println("Error, please enter a valid row ");
        }
            else {
                System.out.println("Amount of positions left in this row: " + getNumAvailablePositions(rowSelect, row));
                System.out.println("Please enter position in row to ADD musician: ");
                positionRow = keyboard.nextInt();
            }
     
        if (positionRow <= rowSize){
            System.out.println("Please enter weight of musician: ");
            musicianWeight = keyboard.nextDouble();
        }
       else {
     
        }
            System.out.println("Error, enter correct position in row");
     
            if (musicianWeight > MAX_WEIGHT || musicianWeight < MIN_WEIGHT) {
                System.out.println(" Error, please enter a valid weight!");
            }
     
                else {
     
                row[rowSelect][positionRow] = musicianWeight;
                System.out.println("*****Musician Added*****");
            } 
        }
     
    private static void removeMusician(double[][] row) {
     
        System.out.println("Please select row to REMOVE musician: ");
        rowSelect = keyboard.nextInt();
     
        if (rowSelect > 10 || rowSelect < 0){
            System.out.println("Error, please enter a valid row ");
        }
        else {
            //select row to begin removing musician  
        System.out.println("Please enter POSITION in row to REMOVE musician: ");
        positionRow = keyboard.nextInt();
    }
     
        if (positionRow <= rowSize){
            //must make the array position 0.0
            row[rowSelect][positionRow] = 0.0;
               // I need all of this to keep looping until a valid position is entered. 
    }
        else {
     
                System.out.println(" Error, please enter a valid position in row!");
    }
     
    }
     
     
     
     
     
       // System.out.println("Band Chart");
        //System.out.println("------------");
     
     
     
        //return 0;
     
        //Need to print the 2d array , alogn with average and totals 
        //Must think about better solution! Maybe make others classes for average and totals.

  5. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: adding and removing from an array while conditions are met.

    do {
        ......
    while ( index <row.length)
    Once the inner for loop ends the value of index will be greater than row.length, therefore outer do/while loop will also end.
    Improving the world one idiot at a time!

  6. #6
    Junior Member
    Join Date
    Nov 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: adding and removing from an array while conditions are met.

    reached EOF while parsing error. Don't know why?

    do {
    //Add number of positions
    for (index = 0; index < row.length; index++) {
        System.out.print("Please enter number of positions in row " + (char)((int) 'A' + index) + ":");
        rowSize = keyboard.nextInt();
        row [index] = new double [rowSize];
     
        if (rowSize < 8 || rowSize >= 0 );{
            for(positionInRow = 0; positionInRow < row.length; positionInRow++){
            row[index][positionInRow]= 0.0;
            }
    displayMenu (row);
        }
    }
    }
      while ( index < row.length)
    Last edited by ddnpresident; November 3rd, 2013 at 11:19 PM. Reason: Forgot to put response

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: adding and removing from an array while conditions are met.

    Usually the error you've cited occurs because the end of the file was found before the last close brace. Check your braces and make sure they match up.

Similar Threads

  1. Removing duplicates from an array
    By Parm in forum Collections and Generics
    Replies: 1
    Last Post: October 13th, 2013, 03:30 AM
  2. Removing duplicates from an array
    By Parm in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 13th, 2013, 03:23 AM
  3. Best way of adding and removing?
    By J-moges in forum Java Theory & Questions
    Replies: 3
    Last Post: May 23rd, 2013, 01:43 PM
  4. Removing duplicates from an Array
    By Rizza in forum Collections and Generics
    Replies: 1
    Last Post: February 21st, 2012, 06:38 PM
  5. Removing Duplicate Values From an Array
    By nicsa in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 30th, 2011, 07:55 PM