Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By Tolls

Thread: Please help with array problem, thanks

  1. #1
    ConnorSG1 is offline Member
    Join Date
    Sep 2012
    Posts
    14
    Rep Power
    0

    Default Please help with array problem, thanks

    The program is to prompt user to enter titles of three songs and their duration and display, promt user to delete a song and it corresponding duration and display. When deleted i mean assign the value null or 0 to the array element however it will not display null or 0 it displays the first values entered.

    Thanks for any help,





    Java Code:
    import com.sun.javafx.scene.EnteredExitedHandler;
    
    import java.util.Scanner;
    
    /**
     * Created with IntelliJ IDEA.
     * User: CONNOR
     * Date: 12/11/12
     * Time: 13:22
     * PROMPT USER TO ENTER 3 SONGS AND THEIR DURATION, PROMPT USER TO CHOOSE A SONG TO DELETE, DISPLAY LIST WITH DELETED SONG
     */
    public class One_Dimensional_And_Parallel_Arrays
    {
       public static void main(String[] args)
       {
          //create an instance of the Scanner class to detect user input;
          Scanner keyboard = new Scanner(System.in);
    
          final int NUMBER_OF_TITLES = 3;
    
          String deleteTitle;
          int number = 0;
          boolean found;
          int location;
    
          //create an array to hold 10 elements;
          String[] songTitle = new String[NUMBER_OF_TITLES];
    
          //parallel array to songTitle;
          double[] songDuration = new double[10];
    
    
    
          //loop until user assigns elements to each index of array;
          for (int count = 0; count < NUMBER_OF_TITLES; count++)
          {
             //prompt user to enter song titles;
             System.out.println("Enter song title " + (count +1) + ". \t");
    
             //assign input to array;
             songTitle[count] = keyboard.nextLine();
    
          }//end of for-loop;
    
    
    
          //loop until user assigns elements to each index in parallel array;
          for (int counter = 0; counter < NUMBER_OF_TITLES; counter++)
          {
             //prompt user to input song length;
             System.out.println("Enter the duration of: " + songTitle[counter] + " - ");
    
             //assign input to song duration array;
             songDuration[counter] = keyboard.nextDouble();
          }//end of for-loop;
    
    
    
          //loop to print all elements of array;
          for (int loop = 0; loop < NUMBER_OF_TITLES; loop++)
          {
             System.out.println("Title " + (loop + 1) + ". is: " + songTitle[loop] + " - " + songDuration[loop]);
          }//end of for-loop;
    
    
    
    
          //prompt user to input name of song to be deleted and assign input to deleteTitle;
          System.out.print("Enter the title of a song to be deleted:");
          deleteTitle = keyboard.next();
    
          //found is false util deleteTitle is found;
          found = (songTitle[number] == deleteTitle);
    
          //loop while number is less than NUMBER_OF_TITLES and boolean found is false;
          while ((number < songTitle.length) && (found == false))
          {
    
             //will check each time number increments to see if the titles match the element of that array index;
             found = (songTitle[number] == deleteTitle);
    
             //increment number by 1 each time;
             number++;
    
          }//end of while-loop;
    
          // if data not found indicate this to user;
          if (found == false)
          {
             //indicate to user it has not yet been found;
             System.out.println("Searching...");
    
          }//end of if-statement;
    
          else
          {
             //assign value held in number to location;
             location = number;
    
          }//end of else-statement;
    
    
    
          //IT SEEMS TO BE HERE THAT SOMETHING IS GOING WRONG, IF I PRINT THE ELEMENTS INDIVIDUALLY WITHIN THE PRINT STATEMENT BELOW
          //THEY DISPLAY NULL AND 0 LIKE I WANT, BUT WHEN I TRY TO PRINT THE ENTIRE LIST WITH A FOR-LOOP AT THE END THE VALUES HAVE NOT CHANGED!;
    
          //loop to delete selected elements;
          for (int count = 0; count < NUMBER_OF_TITLES; count++)
          {
             //if array index equals number, replace that element with null/0;
             if (songDuration[count] == number)
             {
                //i.e. print sonDuration[count] will display 0;
                songDuration[count] = 0;
    
                songTitle[count] = null;
    
             } //end of if-statement;
    
          }//end of for-loop;
    
    
    
    
          //loop to print all elements of array;
          for (int loop = 0; loop < NUMBER_OF_TITLES; loop++)
          {
             System.out.println("Title " + (loop + 1) + ". is: " + songTitle[loop] + " - " + songDuration[loop]);
          }//end of for-loop;
    
    
       }//main
    }//class
    Last edited by ConnorSG1; 11-12-2012 at 05:14 PM. Reason: no code tags

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,945
    Rep Power
    19

    Default Re: Please help with array problem, thanks

    Please use [code] tags [/code] when posting code.
    A lot of us won't bother reading unformatted code.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    ConnorSG1 is offline Member
    Join Date
    Sep 2012
    Posts
    14
    Rep Power
    0

    Lightbulb Re: Please help with array problem, thanks

    ok will do thanks;

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,945
    Rep Power
    19

    Default Re: Please help with array problem, thanks

    Java Code:
    found = (songTitle[number] == deleteTitle);
    number++;
    First, you need to use equals() to compare strings for equality.
    The above will likely never match.

    But then, even if you got a match for the song title at 'number', you then increment 'number' by 1, so you would actually end up deleting the next song, not the one entered.
    ConnorSG1 likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    ConnorSG1 is offline Member
    Join Date
    Sep 2012
    Posts
    14
    Rep Power
    0

    Default Re: Please help with array problem, thanks

    thanks a million

Similar Threads

  1. Replies: 3
    Last Post: 12-28-2013, 09:39 PM
  2. Some problem with the array
    By naveenpaul1987 in forum New To Java
    Replies: 2
    Last Post: 01-03-2012, 04:58 AM
  3. Replies: 0
    Last Post: 05-12-2011, 07:04 PM
  4. Array problem
    By Red727 in forum New To Java
    Replies: 2
    Last Post: 12-19-2010, 03:26 AM
  5. array problem
    By oceansdepth in forum New To Java
    Replies: 3
    Last Post: 04-05-2008, 02:25 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
  •