The Artima Developer Community
Sponsored Link

Java Answers Forum
Array Program - Please Help

9 replies on 1 page. Most recent reply: Mar 9, 2015 7:32 AM by George B

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 9 replies on 1 page
Carol

Posts: 6
Nickname: char
Registered: May, 2003

Array Program - Please Help Posted: May 1, 2003 1:12 PM
Reply to this message Reply
Advertisement
I am trying to write an array program that contains tree parallel arrays. The first array holds student ID numbers, the second holds first names and the third hold the student's GPA. I have to use dialog boxes to accept a student ID number with nine digits and then display the student's first name and GPA. If a match is not found then display "No Match..."

Can someone please help me? I have tried to start writing this program but as you will see below I have no idea what I am doing.

public class StudentIDArray
{
public static void main(String [] args)
{
int [] studentID = {123456789, 234567890, 345789012, 557461522, 611147010};
string [] firstName = {Mary, Bob, Sue, Alice};
float [] GPA = 3.5, 3.8, 2.7, 3.0};
for(int x = 0; x < 4; ++x)
{
if(firstname==studentID[x])
validStudentID = true;
else
System.out.println("No Match....");
}
}


Carol

Posts: 6
Nickname: char
Registered: May, 2003

Re: Array Program - Please Help Posted: May 1, 2003 7:59 PM
Reply to this message Reply
Figured it out.......thanks anyway

Carol

Posts: 6
Nickname: char
Registered: May, 2003

Re: Array Program - Please Help Posted: May 2, 2003 2:29 PM
Reply to this message Reply
Hi:

I thought I had this figured out, but when I run it I get two input dialog boxes instead of one for for just the input. The others are supposed to display the info if found and if not found it is supposed to display no match. Please help!

import javax.swing.*;

public class students
{
static int MAX = 5;
static int [] studentID = {123556780,234052468,345222678,456789874,567025878};
static String [] studentFirstName = {"Carol","Joe","Imad","Alaa","Fred"};
static double [] studentGPA = {4.5,3,2.3,2,6.6};

public static void main (String [] args)
{
String input;
boolean found;
input = JOptionPane.showInputDialog("Please input student ID (0 - exit)");
int idToSearch = Integer.parseInt(input);
while (idToSearch != 0) {
found = false;
for (int i=0;i<MAX;i++) {
if (studentID == idToSearch) {
JOptionPane.showInputDialog("First Name: " + studentFirstName + " GPA: " + studentGPA);
found = true;
}
}
if (!found)
{
JOptionPane.showInputDialog("No Match..");
}
input = JOptionPane.showInputDialog("Please input student ID (0 - exit)");
idToSearch = Integer.parseInt(input);
}
}
}

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Array Program - Please Help Posted: May 2, 2003 5:08 PM
Reply to this message Reply
I don't think this code will even compile, will it? "if( studentID == idToSearch )" is comparing an array to an int. You probably really want "if( studentID[i] == idToSearch )". Likewise for studentFirstName and studentGPA when you try to display the match.

Other things that may cause problems:
- You should do a try/catch on the Integer.paseInt(), because the string may not represent a valid integer.
- You probably don't really want to use JOptionPane.showInputDialog() for showing all your UI, but I suppose you are just trying to get things working now and will do the refinements subsequently.
- I don't know if this is for an assignment (which will have a followup with better design), but this parallel array stuff is not a good plan; it would be better to have a single array of "StudentInfo" objects. I suspect that this is an assigment and a subseqent one will have you do just that, to demonstrate how much better things are when you use a more object-oriented approach.
- You probably want to do the EXIT_ON_CLOSE thing so that the app closes down nicely.
- Finally, when you post code to the forum, use the [java] tags (see the gray Formatting Your Post box on the right when you are composing your post) and it will be much more nice looking.

Carol

Posts: 6
Nickname: char
Registered: May, 2003

