Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: problem searching for a String element in a parallel array

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default problem searching for a String element in a parallel array

    Hi guys

    I have a parallel array of 5 team names and 5 scores

    The user enters a team to search for and it is stored in the variable team

    this is my code

    String team;
    int loopcount;
     
    System.out.println("Please enter the team you would like to search for: ");
    team = input.next(); // identify team to be searched for
     
     
    for(loopcount = 0; loopcount < 5; loopcount++)
    {
         if (teaminfo[0][loopcount].equals(team) ) { 
              System.out.println("Search has found : " + teaminfo[0][loopcount] + " at position: " + (loopcount+1));
         } 
    }

    However when for some reason the for loop does not seem to be functioning. Have a made a glaring obvious newbie error?
    Last edited by Freaky Chris; July 27th, 2010 at 11:09 AM.


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: problem searching for a String element in a parallel array

    Firstly, Welcome to Java Programming Forums I hope you find all the help you need here and more!

    Been a while since I actually practiced Java lol, But i cannot see any flaws in what you have done, perhaps it's to do with the declaration & initiation of the variable 'teaminfo'


    Of course you should be aware that input.next() will read only the next word input for example, if the input was "liverpool fc" only "liverpool" would be read. This might be the problem? In which case I suggest nextLine()


    Please use [hightlight=java]// You Code here[/highlight] tags when posting Java code.

    Thanks,
    Chris

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: problem searching for a String element in a parallel array

    Quote Originally Posted by david185000 View Post
    However when for some reason the for loop does not seem to be functioning.
    What do you mean not functioning? Does it compile? What behavior do you expect to get? How is your array structured? More info the better.

  4. #4
    Junior Member
    Join Date
    Jul 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem searching for a String element in a parallel array

    Hi guys thanks for your replies and the welcome!
    Ive included my code for the entire program in case there are any obvious flaws, but I really am perplexed at why this loop isnt working. Ill press run, and the program will run so i enter my teams and there score. I get to enter team to search for and that is input but then nothing except this error message in the console window

    Exception in thread "main" java.lang.NullPointerException
    at teams.searchteam(teams.java:82)
    at teams.main(teams.java:94)


    //
     
    import java.util.*;
    /*
     * Filename: teams.java
     * created by: David Pepper
     * Created on: 27 Jul 2010
     * Updated on: 27 Jul 2010
     * comment:
     */
     
    public class teams
    {
    	private static Scanner input = new Scanner(System.in);
    	static String teaminfo[][] = new String[5][5];
     
     
     
     
    	public static String team4()
    {
    		String value4;
    		String ireland = new String ("ireland");
     
    		System.out.println("enter ireland as the fourth team: ");
    		value4 = input.next();
     
    		if(value4.equals(ireland))
    		{
    			return null;
    		}
     
    		else 
    		{
    		System.out.println("you did not enter ireland as team4, please do this now:");
    		team4();
    		}
     
    		return null;				
     
     
    	}
     
    	public static void addteaminfo()
    {
    		String teaminfo[][] = new String[5][5];
     
    		for (int i = 0; i < teaminfo.length; i++)    // enter team names ensuring that ireland is team 4
    		{
    			if (i != 3){
    			System.out.println("Please enter team "+ (i+1) + ":");
    			teaminfo[0][i]= input.next();
    			}
    			else team4();
    			teaminfo[0][3] = "ireland";
    		}
     
     
    		for (int i = 0; i < teaminfo.length; i++)     // enter team scores
    		{
    			System.out.println("Please enter " +  teaminfo[0][i] + "'s score");
    			teaminfo[1][i]= input.next();
    		}	
     
     
     
    }
     
    	public static  void searchteam()
    {
    			String team;
    			int loopcount;
     
    			System.out.println("Please enter the team you would like to search for: ");
    			team = input.next();            // identify team to be searched for
     
     
     
     
     
    			for(loopcount = 0; loopcount < 5; loopcount++)
    			{
     
    				if (teaminfo[0][loopcount].equals(team) )  // If search comes across desired value print out location
    				{ 
    					System.out.println("Search has found : " + teaminfo[0][loopcount] + " at position: " + (loopcount+1));
    				}	
     
    			}
     
    }
     
    	public static void main(String[] args)
    	{
    		addteaminfo();
    		searchteam();
    	}
     
     
    }
    Last edited by copeg; July 27th, 2010 at 11:59 AM. Reason: java highlighting

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: problem searching for a String element in a parallel array

    The error should point you to the exact line of code where the problem is. Here's the train of thought: something is null, go to line 82 and behold there is the variable teamInfo. Looking around, there is no code which actually creates the value of the teamInfo (look closely at the addTeamInfo function and the scope of the variables you assign).

  6. #6
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: problem searching for a String element in a parallel array

    Thanks to scoping laws you killed yourself lol. You declared teaminfo as a global variable, correct. But then when you assign all the teams to it you have declared it as a local variable. So the local version masks the global variable. So when the end of addteaminfo is reach the local one goes out of scope with all the data and then when you call searchteam() you are checking the team against a variable which hasn't had values assignment to it, thus giving you a null pointer exception

    Chris

  7. #7
    Junior Member
    Join Date
    Jul 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem searching for a String element in a parallel array

    Quote Originally Posted by copeg View Post
    The error should point you to the exact line of code where the problem is. Here's the train of thought: something is null, go to line 82 and behold there is the variable teamInfo. Looking around, there is no code which actually creates the value of the teamInfo (look closely at the addTeamInfo function and the scope of the variables you assign).
    I see. So Ive assigned information to the array in addTeamInfo()

    How do I make that information accessible from the main method?

    Im sure this is easy enough. However Im using java for the first time in 5 years for a project to get back into uni and there are a few basics that are eluding me

  8. #8
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: problem searching for a String element in a parallel array

    The clue to the solution is in the Scoping laws & masking. It's an easy fix.

    Chris

  9. #9
    Junior Member
    Join Date
    Jul 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: problem searching for a String element in a parallel array

    Quote Originally Posted by Freaky Chris View Post
    The clue to the solution is in the Scoping laws & masking. It's an easy fix.

    Chris
    Im afraid I dont have a clue what that means


  10. #10
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: problem searching for a String element in a parallel array

    If you wish to return to Uni with a working knowledge of Java then you need to find Google

    Here is a simplified version of what you are doing.

    public class Example {
         private int myInt;
         public static void main(String[] args){
              myInt = 5;
              System.out.println(myInt);
              changeMyInt();
              System.out.println(myInt);
              changeMyInt2();
              System.out.println(myInt);
         }
         private static void changeMyInt(){
              int myInt = 22;
         }
         private static void changeMyInt2(){
             myInt = 33;
         }
    }

    Chris

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: problem searching for a String element in a parallel array

    Cross posted at problem searching for a String element in a paralell array - Java Forums

Similar Threads

  1. problem for writing to parallel port LPT1 on windows by RXTX library
    By sahar_m in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: April 20th, 2010, 04:31 AM
  2. Add white spices before the String element
    By bookface in forum Java Theory & Questions
    Replies: 1
    Last Post: March 23rd, 2010, 08:50 PM
  3. Searching and printing string results from an Arraylist. Having difficulty.
    By Espressoul in forum Loops & Control Statements
    Replies: 1
    Last Post: February 25th, 2010, 08:32 PM
  4. searching a string
    By dvsumosize in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 23rd, 2010, 01:31 AM
  5. array/string problem
    By RSYR in forum Collections and Generics
    Replies: 1
    Last Post: December 18th, 2009, 10:24 PM