Results 1 to 12 of 12
  1. #1
    Liam2013 is offline Member
    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0

    Post Assigning int value to a string input

    Hello, i`m fairly new to programming and was wondering if somebody could help me and possibly provide me with an example.
    I am looking to take user input via JOptionPane for the name of a team, this team would have 50 points. Then again input taken for a 2nd team, this team would have 30pts. When both inputs are given I would like the output to be given with the team with the most points and the points difference. If possible up to ten teams can be assigned with points. I have tried many different ways to achieve this but have yet to do so, if this is a silly question forgive me, like I said I am relatively new to this and may be blind to this!!

    Thanks in advance!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Assigning int value to a string input

    You're posting your assignment but not asking a specific question. I'm sure that you are not asking someone to do this for you, but to help explain what you don't understand or what mistakes you may be making. So in that light, why don't you show us your code attempts, wrapped in [code] [/code] tags, and ask any questions about parts you don't understand, and we'll be much better able to help you.

  3. #3
    Liam2013 is offline Member
    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0

    Default Re: Assigning int value to a string input

    Here is some of the code i`ve done so far... I can get the first teams score and output it but how to progress to then taking the 2nd team inputted and then doing the math is where I cant seem get my head at!

    Java Code:
    import javax.swing.JOptionPane;
    public class Team {
        String Team_Name;
        int points;
    public static void main(String[] args) {
            Team aTeam = new Team();
            aTeam.Team_Name = JOptionPane.showInputDialog(null, "Enter Home Team:");
            if ("Man Utd".equals(aTeam.Team_Name)){
                int points = 48;
                JOptionPane.showMessageDialog ( 
    null, "Their current score is " + points, "to date", 
    JOptionPane.PLAIN_MESSAGE);
            }
            if ("Chelsea".equals(aTeam.Team_Name)){
                int points = 53;
                JOptionPane.showMessageDialog ( 
    null, "Their current score is " + points, "to date", 
    JOptionPane.PLAIN_MESSAGE);
            }
            if ("Man City".equals(aTeam.Team_Name)){
                int points = 43;
                JOptionPane.showMessageDialog ( 
    null, "Their current score is " + points, "to date", 
    JOptionPane.PLAIN_MESSAGE);
            }
    Last edited by Fubarable; 10-27-2012 at 09:27 PM. Reason: code tags added

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Assigning int value to a string input

    You seem to be "hard-coding" your team names by using "Chelsea", "Man City", "Man Utd", and I suggest that you not do that, but instead have your program work for any and all team names. Key for your assignment is the Team class. Is this a class you've created yourself? Do you have code for it?

    Also, you didn't add code tags around your posted code, so I edited your post and added them for you. Please if you post any more code, use the tags so we can read the code. I have a link below on how to do this.

  5. #5
    Liam2013 is offline Member
    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0

    Default Re: Assigning int value to a string input

    I`m posting all of the code i`ve written so far, i`m trying another route to get where I want and seem to be making some progress but could do with some pointers and possibly som help code please!!


    Java Code:
    import java.util.ArrayList;
    import java.util.Iterator;
    import javax.swing.JOptionPane;
    
        public class Team{ 
     
    
        public static void main( String [] args )  {  
    
       Premier bs = new Premier( );  
        String homeTeam = JOptionPane.showInputDialog( null,  "Enter a Team" );    
        String awayTeam = JOptionPane.showInputDialog( null,  "Enter a Team" );
        ArrayList<Teams> results = bs.searchForteam( homeTeam ); 
        ArrayList<Teams> results1 = bs.searchForteam( awayTeam );
        Iterator<Teams> it = results.iterator(); it.hasNext();
        Iterator<Teams> its = results1.iterator(); it.hasNext();
                Teams tempLeague = it.next();
                Teams tempLeague1 = its.next();
              int  homeScore = tempLeague.getPoints( );
              int awayScore = tempLeague1.getPoints( );
              int finalScore = homeScore - awayScore;
              JOptionPane.showMessageDialog(null, finalScore);}  
    }
    Java Code:
    import java.util.ArrayList; 
        public class Premier {
        private ArrayList<Teams> league;  
        public Premier( )  {  
        league = new ArrayList<>( );  
        league.add( new Teams( "Manchester Utd", 48 ) );  
        league.add( new Teams( "Manchester", 46 ) );  
        league.add( new Teams( "Liverpool", 40 ) );  
        league.add( new Teams( "Newcastle", 42 ) );  
        league.add( new Teams( "Arsenal", 44 ) );  
        league.add( new Teams( "Tottenham", 45 ) );  
        league.trimToSize( );  }  
    
        @Override
        public String toString( )  {  
        String Teams = "";  
    
        for( Teams tempLeague : league )  {  
        Teams += tempLeague.toString( ) + "\n";  }  
        return Teams;  }
        
    
        public ArrayList<Teams> searchForteam( String searchString )  {  
    
       ArrayList<Teams> searchResult = new ArrayList<>( );  
    
        for ( Teams currentTeams : league )  {  
    
        if ( ( currentTeams.getTeam( ) ).indexOf( searchString ) != -1 ) {
               searchResult.add( currentTeams ); }
       }  
        searchResult.trimToSize( );  
    
        return searchResult;  }  
    }
    Java Code:
    public class Teams {
        private String team;    
        private int points;  
    
       
        public Teams( )  {  
        team = "";  
        points  = 0;  }  
    
        public Teams( String newTeam, int newPoints )  {  
        team = newTeam;  
        points  = newPoints;  }  
    
        public String getTeam( )  {  
        return team;  }  
    
        public int getPoints( )  {  
        return points;  }  
    
        @Override
      public String toString( )  {  
    
       return ( "team: " + team + "\t"  + "points: " + points );  }  
    }

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: Assigning int value to a string input

    What problems are you having?

    Well, apart from Liverpool apparently being bottom.
    Yes, yes, yes, real life blah blah blah...I'm sorry, but some of us like living in the 80s.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    Liam2013 is offline Member
    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0

    Default Re: Assigning int value to a string input

    Not to worry, Liverpool won`t be there for long! ;)
    The problem i`m having is that I can only get the value of the home team to work and I want to be able to get the home teams value, the away teams value and then to display the difference....

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: Assigning int value to a string input

    OK.
    Your indenting needs sorting out as it's dead difficult to follow the flow in that code.

    But it looks to me like you have the home and away team values.
    I don't see the immediate problem.

    What are you seeing in the output that you aren't expecting to see?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    Liam2013 is offline Member
    Join Date
    Oct 2012
    Posts
    5
    Rep Power
    0

    Default Re: Assigning int value to a string input

    Quote Originally Posted by Tolls View Post
    OK.
    Your indenting needs sorting out as it's dead difficult to follow the flow in that code.

    But it looks to me like you have the home and away team values.
    I don't see the immediate problem.

    What are you seeing in the output that you aren't expecting to see?

    This is the error that displays when I try to compile...

    Exception in thread "main" java.util.NoSuchElementException
    at java.util.ArrayList$Itr.next(ArrayList.java:794)
    at Bets.Team.main(Team.java:19)

    Like I said, I can retrieve and display a value for the home team but I cannot retrieve and display the values of both the home and away team, which will then allow me to perform calculations based on this...

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: Assigning int value to a string input

    I would suggest printing out the values at each step in your code, so you can get an idea of what's going on.
    This includes the search values being used, and what is being found in the search loop.

    System.out.println() is the first place to go for debugging. Or use a debugger if you have one and step throught the code.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Assigning int value to a string input

    Why is the class called Teams when it holds information about a single team?
    Why does the searchForTeam method return an ArrayList (which can hold many teams) when all it needs to do is find a single team?
    The logic you use in the searchForTeam method will find multiple teams. eg if user enters Man it will find "Manchester Utd" and "Manchester". Once again the method should only find a single team.
    You call the hasNext method on the Iterator, totally ingore the return value and call the next method regardless. If the ArrayList has no item you get the exception you are seeing. However if you fix your searchForTeam method as suggested above all this will be avoided.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: Assigning int value to a string input

    I suspect that would simply result in a NullPointerException.
    Something is preventing the away team being selected.
    If the general search worked then there would be something in the ArrayList.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Assigning ranking numbers to String.format
    By Dawesome in forum New To Java
    Replies: 8
    Last Post: 05-20-2012, 03:45 PM
  2. User string input?
    By synister1337 in forum New To Java
    Replies: 10
    Last Post: 01-31-2012, 10:01 PM
  3. need help in string to input
    By eyeore in forum New To Java
    Replies: 4
    Last Post: 01-11-2010, 11:07 AM
  4. How to Mask input string
    By nmc.091 in forum New To Java
    Replies: 1
    Last Post: 02-23-2009, 03:49 AM
  5. Assigning a string value to a char
    By coffeebean in forum New To Java
    Replies: 4
    Last Post: 06-15-2008, 06:30 AM

Posting Permissions

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