2 Replies - 493 Views - Last Post: 18 November 2012 - 04:10 PM Rate Topic: -----

#1 mathnerd  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 29-October 12

I have one logical error in my java program?

Posted 18 November 2012 - 12:39 PM

Directions:

* Create an array of 20 String Objects.
* Read in a list of names from the user using a sentinel value of -1 to mark the end of the list.
* Only add the name to the array if the name is not already on the list. (EX: If "Mary" is on the user list 3 times, it should only be in the Names array one time.)
* Print out the total number of names, and print out all of the names in the array.
* Remember to use methods whenever you can.

import java.util.Scanner;

public class NameList
{
   public static void main(String [] args)
   {
      Scanner reader = new Scanner(System.in);
	   
      // An array of 20 String Objects is created
      String [] names = new String [20];
      
      // Instance variables
      String appellation;
      int count = 0;
      final String SENTINEL = "-1";
      boolean isFound = false;
      
      System.out.println("Welcome to the Name List Program:-)");
      System.out.println("***********************************");
      
      // Ask the user for a first name or "-1" if the user would not like to proceed with the program 
      System.out.print("\nEnter a first name (" + SENTINEL + " to quit): ");
      appellation = reader.nextLine();
      
      // Keeps asking the user for first names until "-1" is entered
      while (!appellation.equals(SENTINEL))
      {
    	 System.out.print("Enter a first name (" + SENTINEL + " to quit): ");
         appellation = reader.nextLine();
    	  
         names[count] = appellation; 
         count++;
      }
      
      // The searchForName method is called
      isFound = searchForName(appellation, count, names);
      
      if (isFound)
      {
         count--;
    	 
         System.out.println("\nThere are a total of " + count + " names.");
         System.out.println("\nThe names in the array are:");
      
         // Prints out the array without any repeats
         for (int index = 0; index < count; index++)
         {
     	    System.out.println(names[index]);   
         }
      }   
   } 
   
   // A method that deletes names in the array if they are in it more than once
   public static boolean searchForName(String title, int logicalSize, String [] nameArray)
   {
      for (int i = 0; i < logicalSize; i++) 
      {
         // The name repeats in the array
    	 if (nameArray[i].equals(title))
         {  
        	logicalSize--;
            return true;
         }
      } 
      
      // The name does not repeat in the array
      return false; 
   } 
}



My logical error is located right here.

if (isFound)
      {
         count--; // <-----------
    	 
         System.out.println("\nThere are a total of " + count + " names.");
         System.out.println("\nThe names in the array are:");
      
         // Prints out the array without any repeats
         for (int index = 0; index < count; index++)
         {
     	    System.out.println(names[index]);   
         }
      }   
   } 



When I decrement count, it only gets rid of one repeat. Hoe do I get rid of all the repeats?

User Interface:

Welcome to the Name List Program:-)
***********************************

Enter a first name (-1 to quit): Sarah
Enter a first name (-1 to quit): Joe
Enter a first name (-1 to quit): Sarah
Enter a first name (-1 to quit): Joe
Enter a first name (-1 to quit): -1

There are a total of 3 names.

The names in the array are:
Joe
Sarah
Joe

* The program only leaves out Sarah, and not Joe too.
* I need a way that will tell me how many times count needs to be decremented.

Is This A Good Question/Topic? 0
  • +

Replies To: I have one logical error in my java program?

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: I have one logical error in my java program?

Posted 18 November 2012 - 12:56 PM

Duplicated thread
http://www.dreaminco...1&#entry1743229
Please avoid double posting.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: I have one logical error in my java program?

Posted 18 November 2012 - 04:10 PM

You can give me all the -1 you want :)
The facts are that this question has been answered in one of your previous post and you waste everybody's time by asking the same question again
:v:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1