Re: Array Program - Please Help Posted: May 3, 2003 5:38 AM
Reply to this message Reply
Thanks for the help. This code does compile however I followed your instructions, but could you briefly explain two of the comments in a little more detail as I am not familiar with those concepts yet.

1. You should do a try/catch on the Integer.paseInt(),
because the string may not represent a valid integer.
2. EXIT_ON_CLOSE.

Thanks again.

Carol

Posts: 6
Nickname: char
Registered: May, 2003

Re: Array Program - Please Help Posted: May 3, 2003 1:12 PM
Reply to this message Reply
Can someone give me an explanation on this.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Array Program - Please Help Posted: May 3, 2003 4:35 PM
Reply to this message Reply
When you call parseInt() on a String, it is possible that the string cannot be converted to an integer (like parseInt("cheese"), for example). In that case it will throw a NumberFormatException. If you don't handle this case, your program will just crash with the unhandled exception. Here is a general outline of how you would handle it:
try
{
   idToSearch = Integer.parseInt(input);
}
catch( NumberFormatException nfe )
{
   // Notify the user and try again.
}


Actually, having looked a little closer, you don't need EXIT_ON_CLOSE, because you don't even have a main JFrame (if you enhance your app to work from a main window, you'll want to use frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); so that it exits properly when the main window coses).

In your case, all you really need to do is this:
System.exit(exitCode);
at the end of your main() method, so that the app quits properly when it is finished. exitCode can be whatever integer value you want your program to return, or just 0 if you don't care.

Carol

Posts: 6
Nickname: char
Registered: May, 2003

Re: Array Program - Please Help Posted: May 3, 2003 6:26 PM
Reply to this message Reply
Matt:

This worked perfectly. Thank you very much!!!!!

Juergen Kunzmann

Posts: 1
Nickname: nospaces
Registered: Mar, 2015

Re: Array Program - Please Help Posted: Mar 8, 2015 3:02 PM
Reply to this message Reply
Okay I am having a related issue. The code works, but for only the first four members of the parallel array. Can anybody take a look?

import java.util.*;
import javax.swing.JOptionPane;


public class StudentIDArray {

static String[] studentNum = new String[]
{"1234", "2345", "3456", "4567", "5678", "6789", "7890", "8901", "9012", "0123"};

static String[] studentName = new String[]
{"Peter", "Brian", "Stewie", "Lois", "Chris", "Meg", "Glen", "Joe", "Cleveland", "Morty"};

static double[] studentGpa = new double[]
{2.0, 3.25, 4.0, 3.6, 2.26, 3.20, 3.45, 3.8, 3.0, 3.33};

public static void main(String[] args){


String studentId = null;
while ((studentId = JOptionPane.showInputDialog(null, "Please enter your Student ID number to view your name and GPA")) != null)
{
boolean correct = false;
for (int x = 0; x < studentId.length(); ++x)
{
if(studentId.equals(studentNum[x]))
{
JOptionPane.showMessageDialog(null, "Your name is: " + studentName[x] + "\n"
+ "Your GPA: " + studentGpa[x], "GPA Results", JOptionPane.INFORMATION_MESSAGE);
correct = true;
break;
}
}
if(! correct)
{
JOptionPane.showMessageDialog(null, "Student ID not found, try again.", "Not found", JOptionPane.INFORMATION_MESSAGE);
}
}
}
}

George B

Posts: 24
Nickname: hmgeorge
Registered: Feb, 2008

Re: Array Program - Please Help Posted: Mar 9, 2015 7:32 AM
Reply to this message Reply
This line:

for (int x = 0; x < studentId.length(); ++x)

should be:

for (int x = 0; x < studentNum.length; ++x)

You want to iterate through the number of items in the array, not the number of characters in the ID.

Flat View: This topic has 9 replies on 1 page
Topic: Book Previous Topic   Next Topic Topic: Calculating tax java, need HELP

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use