JAVA This should be easy I have this data.txt: 4 40 80 52 41 72 61 71 60 50 52 61 77 41 61 70 79 41 67 60 50 61 76 and this code: // THIS IS THE ONE YOU ARE LOOKING FORE import java.io.File;import java.io.FileNotFoundException;import java.util.Scanner; //https://www.chegg.com/homework-help/questions-and-answers/program-1-histogram-takes-integer-n-two-integers-left-right-uses-stddraw-plot-histogram-co-q30777519?trackid=e3045eb02494&strackid=cc33c4210bce&ii=1// D:/documents/TESU/000_courses/COS-111 Intro to Programming (-OL009)/eclipse-workspace/COS_111/src/data.txt public class zzz{ public static void main(String[] args) throws FileNotFoundException{ File file = new File("D:\\documents\\TESU\\000_courses\\COS-111 Intro to Programming (-OL009)\\eclipse-workspace\\COS_111\\src\\data.txt");Scanner scanner = new Scanner(file);// get the first 3 conditions// n = categoriseint n = scanner.nextInt();int left = scanner.nextInt();int right = scanner.nextInt();// find catigories// count storse number of values in each categor7int[] counts = new int[n]; // what are the categoriesdouble intervalWidth = (double)(right-left) / n;// find the bounds// get left most value// for each category// increment leftdouble[] bounds = new double [n+1];for (int i=0; i<= n;i++) {bounds[i]=left+intervalWidth*i;} // while has next int // depending on n// check if there is another integer// if integerwhile(scanner.hasNextInt()){// store that int as tempint temp = scanner.nextInt(); // check the bounds against tempfor (int i = 0;i<=bounds.length;i++) {// if temp is within bounds add to that countif (bounds[i]<temp && temp < bounds[i+1]) {// found out where temp belongs and add it to it's countcounts[i]++;}break;}}for (int i = 0; i < counts.length ; i++) {System.out.println(counts[i]);} }} my output is: 3000 I need it to be: 3476 more explanation: reads integers from a file and puts them into categories 40-50,50-60,..etc to 80 each count[] should have the value of however many integers fit within these categories possible problems I think the problem is my for loop section. thank you!
JAVA
This should be easy
I have this data.txt:
4 40 80
52 41 72 61
71 60 50 52 61
77 41 61 70 79 41
67 60 50 61 76
and this code:
// THIS IS THE ONE YOU ARE LOOKING FORE
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
//https://www.chegg.com/homework-help/questions-and-answers/program-1-histogram-takes-integer-n-two-integers-left-right-uses-stddraw-plot-histogram-co-q30777519?trackid=e3045eb02494&strackid=cc33c4210bce&ii=1
// D:/documents/TESU/000_courses/COS-111 Intro to Programming (-OL009)/eclipse-workspace/COS_111/src/data.txt
public class zzz
{
public static void main(String[] args) throws FileNotFoundException
{
File file = new File("D:\\documents\\TESU\\000_courses\\COS-111 Intro to Programming (-OL009)\\eclipse-workspace\\COS_111\\src\\data.txt");
Scanner scanner = new Scanner(file);
// get the first 3 conditions
// n = categorise
int n = scanner.nextInt();
int left = scanner.nextInt();
int right = scanner.nextInt();
// find catigories
// count storse number of values in each categor7
int[] counts = new int[n];
// what are the categories
double intervalWidth = (double)(right-left) / n;
// find the bounds
// get left most value
// for each category
// increment left
double[] bounds = new double [n+1];
for (int i=0; i<= n;i++) {
bounds[i]=left+intervalWidth*i;
}
// while has next int
// depending on n
// check if there is another integer
// if integer
while(scanner.hasNextInt())
{
// store that int as temp
int temp = scanner.nextInt();
// check the bounds against temp
for (int i = 0;i<=bounds.length;i++) {
// if temp is within bounds add to that count
if (bounds[i]<temp && temp < bounds[i+1]) {
// found out where temp belongs and add it to it's count
counts[i]++;}
break;
}
}
for (int i = 0; i < counts.length ; i++) {
System.out.println(counts[i]);
}
}
}
my output is:
3
0
0
0
I need it to be:
3
4
7
6
more explanation:
reads integers from a file and puts them into categories 40-50,50-60,..etc to 80
each count[] should have the value of however many integers fit within these categories
possible problems
I think the problem is my for loop section.
thank you!
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images