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();
}
}
Linear search of an array with stringsDuplicate posts topics merged
25 Replies - 10509 Views - Last Post: 05 December 2008 - 11:19 PM
#1
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.
Replies To: Linear search of an array with strings
#2
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
Scanner sin = new Scanner(new File("blablabla.txt"));
and use the scanner nextLine() method
#3
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
#4
Re: Linear search of an array with strings
Posted 04 December 2008 - 11:13 PM
Use an ArrayList.
#5
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.
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.
#6
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
#7
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.
#8
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?
#9
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.
#10
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?
#11
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.
#12
Re: Linear search of an array with strings
Posted 05 December 2008 - 09:02 PM
Just zip them and attach them to your post.
#13
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
#14
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)
-
girlnames.txt (12.09K)
Number of downloads: 67 -
boynames.txt (11.88K)
Number of downloads: 57
#15
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.
This is a duplicate of Reading files from Txt into an Array using inputStream started last night.
|
|

New Topic/Question
Reply



MultiQuote




|