Linear search of an array with strings

Duplicate posts topics merged

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 12210 Views - Last Post: 05 December 2008 - 11:19 PM Rate Topic: -----

#1 xxwolfsrainxx  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 93
  • Joined: 04-December 08

Linear search of an array with strings

Posted 04 December 2008 - 09:38 PM

I need to read those two files into an array using FileInputStream. Cannot use file read. I need assistance. Everytime I tried look up to write taking in the boyNames and the girlNames from boynames.txt and girlsnames..txt into a boyNames [] array and a girlNames [] array. But when ever I looked it up I get it using buffered reader and FileReader. But we cannot use file reader or buffered Reader.
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.ObjectInputStream;
import java.io.FileOutputStream;
import java.io.BufferedReader;
public class Program13Shaban
{
  public static void main(String[] args)
  {
  Scanner keyboard = new Scanner(System.in);
  System.out.println("\tI will read boy names and girl names");
  Scanner inputStreamBoys = null;
  Scanner inputStreamGirls = null;
  Scanner inputStreamShaban = null;
  PrintWriter outputStreamNames = null;
  try
	{
	 inputStreamBoys =
		  new Scanner(new FileInputStream("boynames.txt"));
	 inputStreamGirls =
		  new Scanner(new FileInputStream("girlnames.txt"));
	 outputStreamNames = new PrintWriter(
		  new FileOutputStream("Shaban.txt"));
	 }
  catch(FileNotFoundException e)
  {
  System.out.println("\tFile boysnames.txt or girlsnames.txt was 
  not found");
  System.out.println("\tor could not be opened.");
  System.exit(0);
  }
  String lineBoys = null;
  String lineGirls = null;
  int count = 0;
  while (inputStreamGirls.hasNextLine( ) &&  
		   inputStreamBoys.hasNextLine( ))
   {
   lineBoys = inputStreamBoys.nextLine( );
   lineGirls = inputStreamGirls.nextLine( );
   count++;
   outputStreamNames.println(count + " " + lineBoys + "\t\t " + lineGirls);
   }
   inputStreamBoys.close( );
   inputStreamGirls.close();
   }
} 


Is This A Good Question/Topic? 0
  • +

Replies To: Linear search of an array with strings

#2 pbl  Icon User is offline

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

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Linear search of an array with strings

Posted 04 December 2008 - 09:46 PM

Just use
Scanner sin = new Scanner(new File("blablabla.txt"));
and use the scanner nextLine() method
Was This Post Helpful? 0
  • +
  • -

#3 xxwolfsrainxx  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 93
  • Joined: 04-December 08

Re: Linear search of an array with strings

Posted 04 December 2008 - 09:56 PM

Put that in an array for loop? Sorry, never learned how to add input from TXT file into an array. This is brand new for me. I could use a concrete example. Also these are Strings not int's or doubles. =/.

This post has been edited by xxwolfsrainxx: 04 December 2008 - 10:06 PM

Was This Post Helpful? 0
  • +
  • -

#4 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Linear search of an array with strings

Posted 04 December 2008 - 11:13 PM

Use an ArrayList.
Was This Post Helpful? 0
  • +
  • -

#5 xxwolfsrainxx  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 93
  • Joined: 04-December 08

Re: Linear search of an array with strings

Posted 05 December 2008 - 06:40 AM

An array list isn't allowed in this assignment. Although we do know what they are. I just need to know how to write the array to take in the lines of text instead.
String boyNames [] = new String [500] there are 500 boy names and girl names. THe problem that I'm having is writing the for loop for the array so that it takes in the strings of the text.
Was This Post Helpful? 0
  • +
  • -

#6 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Linear search of an array with strings

Posted 05 December 2008 - 07:23 AM

for (int i = 0; i < boyNames.length; i++) {
   if (scannner.hasNextLine())
	   boyNames[i] = scanner.nextLine();
   else
	   break;
}


This post has been edited by n8wxs: 05 December 2008 - 07:24 AM

Was This Post Helpful? 0
  • +
  • -

#7 xxwolfsrainxx  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 93
  • Joined: 04-December 08

Re: Linear search of an array with strings

Posted 05 December 2008 - 06:17 PM

import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.ObjectInputStream;
import java.io.FileOutputStream;
import java.io.BufferedReader;
public class Program13Shaban
{
	public static void main(String[] args)
	{
 	   Scanner keyboard = new Scanner(System.in);
 	   System.out.println("\tI will read boy names and girl names");
	   Scanner inputStreamBoys = null;
	   Scanner inputStreamGirls = null;
	   Scanner inputStreamShaban = null;
	   PrintWriter outputStreamNames = null;
	   try
	   {
		   inputStreamBoys =
			  new Scanner(new FileInputStream("boynames.txt"));
		   inputStreamGirls =
		   	  new Scanner(new FileInputStream("girlnames.txt"));
			 outputStreamNames = new PrintWriter(
			  new FileOutputStream("Shaban.txt"));
	   }
	   catch(FileNotFoundException e)
	   {
		   System.out.println("\tFile boysnames.txt or girlsnames.txt was not found");
		   System.out.println("\tor could not be opened.");
		   System.exit(0);
	   }
	   String boyNames [] = new String [1000];
	   String girlNames [] = new String [1000];
	   for (int read = 0; read < 1000; read++)
	   {
		   if(inputStreamBoys.hasNextLine())
		   	boyNames[read] = inputStreamBoys.nextLine( );
		   else
		   	break;
	   }
	   inputStreamBoys.close( );
	   inputStreamGirls.close();
	   System.out.print("\tWhat is the name you want me to search for: ");
	   String searchKey = keyboard.next();
	   int foundIndex = -1;  // -1 indicates not found (yet)
	   int counter = 0;
	   while(counter < boyNames.length && foundIndex == -1)
	   {
	   		if(boyNames[counter].equals(searchKey))
	   		{
	   			foundIndex = counter;  //stash location of the match into foundIndex
	   		}
	   		counter++;
	   }
	   // postcondition:  either we have found the searchKey value and its location
	   // is in foundIndex, or we didn't find the value and foundIndex = -1.
	   if(foundIndex == -1)
	   		System.out.println("\n\tsearchKey value of " + searchKey + " not found in array.");
	   else
	   		System.out.println("\n\tsearchKey value of " + searchKey
	   			+ " was found at position " + foundIndex);

	}
} 


The array isn't filled with the 1000 lines. Only around 303 of the lines are added into the array.
Was This Post Helpful? 0
  • +
  • -

#8 xxwolfsrainxx  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 93
  • Joined: 04-December 08

Re: Linear search of an array with strings

Posted 05 December 2008 - 08:22 PM

Okay I fixed it. NOw i have to do a linear search for a String within the array. Any ideas?
Was This Post Helpful? 0
  • +
  • -

#9 xxwolfsrainxx  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 93
  • Joined: 04-December 08

Re: Linear search of an array with strings

Posted 05 December 2008 - 08:34 PM

import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.ObjectInputStream;
import java.io.FileOutputStream;
import java.io.BufferedReader;
public class Program13Shaban
{
	public static void main(String[] args)
	{
 	   Scanner keyboard = new Scanner(System.in);
 	   System.out.println("\tI will read boy names and girl names");
	   Scanner inputStreamBoys = null;
	   Scanner inputStreamGirls = null;
	   try
	   {
		   inputStreamBoys =
			  new Scanner(new FileInputStream("boynames.txt"));
		   inputStreamGirls =
		   	  new Scanner(new FileInputStream("girlnames.txt"));
	   }
	   catch(FileNotFoundException e)
	   {
		   System.out.println("\tFile boysnames.txt or girlsnames.txt was not found");
		   System.out.println("\tor could not be opened.");
		   System.exit(0);
	   }
	   String boyNames [] = new String [1000];
	   String girlNames [] = new String [1000];
	   while (inputStreamBoys.hasNextLine( ))
	   {
		   int count = 0;
		   boyNames[count] = inputStreamBoys.nextLine( );
		   count++;
	   }
	   while (inputStreamGirls.hasNextLine( ))
	   {
		   int count = 0;
		   girlNames[count] = inputStreamGirls.nextLine( );
		   count++;
	   }

	   System.out.print("\tWhat is the name you want me to search for: ");
	   String searchKey = keyboard.next();
	   int foundIndex = -1;  // -1 indicates not found (yet)
	   int counter = 0;
	   while(counter < boyNames.length && foundIndex == -1)
	   {
	   		if(searchKey.equals(boyNames[counter]))
	   		{
	   			foundIndex = counter;  //stash location of the match into foundIndex
	   		}
	   		else
	   		counter++;
	   }
	   // postcondition:  either we have found the searchKey value and its location
	   // is in foundIndex, or we didn't find the value and foundIndex = -1.
	   if(foundIndex == -1)
	   		System.out.println("\n\tsearchKey value of " + searchKey + " not found in any array.");
	   else
	   		System.out.println("\n\tsearchKey value of " + searchKey
	   			+ " was found at position " + foundIndex);
	   inputStreamBoys.close( );
	   inputStreamGirls.close( );
	}
} 

Trying to search the boyNames array to search for a name that the user inputs. However I am having difficulty doing this. Please can you offer your assistance.
Was This Post Helpful? 0
  • +
  • -

#10 akozlik  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 90
  • View blog
  • Posts: 797
  • Joined: 25-February 08

Re: Linear search of an array with strings

Posted 05 December 2008 - 08:39 PM

Can you give your boynames.txt and girlnames.txt files?
Was This Post Helpful? 0
  • +
  • -

#11 xxwolfsrainxx  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 93
  • Joined: 04-December 08

Re: Linear search of an array with strings

Posted 05 December 2008 - 08:49 PM

yes I can. Do you want me to email them to you or post them? THey are pretty large. Each contains 1000 names and rankings in popularity.
Was This Post Helpful? 0
  • +
  • -

#12 akozlik  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 90
  • View blog
  • Posts: 797
  • Joined: 25-February 08

Re: Linear search of an array with strings

Posted 05 December 2008 - 09:02 PM

Just zip them and attach them to your post.
Was This Post Helpful? 0
  • +
  • -

#13 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Linear search of an array with strings

Posted 05 December 2008 - 09:14 PM

boolean found = false;
int foundIndex;
String searchTarget;

for (int i = 0; i < boyNames.length; i++)
   if (boyNames[i].equals(searchTarget)) {
		found = true;
		foundIndex = i;
		break;
   }


This post has been edited by n8wxs: 05 December 2008 - 09:14 PM

Was This Post Helpful? 0
  • +
  • -

#14 xxwolfsrainxx  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 93
  • Joined: 04-December 08

Re: Linear search of an array with strings

Posted 05 December 2008 - 09:15 PM

Here they are. I changed the entering of the numbers into an array to a for loop because it worked better. Here is the new code for that.
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.ObjectInputStream;
import java.io.FileOutputStream;
import java.io.BufferedReader;
public class Program13Shaban
{
	public static void main(String[] args)
	{
		Scanner keyboard = new Scanner(System.in);
		System.out.println("\tI will read boy names and girl names");
	   Scanner inputStreamBoys = null;
	   Scanner inputStreamGirls = null;
	   try
	   {
		   inputStreamBoys =
			  new Scanner(new FileInputStream("boynames.txt"));
		   inputStreamGirls =
				 new Scanner(new FileInputStream("girlnames.txt"));
	   }
	   catch(FileNotFoundException e)
	   {
		   System.out.println("\tFile boysnames.txt or girlsnames.txt was not found");
		   System.out.println("\tor could not be opened.");
		   System.exit(0);
	   }
	   String boyNames [] = new String [1000];
	   String girlNames [] = new String [1000];
	   for(int count = 0; count < 1000; count++)
	   {
		   boyNames[count] = inputStreamBoys.nextLine( );
	   }
	   for(int count = 0; count < 1000; count++)
	   {
		   girlNames[count] = inputStreamGirls.nextLine( );
	   }

	   System.out.print("\tWhat is the name you want me to search for: ");
	   String searchKey = keyboard.next();
	   int foundIndex = -1;  // -1 indicates not found (yet)
	   int counter = 0;
	   while(counter < 1000 && foundIndex == -1)
	   {
			   if(searchKey.equals(boyNames[counter]))
			   {
				   foundIndex = counter;  //stash location of the match into foundIndex
			   }
			   else
			   counter++;
	   }
	   // postcondition:  either we have found the searchKey value and its location
	   // is in foundIndex, or we didn't find the value and foundIndex = -1.
	   if(foundIndex == -1)
			   System.out.println("\n\tsearchKey value of " + searchKey + " not found in any array.");
	   else
			   System.out.println("\n\tsearchKey value of " + searchKey
				   + " was found at position " + foundIndex);
	   inputStreamBoys.close( );
	   inputStreamGirls.close( );
	}
}

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#15 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Linear search of an array with strings

Posted 05 December 2008 - 09:24 PM

Please do not duplicate post!

This is a duplicate of Reading files from Txt into an Array using inputStream started last night.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2