### Thread: If(thereis(string, array))--how to do ?

1. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Mar 2013
Location
Kiev, Ukrain
Posts
89
Rep Power
6

#### If(thereis(string, array))--how to do ?

i have a task. for it's solution i need to chek the presence of the String in String[] array. how can i do that ?
simple : array[index].equals(str); doesn't work .
2. Write a loop that compares each element in the array against the desired String.

array[index].equals(str); doesn't work .
Can you explain what "doesn't work" means?
Last edited by NormR; September 1st, 2013 at 05:16 PM.
3. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Mar 2013
Location
Kiev, Ukrain
Posts
89
Rep Power
6
I wrote a loop, in which i wrote array[index].equals(str); -- but here was an error (nullpointerException). but array was diclared correct. Look :
this is my code
Code:
```import java.io.*;
import java.util.Scanner;

static String[] names,marks,recc, records;
static int[] finmarks;

public static void main(String[] args)throws IOException{

Scanner scan = new Scanner(System.in);
records= new String[10];

int k=0;

records[0]= "1001-40";
records[1]= "1002-50";
records[2]= "1003-60";
records[3]= "1002-80";
records[4]= "1005-35";
records[5]= "1005-55";
records[6]= "1007-68";
records[7]= "1009-99";
records[8]= "1009-10";
records[9]= "1004-89";

recDiv(records);
recSort(names,marks);
for(int i= 0; i<= 9; i++){
System.out.println(recc[i]);
}
System.out.println(names.length);
System.out.println(marks.length);
}

public static void recDiv(String[] str){

names= new String[str.length];
marks= new String[str.length];

String buf="";
int i, j;
boolean b1=false;

for(i= 0; i< str.length; i++){
for(j= 0; j< str[i].length(); j++){
if(str[i].charAt(j) == '-'){
buf="";
for(int k= 0; k< j; k++){
buf+=str[i].charAt(k);
names[i]=buf;
}
buf="";
for(int k= j+1 ; k< str[i].length(); k++){
buf+=str[i].charAt(k);
marks[i]=buf;
}
}
}

}

}

public static void recSort(String[] names, String[] marks){

recc= new String[names.length];
finmarks= new int[names.length];
String bufstr="";
boolean here= false;
int maxmark= 0, k=0;

for(int i= 0; i< names.length; i++){
for(int j= 0; j< marks.length; j++){
if(names[i].equals(names[j])){
if( Integer.parseInt(marks[i])> Integer.parseInt(marks[j]) ){
recc[k]= names[i]+'-'+marks[i];
k++;
}
if( Integer.parseInt(marks[i])< Integer.parseInt(marks[j]) ){
recc[k]= names[i]+'-'+marks[j];
k++;
}
}
}
}
}

}```
for solution of this task :
Write a program that accepts 10 student records (roll number and score) and prints them in decreasing order of scores.
In case there are multiple records pertaining to the same student, the program should choose a single record containing the highest score.
The program should be capable of accepting a multi-line input. Each subsequent line of input will contain a student record, that is, a roll number and a score (separated by a hyphen). The output should consist of the combination of roll number and corresponding score in decreasing order of score.

I divided array with student records on 2 arrays "marks" and "names" . Then i whant to sort them. I'm checking the marks of students wth the same numbers. and pasting a new record with the biger mark. But i need to check the presence of the same record in a new array . and here i have problems . this is a new version of last function. without checking of the final array .
Last edited by FAQer; September 2nd, 2013 at 02:11 AM.
4. here was an error (nullpointerException)
Please copy the full text of the error message and post it here.

What statement gets the error?
5. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Mar 2013
Location
Kiev, Ukrain
Posts
89
Rep Power
6
There is no error already, thanks . I tried another way of the solution , but this way still doesn't solved the problem. Anyway i'm still trying .