Results 1 to 19 of 19
Thread: extract strings from an array
- 10-01-2010, 12:05 AM #1
Member
- Join Date
- Sep 2010
- Posts
- 32
- Rep Power
- 0
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.
- 10-01-2010, 02:00 AM #2
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
- 10-01-2010, 02:22 AM #3
Member
- Join Date
- Sep 2010
- Posts
- 32
- Rep Power
- 0
the program is supposed to get 3 names and scores and outputs the highest score with the name.
- 10-01-2010, 02:31 AM #4
Have you tried debugging the code yet?
have you looked at the formatting of your code?
Do the {}s line up ok?
- 10-01-2010, 02:45 AM #5
Cross posted at extract strings from an array - Java
- 10-01-2010, 02:49 AM #6
Member
- Join Date
- Sep 2010
- Posts
- 32
- Rep Power
- 0
yes my {} matches.
the program outputs the correct highest score, but the wrong name.
- 10-01-2010, 02:53 AM #7
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?
- 10-01-2010, 03:04 AM #8
Member
- Join Date
- Sep 2010
- Posts
- 32
- Rep Power
- 0
ahh i forgot {} for the if statements...
thanks!!
- 10-01-2010, 04:58 AM #9
Member
- Join Date
- Sep 2010
- Posts
- 32
- Rep Power
- 0
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.
this outputs everything. i've tried another methods but it'll output null and 0.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]); } } }
- 10-01-2010, 02:00 PM #10
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.
- 10-01-2010, 05:31 PM #11
Member
- Join Date
- Sep 2010
- Posts
- 32
- Rep Power
- 0
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 56Last edited by plasticfood; 10-01-2010 at 05:36 PM.
- 10-01-2010, 05:58 PM #12
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
- 10-01-2010, 06:46 PM #13
Member
- Join Date
- Sep 2010
- Posts
- 32
- Rep Power
- 0
how do i extract the highest from the first for loop and use it in the second without creating a new method?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]); } } }
- 10-01-2010, 07:08 PM #14
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.
- 10-02-2010, 01:12 AM #15
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
- 10-02-2010, 02:28 AM #16
Member
- Join Date
- Sep 2010
- Posts
- 32
- Rep Power
- 0
- 10-02-2010, 02:54 AM #17
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.
- 10-02-2010, 03:14 AM #18
Member
- Join Date
- Sep 2010
- Posts
- 32
- Rep Power
- 0
by second loop, do you mean the narray and sarray? sorry, but i don't really understand what you're really saying.
- 10-02-2010, 01:15 PM #19
Similar Threads
-
Strings as array help please..
By crazygurl in forum New To JavaReplies: 1Last Post: 12-01-2009, 08:23 PM -
2 dimensional array with strings.
By dbashby in forum New To JavaReplies: 12Last Post: 10-13-2009, 10:52 PM -
Need help with counting strings in an array.
By dalonehunter in forum New To JavaReplies: 1Last Post: 10-02-2009, 08:47 AM -
storing strings into an array
By anthonym2121 in forum New To JavaReplies: 2Last Post: 04-04-2009, 07:32 AM -
Sorting an array of Strings
By Java Tip in forum java.langReplies: 0Last Post: 04-15-2008, 07:39 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks