Results 1 to 19 of 19
  1. #1
    plasticfood is offline Member
    Join Date
    Sep 2010
    Posts
    32
    Rep Power
    0

    Default extract strings from an array

    Java Code:
    import java.util.Scanner;
    public class Highest{
    	public static void main (String [] args){
    	
    	Scanner kb = new Scanner(System.in);
    	
    	int scores [] = new int[3];
    	String names [] = new String [3];
    	int highest = scores [0];
    	String names1 = names [0];
    	
    
    	
    	for (int i = 0; i<3; i++){
    		System.out.println("enter name and score: ");
    		names[i] = kb.next();
    		scores[i] = kb.nextInt();
    		if (scores[i] > highest)
    			highest = scores[i];
    			names1 = names[i];
    
    		}
    		
    		
    	
    	
    	System.out.println(highest + names1);
    	
    						
    				
    	
    	
    		
    		
    		
    		
    		
    		
    	}
    }
    ---------------------------------------------------------



    i'm stuck on the part where i have to extract the name that associates with the highest score. it always prints out the last name that i enter instead of the one with the highest score.
    Last edited by plasticfood; 10-01-2010 at 02:21 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,565
    Rep Power
    25

    Default

    What is the code you posted supposed to do?

    Try debugging your code by adding print outs of the values of the variables as they are changed and to show the execution flow. The output should show you where your logic is bad.

    Please edit your code and add code tags to preserve formating. Info here: Java Forums - BB Code List

  3. #3
    plasticfood is offline Member
    Join Date
    Sep 2010
    Posts
    32
    Rep Power
    0

    Default

    the program is supposed to get 3 names and scores and outputs the highest score with the name.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,565
    Rep Power
    25

    Default

    Have you tried debugging the code yet?

    have you looked at the formatting of your code?
    Do the {}s line up ok?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,565
    Rep Power
    25

  6. #6
    plasticfood is offline Member
    Join Date
    Sep 2010
    Posts
    32
    Rep Power
    0

    Default

    yes my {} matches.

    the program outputs the correct highest score, but the wrong name.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,565
    Rep Power
    25

    Default

    Look at the code and see why the last name is always being stored in the names1 variable but the score is correct?
    Have you tried debugging the code yet by adding print outs?

  8. #8
    plasticfood is offline Member
    Join Date
    Sep 2010
    Posts
    32
    Rep Power
    0

    Default

    ahh i forgot {} for the if statements...
    thanks!!

  9. #9
    plasticfood is offline Member
    Join Date
    Sep 2010
    Posts
    32
    Rep Power
    0

    Default

    i am trying to modify that program, so if the user enters two of the same highest scores then it'll output two scores and two names.

    Java Code:
    import java.util.Scanner;
    public class Highest{
    	public static void main (String [] args){
    	
    	Scanner kb = new Scanner(System.in);
    	
    	int scores [] = new int[3];
    	String names [] = new String [3];
    	int highest = scores [0];
    	String names1 = names [0];
    	String narray [] = new String [3];
    	int sarray [] = new int [3];
    
    	
    	for (int i = 0; i<3; i++){
    		System.out.println("enter name and score: ");
    		names[i] = kb.next();
    		scores[i] = kb.nextInt();
    		if (scores[i] >= highest){
    			sarray [i] = scores [i];
    			narray [i] = names[i];	}
    			}			
    				
    		
    	
    	
    	
    	
    	for (int i = 0; i<3; i++){
    			System.out.println(sarray [i] + narray [i]);
    
    	}
    						
    				
    	
    	
    		
    		
    		
    		
    		
    		
    	}
    }
    this outputs everything. i've tried another methods but it'll output null and 0.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,565
    Rep Power
    25

    Default

    Show the input and output.
    Also please add comments to the code describing what the logic is supposed to do.

    You need separate index for the second array. Don't use the loop control index.

  11. #11
    plasticfood is offline Member
    Join Date
    Sep 2010
    Posts
    32
    Rep Power
    0

    Default

    ok i really need a hint on the logic first b/c i can't even figure it out on paper how am i supposed to put more than 1 highest score and name in an array.

    do i check if the score is equal to or higher than first in the if statements? either way, i'm going to have the score at scores[0] to be inputted in an array. or the highest score is always going to be equal to score[i].


    the input is the same, the user types 3 scores and names. the program will print out the highest score(s) and name(s) if there are more than 1 of the same highest score.

    input:
    house 56
    foreman 56
    chase 12

    output

    house 56
    foreman 56
    Last edited by plasticfood; 10-01-2010 at 05:36 PM.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,565
    Rep Power
    25

    Default

    You need a separate index for the array where you are saving duplicates/ties.
    Then you need logic to consider:
    Is there a new high? If so clear all saved ties
    Is there a tie for high? If so save the tie in the second array

  13. #13
    plasticfood is offline Member
    Join Date
    Sep 2010
    Posts
    32
    Rep Power
    0

    Default

    Java Code:
    import java.util.Scanner;
    public class Highest{
    	public static void main (String [] args){
    	
    	int scores [] = new int[3];
    	String names [] = new String [3];
    	int highest = scores [0];
    	String names1 = names [0];
    	String narray [] = new String [3];
    	int sarray [] = new int [3];
    	
    	Scanner kb = new Scanner(System.in);
    	
     
    	for (int i = 0; i<3; i++){
    		System.out.println("enter name and score: ");
    		names[i] = kb.next();
    		scores[i] = kb.nextInt();
    			if (scores[i] > highest){
    			highest = scores[i];
    			names1 = names[i];
    		
    			}
    	}
    
    		
    	for (int i = 0; i<3; i++){
    		if (scores[i] == highest){
    			sarray [i] = scores [i];
    			narray [i] = names[i];	
    		}
    	}			
    				
    
    
    	
    	
    	System.out.println(names1 + " "+ scores);
    	for (int i = 0; i<3; i++){
    			System.out.println(sarray [i] + narray [i]);
    
    	}
    						
    				
    	
    	
    		
    		
    		
    }		
    		
    		
    	
    }
    how do i extract the highest from the first for loop and use it in the second without creating a new method?

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,565
    Rep Power
    25

    Default

    Describe in detail, step by step, what the second loop is supposed to do.
    Include the use of the i index. What is the sarray supposed to hold?

    Question: How can the same index be used in two arrays? Aren't elements separate?

    Let me repeat for the third time:
    You need a separate index for the second array. Don't use the loop control index.

  15. #15
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Quote Originally Posted by plasticfood View Post
    Java Code:
    import java.util.Scanner;
    public class Highest{
    	public static void main (String [] args){
    	
    	int scores [] = new int[3];
    	String names [] = new String [3];
    	[COLOR="DarkOrange"]int highest = scores [0];[/COLOR]
    	String names1 = names [0];
    	String narray [] = new String [3];
    	int sarray [] = new int [3];
    	
    	Scanner kb = new Scanner(System.in);
    	
     
    	for (int i = 0; i<3; i++){
    		System.out.println("enter name and score: ");
    		names[i] = kb.next();
    		scores[i] = kb.nextInt();
    			if (scores[i] > highest){
    			highest = scores[i];
    			names1 = names[i];
    		
    			}
    	}
    
    		
    	for (int i = 0; i<3; i++){
    		if (scores[i] == highest){
    			sarray [i] = scores [i];
    			narray [i] = names[i];	
    		}
    	}			
    				
    
    
    	
    	
    	System.out.println(names1 + " "+ scores);
    	for (int i = 0; i<3; i++){
    			System.out.println(sarray [i] + narray [i]);
    
    	}
    						
    				
    	
    	
    		
    		
    		
    }		
    		
    		
    	
    }
    how do i extract the highest from the first for loop and use it in the second without creating a new method?
    Code in orange looks like a problem, why?

  16. #16
    plasticfood is offline Member
    Join Date
    Sep 2010
    Posts
    32
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Describe in detail, step by step, what the second loop is supposed to do.
    Include the use of the i index. What is the sarray supposed to hold?

    Question: How can the same index be used in two arrays? Aren't elements separate?

    Let me repeat for the third time:
    You need a separate index for the second array. Don't use the loop control index.
    you mean like a nested for loop?

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,565
    Rep Power
    25

    Default

    You don't need a separate loop to index thru the second array.
    To extract and save data from one array to another, use a for loop to index thru the array to be searched. Use a manually set and incremented index for the second array where you are saving the extracted data from the first array.

  18. #18
    plasticfood is offline Member
    Join Date
    Sep 2010
    Posts
    32
    Rep Power
    0

    Default

    by second loop, do you mean the narray and sarray? sorry, but i don't really understand what you're really saying.

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,565
    Rep Power
    25

    Default

    by second loop, do you mean the narray and sarray
    No an array is not a loop.

    you mean like a nested for loop?
    You don't need a separate/nested loop to index thru the second array.

Similar Threads

  1. Strings as array help please..
    By crazygurl in forum New To Java
    Replies: 1
    Last Post: 12-01-2009, 08:23 PM
  2. 2 dimensional array with strings.
    By dbashby in forum New To Java
    Replies: 12
    Last Post: 10-13-2009, 10:52 PM
  3. Need help with counting strings in an array.
    By dalonehunter in forum New To Java
    Replies: 1
    Last Post: 10-02-2009, 08:47 AM
  4. storing strings into an array
    By anthonym2121 in forum New To Java
    Replies: 2
    Last Post: 04-04-2009, 07:32 AM
  5. Sorting an array of Strings
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 07:39 PM

Posting Permissions

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