-1
import java.util.Scanner;
import java.text.SimpleDateFormat;
import java.util.Date;

public class salary
{
public static void main(String[] args) 
{
int n,sum=0,average=0,count1=0,count2=0;
System.out.println("How many people salary you want to enter" );
Scanner in = new Scanner(System.in);
n = in.nextInt();
String[] salary=new String[n];
int[] sal=new int[n];
for (int i=0;i<n;i++)
{
System.out.println("please enter salary with $ sign as prefix" );
salary[i]=in.nextLine();
}
for (int j=0;j<n;j++)
{
salary[j] = salary[j].replaceAll("[$]+", "0");
System.out.println(salary[j]);
sal[j]= Integer.parseInt(salary[j]);
//salary[j] = Integer.parseInt(salary[j].replaceAll("[$]+", " ")); 
sum=sum+sal[j];
}
average=sum/n;
for(int q=0;q<n;q++)
{
if (sal[q]>average)
{count1=count1+1;
}
else if (sal[q]<average)
{
count2=count2+1;
}}
System.out.println("The average is  "+average);
System.out.println("The salary greater than average  "+count1);
System.out.println("The salary less than average  "+count2);
}
}

getting error and also for loop is not working properly

Exception in thread "main" java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:592)
    at java.lang.Integer.parseInt(Integer.java:615)
    at salary.main(salary.java:34)

read a list of salary amounts that start with a dollar sign “$” and followed by a nonnegative number, save the valid salary inputs into an array and sort the salary in ascending order, calculate the average of the salary inputs, and count the number of inputs less than /greater than the average.

Cœur
  • 32,421
  • 21
  • 173
  • 232
  • 1
    If you *indent* your code to make it readable, I'll have a look. I won't look at it, the way it is right now!!! But, just out of curiosity, which part of the error message saying that a blank string is not a number is confusing you? – Andreas Sep 11 '16 at 19:55
  • why asking for trouble requiring to enter $ as a prefix when you could do a `nextInt()` like above? – Jean-François Fabre Sep 11 '16 at 19:57
  • 1
    Please read [What is a debugger and how can it help me diagnose problems?](http://stackoverflow.com/q/25385173/5221149) – Andreas Sep 11 '16 at 19:58
  • @Andreas Copy into Eclipse, Ctrl-A then Ctrl-Shift-F . Eazy-peezy. – nicomp Sep 11 '16 at 20:32
  • @nicomp I shouldn't have to copy/paste into an editor and apply formatting in order to read the question. OP should format the code to be readable, so we nice helpers, spending our free time, don't waste it on stupid stuff like that!! – Andreas Sep 11 '16 at 22:30
  • @Andreas It's no problem for me, it's actually quicker than telling the OP to do it. – nicomp Sep 12 '16 at 00:16
  • @nicomp It's even quicker to simply skip the question, and let OP fix it, so OP will learn and make the next question better. – Andreas Sep 12 '16 at 00:40
  • @Andreas but neither of us did that. – nicomp Sep 12 '16 at 13:53

1 Answers1

0

Change Line 16 to in.next:

for (int i = 0; i < n; i++) {
    System.out.println("please enter salary with $ sign as prefix");
    salary[i] = in.next();
}
nicomp
  • 3,775
  • 4
  • 19
  • 43