1. Engineering
  2. Computer Science
  3. trying to figure out how to correct the error i...

Question: trying to figure out how to correct the error i...

Question details

Trying to figure out how to correct the error I am getting. The code is posted below. Thanks
expected output: Bar graph for 23 actual output: Number of values to graph Bar graph for 1 23 input used: 3 1 2 3 expected output:

import java.util.Scanner;

public class BarGraph2 {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

System.out.println("Number of values to graph: ");

int n = in.nextInt();

int data[] = new int[n];

int max = 0;

for(int i=0; i

data[i] = in.nextInt();

if(max < data[i]) {

max = data[i];

}

}

System.out.print("Bar graph for ");

for(int i=0; i

System.out.print(data[i]+ " ");

}

System.out.println(":");

for(int height = max; height > 0; height--) {

for(int i=0; i

if(data[i] >= height) {

System.out.printf("%3s", "#");

} else {

System.out.printf("%3s", " ");

}

}

System.out.println();

}

for(int i=0; i

System.out.printf("---");

}

System.out.println();

for(int i=0; i

System.out.printf("%3d", data[i]);

}

System.out.println();

}

}

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution