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
Asked Oct 29, 2019
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;

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

check_circle

Expert Answer

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

Check out a sample Q&A here.

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

Engineering

Computer Engineering

Programing

Related Computer Engineering Q&A

Find answers to questions asked by student like you
Show more Q&A
add
question_answer

Q: How would I solve this problem in python language Grades a) Write a program that reads in the names ...

A: Program Instructions:Use the open() function to read from the file.Save the result o spilt() functio...

question_answer

Q: how do I Write a String constant consisting of exactly 5 exclamation marks. in python language

A: String Constants: The string constant is nothing but the sequences of characters that is used as dat...

question_answer

Q: Use the Problem-Solving Procedure to analyze the following problem. In a posting, express in your ow...

A: Pseudocode to take inputs of the two integers by the user and then calculating and displaying the qu...

question_answer

Q: Fix these codes in order to get the following output: Code: public class Course { private String cou...

A: Revised code is as follows:import java.util.Scanner; //importing packages.// declaring classclass Co...

question_answer

Q: this question is from fundamentals of logic. related to boolean algebra. I am unable to decipher the...

A: Click to see the answer

question_answer

Q: I'm not understanding this question. This is a Python course. This is the problem. "One acre of land...

A: This question asked about to convert the value of tract land in the square feet. To solve this quest...

question_answer

Q: As SOA service vendors are likely to be consolidated over time, an IT utility will emerge. In that s...

A: Success factors:The success factors of service enterprises depending on IT utility are as follows:Bu...

question_answer

Q: Hello, I am trying to write a program: Write a .py file like MainFiles.py In that .py, create a func...

A: A python file is created named as MainFiles.py:Def main( ):Print(“ Welcome to python”)The above pyth...

question_answer

Q: What is the difference between the COUNT aggregate function and the SUM aggregatefunction?

A: In database management, an aggregate function is a feature where various row values are grouped toge...