Question

In: Computer Science

Write a Java program called Numbers that reads a file containing a list of numbers and...

Write a Java program called Numbers that reads a file containing a list of numbers and outputs, for each number in the list, the next bigger number. For example, if the list is

78, 22, 56, 99, 12, 14, 17, 15, 1, 144, 37, 23, 47, 88, 3, 19

the output should look like the following:

78: 88

22: 23

56: 78

99: 144

12: 14

14: 15

17: 19

15: 17

1: 3

144: 2147483647

37: 47

23: 37

47: 56

88: 99

3: 12

19: 22

NOTE: If there is no bigger number in the sequence, just display the value of Integer.MAX_VALUE .

The output should be shown on the screen and also saved in a file.

Program Design

  • You should have a single class called Numbers.
  • You should have a static method called nextLargest, which takes as input an array of integers called list and another single integer called value. It should return the number that is the next larger than the value in the list (as described above). You need to use this method to implement your program.
  • The input file from which the list is read should be called "list.txt". Assume it's in the current directory.
  • The output file to which the results should be saved should be called "nextlist.txt". Save it in the current directory (i.e. don't specify the path).

Additional Requirements

  1. The name of your Java Class that contains the main method should be Numbers. All your code should be within a single file.
  2. Your code should follow good coding practices, including good use of whitespace (indents and line breaks) and use of both inline and block comments.
  3. You need to use meaningful identifier names that conform to standard Java naming conventions.
  4. At the top of the file, you need to put in a block comment with the following information: your name, date, course name, semester, and assignment name.
  5. If the input file contains something not in the proper format, your program should handle this gracefully by displaying an appropriate message to the user and closing the program (without letting it crash).

The output of your program should exactly match the sample program output given at the end

Solutions

Expert Solution


/*
 *  Java Program to read integers from a text file and write next largest number of integers to new file
 */

import java.io.*;
import java.util.*;

class Main {

  public static int nextLargest(int list[], int val) {
    int next;
      next = Integer.MAX_VALUE;
      for (int i = 1; i < list.length; i++) {
        if (val < list[i]) {
          next = list[i];
          break;
        }
      }
    return next;
  }

  public static void main(String[] args) throws Exception {

    List<Integer> list = new ArrayList<Integer>();

    File file = new File("list.txt");
    FileWriter fWrite = new FileWriter("nextlist.txt");
    Scanner sc = new Scanner(file);

    while (sc.hasNextInt())
      list.add(sc.nextInt());
    
    int intList[] = new int[list.size()];

    for (int i = 0; i < list.size(); i++) {
      intList[i] = list.get(i);
    }

    for (int i = 0; i < list.size(); i++) {
      System.out.println(list.get(i) + ": " + nextLargest(intList, list.get(i)));
      fWrite.write(list.get(i) + ": " + nextLargest(intList, list.get(i)) + "\n");
    }
    fWrite.close();
  }
}

Note: Drop comments for queries.


Related Solutions

Write a program that reads a file called document.txt which is a text file containing an...
Write a program that reads a file called document.txt which is a text file containing an excerpt from a novel. Your program should print out every word in the file that contains a capital letter on a new line to the stdout. For example: assuming document.txt contains the text C++
Write a program in Java that reads a file containing data about the changing popularity of...
Write a program in Java that reads a file containing data about the changing popularity of various baby names over time and displays the data about a particular name. Each line of the file stores a name followed by integers representing the name’s popularity in each decade: 1900, 1910, 1920, and so on. The rankings range from 1 (most popular) to 1000 (least popular), or 0 for a name that was less popular than the 1000th name. A sample file...
In java, Write a program that reads a data file containing final exam scores for a...
In java, Write a program that reads a data file containing final exam scores for a class and determines the number of passing scores (those greater than 60) and the number of failing scores (those less than 60). The average score as well as the range of scores should also be determined. The program should request the name of the data file from the end user. The file may contain any number of scores. Using a loop, read and process...
Using Java Write a program that reads a file of numbers of type int and outputs...
Using Java Write a program that reads a file of numbers of type int and outputs all of those numbers to another file, but without any duplicate numbers. You should assume that the input file is sorted from smallest to largest with one number on each line. After the program is run, the output file should contain all numbers that are in the original file, but no number should appear more than once. The numbers in the output file should...
Using Java Write a program that reads a file of numbers of type int and outputs...
Using Java Write a program that reads a file of numbers of type int and outputs all of those numbers to another file, but without any duplicate numbers. You should assume that the input file is sorted from smallest to largest with one number on each line. After the program is run, the output file should contain all numbers that are in the original file, but no number should appear more than once. The numbers in the output file should...
In java Write a program called FileProcessor.java that reads the file numbers.txt, and: Calculate the total...
In java Write a program called FileProcessor.java that reads the file numbers.txt, and: Calculate the total number of numbers in the file, Calculate the sum of all the numbers in the file, Calculate the average of all the numbers in the file, Find the smallest value of all the numbers in the file, Find the largest value of all the numbers in the file, Calculate the standard deviation of all the numbers in the file
Module 1 Program Write a complete Java program in a file called Module1Program.java that reads all...
Module 1 Program Write a complete Java program in a file called Module1Program.java that reads all the lyrics from a file named lyrics.txt that is to be found in the same directory as the running program. The program should read the lyrics for each line and treat each word as a token. If the line contains a double (an integer is also treated as a double) it should use the first double it finds in line as the timestamp for...
Using Java Project 2: Deduplication Write a program that reads a file of numbers of type...
Using Java Project 2: Deduplication Write a program that reads a file of numbers of type int and outputs all of those numbers to another file, but without any duplicate numbers. You should assume that the input file is sorted from smallest to largest with one number on each line. After the program is run, the output file should contain all numbers that are in the original file, but no number should appear more than once. The numbers in the...
Write a Java program that reads a list of 30 fruits from the file “fruits.txt”, inserts...
Write a Java program that reads a list of 30 fruits from the file “fruits.txt”, inserts them into a string array, and sorts the array in alphabetical order. String objects can be compared using relational operators such as <, >, or ==. For example, “abc” > “abd” is false, but “abc” < “abd” is true. Sample output: Before Sorting: Cherry, Honeydew, Cranberry, Lemon, Orange, Persimmon, Watermelon, Kiwifruit, Lime, Pomegranate, Jujube, Pineapple, Durian, Plum, Banana, Coconut, Apple, Tomato, Raisin, Mandarine, Blackberry,...
Write a Java program (single file/class) whose filename must be Numbers.java. The program reads a list...
Write a Java program (single file/class) whose filename must be Numbers.java. The program reads a list of integers from use input and has the following methods: 1. min - Finds the smallest integer in the list. 2. max- Finds the largest integer in the list. 3. average - Computes the average of all the numbers. 4. main - method prompts the user for the inputs and ends when the user enter Q or q and then neatly outputs the entire...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT