# JAVAThis should be easyI have this data.txt:4 40 8052 41 72 6171 60 50 52 6177 41 61 70 79 4167 60 50 61 76and this code: // THIS IS THE ONE YOU ARE LOOKING FOREimport 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:3000I need it to be:3476more explanation:reads integers from a file and puts them into categories 40-50,50-60,..etc to 80each count[] should have the value of however many integers fit within these categoriespossible problemsI think the problem is my for loop section.thank you!

Question
46 views

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;

// 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!

check_circle

star
star
star
star
star
1 Rating
Step 1
• Create a java program and a main class “Zzz” and create a main method to read “data.txt” file.
• And inside main method declares all required variables and array to solve the given problem.
• Read first three data from file very firstly and then and the basis of them crea...

### Want to see the full answer?

See Solution

#### Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in