Empty Array

Check for an empty array, show error message.

Page 1 of 1

2 Replies - 738 Views - Last Post: 04 December 2009 - 08:14 PM Rate Topic: -----

#1 skittlebud  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 22-April 09

Empty Array

Post icon  Posted 04 December 2009 - 07:58 PM

I am making a program that reads test scores.

What I want to do.
Make an error message show if the input is left blank.
If 0 is entered, I want an error message to show also.

How do I do this? This is my code. It gives me no errors, but it also does not do what I want it to, which is show an error message for incorrect input.


//If the user enters 0, an error message shows
//If the input is blank an error message shows.

for (int i = 0; i < array.length; i++)
{
if (array[i] <1 && array[i] > -1 )
{
input = JOptionPane.showInputDialog("Enter a valid number.");
}
}

Is This A Good Question/Topic? 0
  • +

Replies To: Empty Array

#2 SpeedisaVirus  Icon User is offline

  • Baller
  • member icon

Reputation: 114
  • View blog
  • Posts: 855
  • Joined: 06-October 08

Re: Empty Array

Posted 04 December 2009 - 08:13 PM

Uh, well...a start would be to have a line of code that outputs an error message. Put effort into this and come back. I think its safe to say you haven't put any in yet.
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10576
  • View blog
  • Posts: 39,152
  • Joined: 27-December 08

Re: Empty Array

Posted 04 December 2009 - 08:14 PM

To test to see if the input is blank, use the following comparison: input == null. Now I'm assuming your array is an int[], not a String. And because Strings aren't primitives, when you want to test their values, you have to use the .equals(String) method instead of the == operator. So I would do something like:
String input = "":
for(int i = 0; i < array.length; i++){
   input = JOptionPane.showInputDialog("Enter a non-zero number");
   while(input == null || input.equals("0"))  //while no val was entered or 0 was entered
		  input = JOptionPane.showInputDialog("Enter a non-zero number");  //get input
   array[i] = Integer.parseInt(input); //assuming array is an int[] 
}



Also, please :code:
Thanks for helping us help you! :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1