1

I am making a simple sports simulator. In the beginning you must enter both teams playing in the format (CityName TeamName e.g. Chicago Bulls). I am using two methods to get the name of the city and the name of the team from that initial name so I can call them in separate places for a more natural looking output.

public String getLocation(String teamName){
    String location = this.name.substring(0, this.name.indexOf(" "));
    return location;
}

public String getFirstName(String teamName){
    String name = this.name.substring(this.name.indexOf(" "));
    return name;
}

The first method for getting the location works fine. The second one for getting the team name returns null every time. I can't figure out what I've done different in the two as to why I get different results.

EDIT:

Here is the line that is implementing these methods:

    System.out.println("The " + TeamX.getFirtName(TeamX.getName()) + " are visting from " + TeamX.getLocation(TeamX.getName()) + " to play the " + TeamY.getFirtName(TeamY.getName()) + " in " + TeamY.getLocation(TeamY.getName()) );

And the output for it when "NYC Jets" and "Arizona Cardinals" has been:

The null are visting from NYC to play the null in Arizona
5
  • What's teamName and how does it relate to this.name? Also, it seems highly unlike that you'll be getting null from either method; do you by any chance mean an empty string ("")? Commented May 1, 2016 at 21:42
  • I noticed that two, but I figured since he said it was working on the first one I'd just answer his actual question. Commented May 1, 2016 at 21:44
  • he just didnt specify two parameters as explained the solution i posted Commented May 1, 2016 at 21:45
  • @NPE I've updated my question to answer that. I definitely am receiving null from it but you are right it doesn't look like it should be returning it. Commented May 1, 2016 at 21:49
  • Why are you passing the String teamName to the methods, but then doing name.substring(this.name)? Should the method operate on the parameter? Commented May 1, 2016 at 22:05

5 Answers 5

2

I believe the issue is because in the function you are calling substring on this.name and not the local variable teamName:

public String getLocation(String teamName){
   return teamName.substring(0, teamName.indexOf(" "));
}

public String getFirstName(String teamName){
    return teamName.substring(teamName.indexOf(" "));
}
Sign up to request clarification or add additional context in comments.

5 Comments

its because he orignially had string as null.. and tried to substring it... thus he shouldset string name = team name then return the name
@DarkV1 He shouldn't even use local variables, since there is no need for them. And he should fix the starting index.
@Tom Ima laugh if this.name is a static or instance variable .... and its the variable he inputs as teamName
@DarkV1 It doesn't matter what this.name is. It is obviously an issue in OPs code and the local variable name in your answer is also not needed. Thus, there is no need to initialize it.
@Tom name is actually an instance variable for the NFL team class. A method called setName changes the value to the user input before the code in question is called. So it should not be null unless I am misunderstanding something
2

First, your function parameters are not used. And then, you do not show how these functions are called. The 'name' seems to be an attribute that might be changing in your main class instructions. By the way, "String name = this.name.sub..." is not good looking even it works fine.

Please try split function :

public String getLocation(String teamName){
    return teamName.split(" ")[0];
}

public String getFirstName(){
    return teamName.split(" ")[1];
}

Comments

2

Is there always a space between? Because then you could use good ol' split

fullname.split(' ');

Which will return a 2 element array of strings with the first one being a city and the second one being the team name

Oh yeah. After re reading the question I am starting to wonder if the OP knows how to use classes properly... It seems weird to pass in an attribute of a class to itself...

The team should be initialized with the full team name which is saved as an instance variable and then simple call teamx.getFirstName() which will internally use the class variable... Same with getLocation

6 Comments

That is a different approach, but the author still hasnt clarified if there will be a space in the team name always :/
@DarkV1 Sorry. Yes it is intended to only be entered in that format with one space in between.
@DarkV1 I assumed from his current use of indexOf(' '). But you know what they say about assumptions...
Lol. I answered this with JavaScript. But turns out it works anyways! :)
Except for without the semi colon and it returns an array of strings lol
|
0

Will there always be a space between team names? and also... You only specified the first index? not the last...

public String getFirstName(String teamName){
    String name = teamName;
    name =name.substring(name.indexOf(" "),name.length());
    // you for got the second parameter for the substring Thats why it returned null as you only found " ".

    return name;
}

I reedited- it...

The problem is that String is originally null... if you want to find the index or values within string you should initialize it before you call substring on it.

String name = teamName;

4 Comments

Oh i see the problem.
@andrewxt it doesnt work because name is orginally null
@sbowde4 its because string is originally null.
So how would u find the substring of a null variable?
0

I didn't realize on my earlier post that substring could handle only one parameter, as I've only used it with two. Like other people have stated, name is a null variable and needs to first be set equal to teamName

    String name = teamName;
    name = this.name.substring(this.name.indexOf(" "))

3 Comments

I was under the impression that if there was only one parameter then it would start at that point and go to the end. Unfortunately this fix did not stop it from returning null.
You still need two parameters, that is how substring works. I believe it also may because you never set name = teamName
@andrewxt "I was under the impression that if there was only one parameter then it would start at that point and go to the end." you are correct. This answer is wrong. (it works like OPs current code, no fix here)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.