Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: I don't know where my problem is help please. :D

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I don't know where my problem is help please. :D

    Here's the problem I was assigned to do: Given 2 lists of student names and grades write a program to write the name and grade for a student in the 1st list. The arrays will be of length 12. If the student's name is not in the array, state "The name is not in the list".

    Here is my code:
    import java.io.*;
     
    public class StudentsandGrades{
       public static void main(String [] args) throws IOException{
     
       String input;
       String namein;
       int count = 0;
       int i;
       String [] name = new String [12];
       int [] grade = new int [12];
       BufferedReader myIn = new BufferedReader(new InputStreamReader(System.in));
     
          for(i = 0; i < 12; i++){
          System.out.print("Insert name of student " + (i + 1) + " : ");
          name [i] = myIn.readLine();
          System.out.print("Insert grade of student " + (i + 1) + " : ");
          input = myIn.readLine();
          grade [i] = Integer.parseInt(input);
       }
     
          System.out.print("Enter the name of the student you would like to search for: ");
          namein = myIn.readLine();
     
          for(i = 0; i < 12; i++){
          if(namein == name [i]){
          count++;
          break;
    }
    }
     
          if(count != 0)
          System.out.println("Student: " + name[i] + "Grade: " + grade[i]);
          if(count == 0)
          System.out.println("The name is not in the list");
      }
    }
    For some reason it always says "The name is not in the list" whether it is there or not.
    Can you guys help me out?
    Last edited by helloworld922; November 20th, 2010 at 10:20 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: I don't know where my problem is help please. :D

    See the following page: == or equals()

  3. The Following User Says Thank You to copeg For This Useful Post:

    javapenguin (November 23rd, 2010)

  4. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I don't know where my problem is help please. :D

    Thank you so much! It works perfectly now!

    I guess as you could probably guess I'm kind of new to this, but thank you very much for your help. I really appreciate it.