Using a text editor create a file that contains a list of at least 10 sin-dight
ID: 3683157 • Letter: U
Question
Using a text editor create a file that contains a list of at least 10 sin-dight account numbers read in each account number and display whether it is valid An account divided by 10.For example the number 223355 is valid because the sum of the fine Write only valid account numbers to an output. file each on its own line save the application as validate chek Digits java we an application that allows a user to enter a filename and an integer representing file Assume that the to enter is in the sane the next 20 characters there are the file as Seek Position javaExplanation / Answer
5.
DisplaySavedCustomerList.java
import java.nio.file.*;
import java.io.*;
import static java.nio.file.AccessMode.*;
public class DisplaySavedCustomerList
{
public static void main(String[] args)
{
Path file =
Paths.get("CustomerList.txt");
String[] array = new String[4];
String s = "";
String delimiter = ",";
try
{
InputStream input = new BufferedInputStream(Files.newInputStream(file));
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
s = reader.readLine();
while(s != null)
{
array = s.split(delimiter);
for(int x = 0; x < array.length; ++x)
System.out.print(array[x] + " ");
System.out.println();
s = reader.readLine();
}
reader.close();
}
catch(Exception e)
{
System.out.println("Message: " + e);
}
}
}
DisplaySelectedCustomer.java
import java.nio.file.*;
import java.io.*;
import static java.nio.file.AccessMode.*;
import java.util.Scanner;
public class DisplaySelectedCustomer
{
public static void main(String[] args)
{
Scanner keyBoard = new Scanner(System.in);
Path file =
Paths.get("CustomerList.txt");
String[] array = new String[4];
String s = "";
String delimiter = ",";
String searchID;
boolean wasFound = false;
try
{
InputStream input = new BufferedInputStream(Files.newInputStream(file));
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
System.out.println();
System.out.print("Enter ID number to search for >> ");
searchID = keyBoard.nextLine();
s = reader.readLine();
while(s != null)
{
array = s.split(delimiter);
if(searchID.equals(array[0]))
{
wasFound = true;
for(int x = 0; x < array.length; ++x)
System.out.print(array[x] + " ");
System.out.println();
}
s = reader.readLine();
}
if(!wasFound)
System.out.println("No records found for " + searchID);
reader.close();
}
catch(Exception e)
{
System.out.println("Message: " + e);
}
}
}
DisplaySelectedCustomersByBalance.java
import java.nio.file.*;
import java.io.*;
import static java.nio.file.AccessMode.*;
import java.util.Scanner;
public class DisplaySelectedCustomersByBalance
{
public static void main(String[] args)
{
Scanner keyBoard = new Scanner(System.in);
Path file =
Paths.get("CustomerList.txt");
String[] array = new String[4];
String s = "";
String delimiter = ",";
double balance;
boolean wasFound = false;
final int BAL_POSITION = 3;
try
{
InputStream input = new BufferedInputStream(Files.newInputStream(file));
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
System.out.println();
System.out.print("Enter minimum balance >> ");
balance = keyBoard.nextDouble();
s = reader.readLine();
while(s != null)
{
array = s.split(delimiter);
custBal = Double.parseDouble(array[BAL_POSITION]);
if(custBal >= balance)
{
wasFound = true;
for(int x = 0; x < array.length; ++x)
System.out.print(array[x] + " ");
System.out.println();
}
s = reader.readLine();
}
if(!wasFound)
System.out.println("No records found with a balance of at least $" + balance);
reader.close();
}
catch(Exception e)
{
System.out.println("Message: " + e);
}
}
}
DisplaySelectedCustomersByName.java
import java.nio.file.*;
import java.io.*;
import static java.nio.file.AccessMode.*;
import java.util.Scanner;
public class DisplaySelectedCustomersByName
{
public static void main(String[] args)
{
Scanner keyBoard = new Scanner(System.in);
Path file =
Paths.get("CustomerList.txt");
String[] array = new String[4];
String s = "";
String delimiter = ",";
String searchName;
boolean wasFound = false;
final int LAST_NAME_POSITION = 2;
try
{
InputStream input = new BufferedInputStream(Files.newInputStream(file));
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
System.out.println();
System.out.print("Enter last name to search for >> ");
searchName = keyBoard.nextLine();
s = reader.readLine();
while(s != null)
{
array = s.split(delimiter);
if(searchName.equals(array[LAST_NAME_POSITION]))
{
wasFound = true;
for(int x = 0; x < array.length; ++x)
System.out.print(array[x] + " ");
System.out.println();
}
s = reader.readLine();
}
if(!wasFound)
System.out.println("No records found for " + searchName);
reader.close();
}
catch(Exception e)
{
System.out.println("Message: " + e);
}
}
}
WriteCustomerList.java
import java.nio.file.*;
import java.io.*;
import static java.nio.file.AccessMode.*;
import java.util.Scanner;
public class WriteCustomerList
{
public static void main(String[] args)
{
Path file =
Paths.get("C:\Java\Chapter.13\CustomerList.txt");
Scanner kb = new Scanner(System.in);
String[] array = new String[3];
String s = "";
String delimiter = ",";
String id;
String firstName;
String lastName;
double balance;
final String QUIT = "ZZZ";
try
{
OutputStream output = new BufferedOutputStream(Files.newOutputStream(file));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output));
System.out.print("Enter ID number or " + QUIT + " to quit >> ");
id = kb.nextLine();
while(!id.equals(QUIT))
{
System.out.print("Enter first name >> ");
firstName = kb.nextLine();
System.out.print("Enter last name >> ");
lastName = kb.nextLine();
System.out.print("Enter balance >> ");
balance = kb.nextDouble();
kb.nextLine();
s = id + delimiter + firstName + delimiter + lastName +
delimiter + balance + System.getProperty("line.separator");
writer.write(s, 0, s.length());
System.out.print("Enter ID number or " + QUIT + " to quit >> ");
id = kb.nextLine();
}
writer.close();
}
catch(Exception e)
{
System.out.println("Message: " + e);
}
}
}
6.
import java.nio.file.*;
import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.ByteBuffer;
import static java.nio.file.StandardOpenOption.*;
public class ValidateCheckDigits
{
public static void main(String[] args)
{
Path fileIn =
Paths.get("AcctNumsIn.txt");
Path fileOut =
Paths.get("AcctNumsOut.txt");
String acct;
int acctNum;
int lastDigit;
int digit;
int sum;
int x;
String newline = " ";
InputStream input = null;
OutputStream output = null;
try
{
input = Files.newInputStream(fileIn);
BufferedReader reader = new BufferedReader
(new InputStreamReader(input));
output = Files.newOutputStream(fileOut);
acct = reader.readLine();
while(acct != null)
{
sum = 0;
acctNum = Integer.parseInt(acct);
lastDigit = acctNum % 10;
acctNum = acctNum / 10;
for(x = 0; x < 6; x++)
{
digit = acctNum % 10;
acctNum = acctNum / 10;
sum = sum + digit;
}
sum = sum % 10;
if(sum == lastDigit)
{
System.out.println(acct + " is valid");
acct = acct + System.getProperty("line.separator");
byte[] data = acct.getBytes();
output.write(data);
}
else
{
System.out.println(acct + " is invalid");
}
acct = reader.readLine();
}
input.close();
output.close();
}
catch (IOException e)
{
System.out.println(e);
}
}
}
AcctNumsIn.txt
345619
789400
871208
901156
984334
723422
172257
100000
273699
999995
7
SeekPosition.java
import java.nio.file.*;
import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.ByteBuffer;
import static java.nio.file.StandardOpenOption.*;
import java.util.Scanner;
public class SeekPosition
{
public static void main(String[] args)
{
String name;
Scanner keyboard = new Scanner(System.in);
int pos;
final int SIZE = 20;
System.out.print("Enter a file name >> ");
name = keyboard.nextLine();
Path inputPath = Paths.get(name);
Path fullPath = inputPath.toAbsolutePath();
System.out.print("Enter a position number >> ");
pos = keyboard.nextInt();
byte[] data = new byte[SIZE];
ByteBuffer buffer = ByteBuffer.allocate(SIZE);
FileChannel fc = null;
String s;
try
{
fc = (FileChannel)Files.newByteChannel(fullPath, READ);
buffer= ByteBuffer.wrap(data);
fc.position(pos);
fc.read(buffer);
s = new String(data);
System.out.println(s);
}
catch (Exception e)
{
System.out.println("Error message: " + e);
}
}
}
SeekPosition2.java
import java.nio.file.*;
import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.ByteBuffer;
import static java.nio.file.StandardOpenOption.*;
import java.util.Scanner;
public class SeekPosition2
{
public static void main(String[] args)
{
String name;
Scanner keyboard = new Scanner(System.in);
int pos;
int numToDisplay;
System.out.print("Enter a file name >> ");
name = keyboard.nextLine();
Path inputPath = Paths.get(name);
Path fullPath = inputPath.toAbsolutePath();
System.out.print("Enter a position number >> ");
pos = keyboard.nextInt();
System.out.print("Enter number of characters to display >> ");
numToDisplay = keyboard.nextInt();
byte[] data = new byte[numToDisplay];
ByteBuffer buffer = ByteBuffer.allocate(numToDisplay);
FileChannel fc = null;
String s;
try
{
fc = (FileChannel)Files.newByteChannel(fullPath, READ);
buffer= ByteBuffer.wrap(data);
fc.position(pos);
fc.read(buffer);
s = new String(data);
System.out.println(s);
}
catch (Exception e)
{
System.out.println("Error message: " + e);
}
}
}
8
StudentsStanding.java
import java.nio.file.*;
import java.io.*;
import static java.nio.file.StandardOpenOption.*;
import java.util.Scanner;
import java.nio.channels.FileChannel;
public class StudentsStanding
{
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
Path goodFile =
Paths.get("StudentsGoodStanding.txt");
Path probFile =
Paths.get("StudentsAcademicProbation.txt");
String s = "";
String delimiter = ",";
String stringId;
String firstName;
String lastName;
double gpa;
final String QUIT = "ZZZ";
final double CUTOFF = 2.0;
try
{
OutputStream goodStream = new BufferedOutputStream(Files.newOutputStream(goodFile, CREATE));
BufferedWriter goodWriter = new BufferedWriter(new OutputStreamWriter(goodStream));
OutputStream probStream = new BufferedOutputStream(Files.newOutputStream(probFile, CREATE));
BufferedWriter probWriter = new BufferedWriter(new OutputStreamWriter(probStream));
System.out.print("Enter student ID >> ");
stringId = kb.nextLine();
while(!stringId.equals(QUIT))
{
System.out.print("Enter firstName >> ");
firstName = kb.nextLine();
System.out.print("Enter last name >> ");
lastName = kb.nextLine();
System.out.print("Enter grade point average >> ");
gpa = kb.nextDouble();
kb.nextLine();
s = stringId + delimiter + firstName + delimiter +
lastName + delimiter + gpa + System.getProperty("line.separator");
if(gpa < CUTOFF)
probWriter.write(s, 0, s.length());
else
goodWriter.write(s, 0, s.length());
System.out.print("Enter next student ID or " + QUIT + " to quit >> ");
stringId = kb.nextLine();
}
probWriter.close();
goodWriter.close();
}
catch(Exception e)
{
System.out.println("Message: " + e);
}
}
}
sample output
Enter student ID >> 1
Enter firstName >> sowmi
Enter last name >> kavi
Enter grade point average >> 9
Enter next student ID or ZZZ to quit >> zzz
Enter firstName >> 2
Enter last name >> kavi
Enter grade point average >> 8
Enter next student ID or ZZZ to quit >> ZZZ
StudentsStanding2.java
import java.nio.file.*;
import java.io.*;
import static java.nio.file.StandardOpenOption.*;
public class StudentsStanding2
{
public static void main(String[] args)
{
Path goodFile =
Paths.get("StudentsGoodStanding.txt");
Path probFile =
Paths.get("StudentsAcademicProbation.txt");
String s = "";
try
{
InputStream goodStream = new BufferedInputStream(Files.newInputStream(goodFile));
BufferedReader goodReader = new BufferedReader(new InputStreamReader(goodStream));
InputStream probStream = new BufferedInputStream(Files.newInputStream(probFile));
BufferedReader probReader = new BufferedReader(new InputStreamReader(probStream));
System.out.println(" Probationary Standing ");
s = probReader.readLine();
while( s != null)
{
display(s);
s = probReader.readLine();
}
System.out.println(" Good Standing ");
s = goodReader.readLine();
while( s != null)
{
display(s);
s = goodReader.readLine();
}
probStream.close();
goodStream.close();
}
catch(Exception e)
{
System.out.println("Message: " + e);
}
}
public static void display(String s)
{
String[] array = new String[4];
String delimiter = ",";
String stringId;
String firstName;
String lastName;
double gpa;
double overUnder;
final double CUTOFF = 2.0;
array = s.split(delimiter);
stringId = array[0];
firstName = array[1];
lastName = array[2];
gpa = Double.parseDouble(array[3]);
overUnder = gpa - CUTOFF;
System.out.println("ID #" + stringId + " " + firstName + " " +
lastName + " GPA: " + gpa + " " + overUnder +
" from " + CUTOFF + " cutoff");
}
}
sample output
Probationary Standing
Good Standing
ID #java -Xmx128M -Xms16M HelloWorld sowmi kavi GPA: 9.0 7.0 from 2.0 cutoff
ID #zzz 2 kavi GPA: 8.0 6.0 from 2.0 cutoff
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.