Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Mar 2013
    Posts
    55
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Question A program that searches for two strings on one line

    I am trying to write a program that'll search for my friends first name and my friends last name, within a TXT document, and tell me if it's found(and maybe what line it's on). I have different names within the TXT document that all have middle names. So I need to search for her first name, and when found, I need to check if her last name is on the same line.

    This is the code that I'm have trouble with...

    Code:
    import java.io.*;
    import java.util.*;
    import java.util.logging.Logger;
    
    public class Search{
    	
    	private static final Logger logger = Logger.getLogger(Search.class.getName());
    	public static void main(String[]args) throws Exception {
    		String fname = "Jonah";
    		String lname = "Smith";
    		String s1 = null;
    		String s2 = null;
    		String s3 = null;
    		int count = 0;
    		File f = new File("C:\\Users\\MrA\\Desktop\\Friends.txt");
    		Scanner input = new Scanner(f);
    		while(input.hasNextLine()){
    			String line = input.nextLine();
    			System.out.println(line);
    			String[] parts = line.split(" ");
    			s1 = parts[0];
    			s2 = parts[1];
    			System.out.println(s1);
    			System.out.println(s2);
    			if (s1 == "Jonah") {
    			    System.out.println("hi");
    			    }
    			if (s1 == fname && s3 == lname) {
    				System.out.println("hi");
    				}
    			if (s2== fname && s3 == lname) {
    				System.out.println("hi");
    				}
    			}
    		}
    }

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Equivalency doesn't work in Java like that with strings. Strings are objects in java; compare them using the .equals function. Using obj1 == obj2 only works if the objects share the same pointer, and since strings in java are immutable that will rarely occur.

    You did mention a middle name though. If that is the in structure of first middle last, than the last must come out of parts[2]. Extracting to s{x} is optional of course (but if you have a variable length would make the most sense), but it looks to me that you'll want to rely on the parts.length to tell you if a middle name exists. If its 3, than you have the first, middle and last, so you know to index out to the parts[2]. If its 2, than its parts[1]. Assuming the structure that is first middle last.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    MrAtoBandMe (08-25-2013)

  • #3
    New Coder
    Join Date
    Mar 2013
    Posts
    55
    Thanks
    27
    Thanked 0 Times in 0 Posts
    Alright, now I'm getting the Array Out Of Bounds...

    I know why, it hits the first person that doesn't have a middle name, the parts[2] is empty...

    I tried adding this in, but it doesn't help...

    Code:
    if (parts[2].isEmpty()) {
    	s2 = "Nothing";
    	} else {
    		s2 = parts[2];
    	         }

  • #4
    New Coder
    Join Date
    Mar 2013
    Posts
    55
    Thanks
    27
    Thanked 0 Times in 0 Posts
    Here's the whole code now, without that little bit from above...

    Code:
    import java.io.*;
    import java.util.*;
    import java.util.logging.Logger;
    
    public class Search{
    	
    	private static final Logger logger = Logger.getLogger(Search.class.getName());
    	public static void main(String[]args) throws Exception {
    		String fname = "Jonah";
    		String lname = "Smith";
    		boolean FirstFound = false;
    		boolean LastFound = false;
    		boolean emptyer = false;
    		String s0 = null;
    		String s1 = null;
    		String s2 = null;
    		int count = 0;
    		File f = new File("C:\\Users\\MrA\\Desktop\\Folk.txt");
    		Scanner input = new Scanner(f);
    		while(input.hasNextLine()){
    			FirstFound = false;
    			LastFound = false;
    			String line = input.nextLine();
    			System.out.println(line);
    			String[] parts = line.split(" ");
    			s0 = parts[0];
    			s1 = parts[1];
    			s2 = parts[2];
    			System.out.println(s0);
    			System.out.println(s1);
    			System.out.println(s2);
    			count = count + 1;
    			if (s0.equals("Jonah") || s1.equals("Jonah") || s2.equals("Jonah")) {
    			    FirstFound = true;
    			    }
    			if (s0.equals("Smith") || s1.equals("Smith") || s2.equals("Smith")) {
    				LastFound = true;
    				}
    			if (FirstFound == true && LastFound == true) {
    				System.out.println("Found on line " + count + ".");
    			    }
    			}
    		}
    }

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    You have to make use of the parts.length to determine if you have a middle name to set or not. The length -1 will be the last entry regardless of if a middle name exists.
    These comparisons aren't right. This lets you use the first name and last name in any position in the line. You should probably be only checking the first entry in the line for the first name and the last entry in the line for the last. A simple && check between them is all that's required, you don't need to waste memory on additional variables.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #6
    New Coder
    Join Date
    Mar 2013
    Posts
    55
    Thanks
    27
    Thanked 0 Times in 0 Posts
    This right here gives me an out of bounds error because the parts[2] doesn't exist due to the line only having a first and last name, rather than first middle last...

    Code:
    import java.io.*;
    import java.util.*;
    import java.util.logging.Logger;
    
    public class Search{
    	
    	private static final Logger logger = Logger.getLogger(Search.class.getName());
    	public static void main(String[]args) throws Exception {
    		File f = new File("C:\\Users\\MrA\\Desktop\\Folk.txt");
    		Scanner input = new Scanner(f);
    		while(input.hasNextLine()){
    			String line = input.nextLine();
    			System.out.println(line);
    			String[] parts = line.split(" ");
    			
    			if (parts[0].length() > 0) {
    				System.out.println(parts[0]);	
    				}
    			if (parts[1].length() > 0) {
    				System.out.println(parts[1]);	
    				}
    			if (parts[2].length() > 0) {
    				System.out.println(parts[2]);	
    				}
    		}
    	}
    }
    The only other way I see this being possible, is the count the number of " "(blank spaces on the line) and then using parts[2] if the count is 2(which is how spaces will be there if it is First Middle Last, rather than just 1 space when there is First Last).

    Is there a better way?

  • #7
    New Coder
    Join Date
    Mar 2013
    Posts
    55
    Thanks
    27
    Thanked 0 Times in 0 Posts
    This is what is inside the TXT file...

    Code:
    John P. Gordon
    Ronald Trump
    Jonah Miles
    Lisa Anderson
    Jonah Lynn Smith
    Sally Stetson
    Peter Laurence Pan
    Jenny Lou Smith
    Katty R. Licker

  • #8
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    Quote Originally Posted by MrAtoBandMe View Post
    This right here gives me an out of bounds error because the parts[2] doesn't exist due to the line only having a first and last name, rather than first middle last...

    Code:
    import java.io.*;
    import java.util.*;
    import java.util.logging.Logger;
    
    public class Search{
    	
    	private static final Logger logger = Logger.getLogger(Search.class.getName());
    	public static void main(String[]args) throws Exception {
    		File f = new File("C:\\Users\\MrA\\Desktop\\Folk.txt");
    		Scanner input = new Scanner(f);
    		while(input.hasNextLine()){
    			String line = input.nextLine();
    			System.out.println(line);
    			String[] parts = line.split(" ");
    			
    			if (parts[0].length() > 0) {
    				System.out.println(parts[0]);	
    				}
    			if (parts[1].length() > 0) {
    				System.out.println(parts[1]);	
    				}
    			if (parts[2].length() > 0) {
    				System.out.println(parts[2]);	
    				}		}
    	}
    }
    The only other way I see this being possible, is the count the number of " "(blank spaces on the line) and then using parts[2] if the count is 2(which is how spaces will be there if it is First Middle Last, rather than just 1 space when there is First Last).

    Is there a better way?
    The red part is still not correct. What Fou-Lu meant was, check the length of the array, not the length of the array elements.

    So:

    if (parts.length > 2) {
    // a middle name is present, act upon it
    } else {
    // no middle name present, only check for first and last
    }
    I am the luckiest man in the world

  • Users who have thanked Roelf for this post:

    MrAtoBandMe (08-26-2013)

  • #9
    New Coder
    Join Date
    Mar 2013
    Posts
    55
    Thanks
    27
    Thanked 0 Times in 0 Posts
    So parts.length is for the actual number of peices in the array? I never knew that. Thank you.

    I wrote the program to just count the number of blank spaces and then determine what to do after that. I could rewrite it, but it's not a big deal to me.

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Yep that's right. When you use .length on an array, it tells you the number of elements within that array. Length on a String is the number of chars. Technically a String is little more than a char[], so you can decay it into one and pull the .length instead of .length() if you *really* wanted too (I personally say that's a waste :P).
    I would suggest a different delimiter though. Space is fine for your current names, but what about names like Dick Van Dyke? Using spaces you cannot separate the last name from the middle name. I'd suggest either comma or semi-colon delmited instead. Everything would be the same except for the character used to delimit and the use of the .split().
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •