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: Need to add an error, and return the correct values in a dialog box

  1. #1
    Junior Member
    Join Date
    Jul 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need to add an error, and return the correct values in a dialog box

    Hi!
    Sorry in advance if this is really long or a bad question, I am new to Java and object-oriented programming and easily confused.

    My class very briefly in the first assignment like.... 4 weeks ago touched on the JOptionPane, but I can't find anything I need from the book or from the first assignment except the basics, so I was hoping someone could help!

    This is the assignment:

    "Write an application containing three parallel arrays that hold 10 elements each. The first array hold four-digit student ID numbers, the second array holds first names, and the third array holds the students’ grade point averages. Use dialog boxes to accept a student ID number and display the student’s first name and grade point average. If a match is not found, display an error message that includes the invalid ID number and allow the user to search for a new ID number."

    I am stuck on the error message, mainly: I have not worked in JOptionPane more than once, and what you'll see in my code is basically all I've ever done.
    Furthermore, what I have doesn't return the correct values: the dialog box returns the correct text, but the wrong format and a bunch of weird symbols instead of the corresponding FirstName and GPA ("First name: [Ljava.lang.String;@57e1b0cGPA:[D@4232c52b").

    This is my code so far:

    import javax.swing.JOptionPane;

    public class Assignment1 {
    public static void main(String[]args) {

    int [] StudentID = {1234, 5678, 9101, 1213, 1415, 1617, 1819, 2021, 2223, 2425};
    String[] FirstName = {"Bob", "Joe", "Jane", "John", "Roman", "Billy", "Casy", "Kylie", "Raimy", "Abby"};
    double [] GPA = {3.4, 3.6, 2.1, 1.2, 4.0, 3.7, 1.0, 3.7, 3.9, 3.4};

    JOptionPane.showInputDialog(null, "Enter student ID");



    JOptionPane.showMessageDialog(null, "First name: " + FirstName + "GPA: " + GPA);
    }

    }




    I've attempted if....else, and a do....while, but I'm really confused by how to check if the user-input number = the StudentID

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need to add an error, and return the correct values in a dialog box

    how to check if the user-input number = the StudentID
    Use the == operator in an if statement to do that.
    Use a loop to iterate through all the values in the StudentID array.
    Access the elements in the array using array notation: StudentID[i] refers to the element at index i

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jan 2024
    Posts
    75
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Need to add an error, and return the correct values in a dialog box

    To achieve your goal, you need to search for the entered student ID in the `StudentID` array and then display the corresponding first name and GPA if a match is found. Otherwise, you should show an error message. Here's the modified version of your code to accomplish this:

    ```java
    import javax.swing.JOptionPane;

    public class Assignment1 {
    public static void main(String[] args) {
    int[] StudentID = {1234, 5678, 9101, 1213, 1415, 1617, 1819, 2021, 2223, 2425};
    String[] FirstName = {"Bob", "Joe", "Jane", "John", "Roman", "Billy", "Casy", "Kylie", "Raimy", "Abby"};
    double[] GPA = {3.4, 3.6, 2.1, 1.2, 4.0, 3.7, 1.0, 3.7, 3.9, 3.4};

    // Prompt user to enter student ID
    String input = JOptionPane.showInputDialog(null, "Enter student ID");

    // Convert the input to an integer
    int searchID = Integer.parseInt(input);

    // Initialize variables to hold found data
    String foundFirstName = null;
    double foundGPA = 0.0;
    boolean found = false;

    // Search for the entered student ID in the array
    for (int i = 0; i < StudentID.length; i++) {
    if (searchID == StudentID[i]) {
    foundFirstName = FirstName[i];
    foundGPA = GPA[i];
    found = true;
    break;
    }
    }

    // If a match is found, display the student's details
    if (found) {
    JOptionPane.showMessageDialog(null, "First name: " + foundFirstName + "\nGPA: " + foundGPA);
    } else {
    // If no match is found, display an error message
    JOptionPane.showMessageDialog(null, "Error: Student ID " + searchID + " not found.");
    }
    }
    }
    ```

    This code prompts the user to enter a student ID, searches for that ID in the `StudentID` array, and then displays the corresponding first name and GPA if found. If the ID is not found, it shows an error message.

    If no match is found, display an error message. Remember, seeking assistance with Java assignments can provide valuable insights and guidance to overcome hurdles. Additionally, exploring various online resources and seeking help from experienced programmers can further enhance your understanding and proficiency in Java programming. Understanding these concepts deeply can assist you in tackling similar challenges in the future. There are various online platforms, such as programminghomeworkhelp.com, where you can find help with Java assignment.

Similar Threads

  1. This code is not giving correct values
    By Metamorphoseon in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 24th, 2014, 09:30 PM
  2. can i return two values from a return method?
    By tonu in forum Object Oriented Programming
    Replies: 4
    Last Post: January 1st, 2014, 11:02 AM
  3. Return the rgb values from a jpg image
    By newparticipant in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 25th, 2011, 03:47 PM
  4. Replies: 1
    Last Post: March 22nd, 2010, 04:34 PM
  5. Replies: 0
    Last Post: February 2nd, 2010, 08:20 AM

Tags for this Thread