- Create a Java program that reads in numbers from a file and determines if they
ID: 3675234 • Letter: #
Question
- Create a Java program that reads in numbers from a file and determines if they are in between two other numbers. For example, it might print a message if the number is between 1 and 5.
- Repeat the previous exercise but read the numbers from a console, and quit the program when the user enters a negative number.
- Create a static method that takes a number as an argument and returns an array of numbers. As an example, the method might take the number 5 and return an array of [5,10,15] (1, 2, and 3 times the input).
- Create a program that stores strings in an ArrayList and then prints them to the console.
Explanation / Answer
Data read from file:
package readfromfile;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class ReadFromFile {
public static void main(String[] args) {
// The name of the file to open.
String fileName = "temp.txt";
int value;
// This will reference one line at a time
String line = null;
try {
// FileReader reads text files in the default encoding.
FileReader fileReader =
new FileReader(fileName);
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader =
new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
value=Integer.parseInt(line);
int i=value;
while(i>=0)
{
if(i==0)
{
System.out.println(value+" is between "+i+" or("+0+"to"+(value-1)+")and "+(value+1)+" and above it");
}
i--;
}
}
// Always close files.
bufferedReader.close();
}
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
fileName + "'");
}
catch(IOException ex) {
System.out.println(
"Error reading file '"
+ fileName + "'");
// Or we could just do this:
// ex.printStackTrace();
}
}
}
file contents:
1
2
3
4
5
Data read from console:
package readfromfile;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ReadFormConsole {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int value,i;
public void read() throws IOException{
do{
System.out.println("enter positive number");
value=Integer.parseInt(br.readLine());
int i=value;
while(i>=0)
{
if(i==0)
{
System.out.println(value+" is between "+i+" or("+0+"to"+(value-1)+")and "+(value+1)+" and above it");
}
i--;
}
}while(value > 0);
}
public static void main(String[] args) throws IOException
{
ReadFormConsole c=new ReadFormConsole();
c.read();
}
}
Make array from input:
package readfromfile;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ArrayOfNumbers {
public static void makeArray() throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int array[]=new int[3];
int input,quit;
boolean j = true;
do{
do{
System.out.println(" enter positve integer ");
input=Integer.parseInt(br.readLine());
}while(input <= 0);
for(int i=0;i<3;i++)
{
array[i]=(input*(i+1));
}
for(int i=0;i<3;i++)
{
System.out.print(array[i]+" ");
}
System.out.println(" enter (-ve)value for quit or (+ve)value to proceed ");
quit=Integer.parseInt(br.readLine());
if(quit<0)
j=false;
}while(j);
}
public static void main(String[] args) throws IOException
{
ArrayOfNumbers a=new ArrayOfNumbers();
a.makeArray();
}
}
Creating ArrayList:
package readfromfile;
import java.util.Arrays;
import java.util.ArrayList;
public class MyClass {
public static void main(String[] args)
{
//String object having values, separated by ","
String strNumbers = "1,2,3,4,5";
/*
* To convert Java String to ArrayList, first split the string and then
* use asList method of Arrays class to convert it to ArrayList.
*/
//split the string using separator, in this case it is ","
String[] strValues = strNumbers.split(",");
/*
* Use asList method of Arrays class to convert Java String array to ArrayList
*/
ArrayList<String> aListNumbers = new ArrayList<>(Arrays.asList(strValues));
System.out.println("Java String converted to ArrayList: " + aListNumbers);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.