Related
I'm new to using Java, but I have some previous experience with C#. The issue I'm having comes with reading user input from console.
I'm running into the "java.util.NoSuchElementException" error with this portion of code:
payment = sc.next(); // PromptCustomerPayment function
I have two functions that get user input:
PromptCustomerQty
PromptCustomerPayment
If I don't call PromptCustomerQty then I don't get this error, which leads me to believe I am doing something wrong with scanner. Below is my full code sample. I appreciate any help.
public static void main (String[] args) {
// Create a customer
// Future proofing the possabiltiies of multiple customers
Customer customer = new Customer("Will");
// Create object for each Product
// (Name,Code,Description,Price)
// Initalize Qty at 0
Product Computer = new Product("Computer","PC1003","Basic Computer",399.99);
Product Monitor = new Product("Monitor","MN1003","LCD Monitor",99.99);
Product Printer = new Product("Printer","PR1003x","Inkjet Printer",54.23);
// Define internal variables
// ## DONT CHANGE
ArrayList<Product> ProductList = new ArrayList<Product>(); // List to store Products
String formatString = "%-15s %-10s %-20s %-10s %-10s %n"; // Default format for output
// Add objects to list
ProductList.add(Computer);
ProductList.add(Monitor);
ProductList.add(Printer);
// Ask users for quantities
PromptCustomerQty(customer, ProductList);
// Ask user for payment method
PromptCustomerPayment(customer);
// Create the header
PrintHeader(customer, formatString);
// Create Body
PrintBody(ProductList, formatString);
}
public static void PromptCustomerQty(Customer customer, ArrayList<Product> ProductList) {
// Initiate a Scanner
Scanner scan = new Scanner(System.in);
// **** VARIABLES ****
int qty = 0;
// Greet Customer
System.out.println("Hello " + customer.getName());
// Loop through each item and ask for qty desired
for (Product p : ProductList) {
do {
// Ask user for qty
System.out.println("How many would you like for product: " + p.name);
System.out.print("> ");
// Get input and set qty for the object
qty = scan.nextInt();
}
while (qty < 0); // Validation
p.setQty(qty); // Set qty for object
qty = 0; // Reset count
}
// Cleanup
scan.close();
}
public static void PromptCustomerPayment (Customer customer) {
// Initiate Scanner
Scanner sc = new Scanner(System.in);
// Variables
String payment = "";
// Prompt User
do {
System.out.println("Would you like to pay in full? [Yes/No]");
System.out.print("> ");
payment = sc.next();
} while ((!payment.toLowerCase().equals("yes")) && (!payment.toLowerCase().equals("no")));
// Check/set result
if (payment.toLowerCase().equals("yes")) {
customer.setPaidInFull(true);
}
else {
customer.setPaidInFull(false);
}
// Cleanup
sc.close();
}
This has really puzzled me for a while but this is what I found in the end.
When you call, sc.close() in first method, it not only closes your scanner but closes your System.in input stream as well. You can verify it by printing its status at very top of the second method as :
System.out.println(System.in.available());
So, now when you re-instantiate, Scanner in second method, it doesn't find any open System.in stream and hence the exception.
I doubt if there is any way out to reopen System.in because:
public void close() throws IOException --> Closes this input stream and releases any system resources associated with this stream. The general contract of close is that it closes the input stream. A closed stream cannot perform input operations and **cannot be reopened.**
The only good solution for your problem is to initiate the Scanner in your main method, pass that as argument in your two methods, and close it again in your main method e.g.:
main method related code block:
Scanner scanner = new Scanner(System.in);
// Ask users for quantities
PromptCustomerQty(customer, ProductList, scanner );
// Ask user for payment method
PromptCustomerPayment(customer, scanner );
//close the scanner
scanner.close();
Your Methods:
public static void PromptCustomerQty(Customer customer,
ArrayList<Product> ProductList, Scanner scanner) {
// no more scanner instantiation
...
// no more scanner close
}
public static void PromptCustomerPayment (Customer customer, Scanner sc) {
// no more scanner instantiation
...
// no more scanner close
}
Hope this gives you some insight about the failure and possible resolution.
The problem is
When a Scanner is closed, it will close its input source if the source implements the Closeable interface.
http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html
Thus scan.close() closes System.in.
To fix it you can make
Scanner scan static
and do not close it in PromptCustomerQty. Code below works.
public static void main (String[] args) {
// Create a customer
// Future proofing the possabiltiies of multiple customers
Customer customer = new Customer("Will");
// Create object for each Product
// (Name,Code,Description,Price)
// Initalize Qty at 0
Product Computer = new Product("Computer","PC1003","Basic Computer",399.99);
Product Monitor = new Product("Monitor","MN1003","LCD Monitor",99.99);
Product Printer = new Product("Printer","PR1003x","Inkjet Printer",54.23);
// Define internal variables
// ## DONT CHANGE
ArrayList<Product> ProductList = new ArrayList<Product>(); // List to store Products
String formatString = "%-15s %-10s %-20s %-10s %-10s %n"; // Default format for output
// Add objects to list
ProductList.add(Computer);
ProductList.add(Monitor);
ProductList.add(Printer);
// Ask users for quantities
PromptCustomerQty(customer, ProductList);
// Ask user for payment method
PromptCustomerPayment(customer);
// Create the header
PrintHeader(customer, formatString);
// Create Body
PrintBody(ProductList, formatString);
}
static Scanner scan;
public static void PromptCustomerQty(Customer customer, ArrayList<Product> ProductList) {
// Initiate a Scanner
scan = new Scanner(System.in);
// **** VARIABLES ****
int qty = 0;
// Greet Customer
System.out.println("Hello " + customer.getName());
// Loop through each item and ask for qty desired
for (Product p : ProductList) {
do {
// Ask user for qty
System.out.println("How many would you like for product: " + p.name);
System.out.print("> ");
// Get input and set qty for the object
qty = scan.nextInt();
}
while (qty < 0); // Validation
p.setQty(qty); // Set qty for object
qty = 0; // Reset count
}
// Cleanup
}
public static void PromptCustomerPayment (Customer customer) {
// Variables
String payment = "";
// Prompt User
do {
System.out.println("Would you like to pay in full? [Yes/No]");
System.out.print("> ");
payment = scan.next();
} while ((!payment.toLowerCase().equals("yes")) && (!payment.toLowerCase().equals("no")));
// Check/set result
if (payment.toLowerCase() == "yes") {
customer.setPaidInFull(true);
}
else {
customer.setPaidInFull(false);
}
}
On a side note, you shouldn't use == for String comparision, use .equals instead.
You need to remove the scanner closing lines: scan.close();
It happened to me before and that was the reason.
the reason of the exception has been explained already, however the suggested solution isn't really the best.
You should create a class that keeps a Scanner as private using Singleton Pattern, that makes that scanner unique on your code.
Then you can implement the methods you need or you can create a getScanner ( not recommended ) and you can control it with a private boolean, something like alreadyClosed.
If you are not aware how to use Singleton Pattern, here's a example:
public class Reader {
private Scanner reader;
private static Reader singleton = null;
private boolean alreadyClosed;
private Reader() {
alreadyClosed = false;
reader = new Scanner(System.in);
}
public static Reader getInstance() {
if(singleton == null) {
singleton = new Reader();
}
return singleton;
}
public int nextInt() throws AlreadyClosedException {
if(!alreadyClosed) {
return reader.nextInt();
}
throw new AlreadyClosedException(); //Custom exception
}
public double nextDouble() throws AlreadyClosedException {
if(!alreadyClosed) {
return reader.nextDouble();
}
throw new AlreadyClosedException();
}
public String nextLine() throws AlreadyClosedException {
if(!alreadyClosed) {
return reader.nextLine();
}
throw new AlreadyClosedException();
}
public void close() {
alreadyClosed = true;
reader.close();
}
}
For anyone who arrived here while taking an online exam on a site like HackerRank-
You might receive this if you are trying to test your (possibly) perfectly fine code by clicking the button to execute main() with custom input.
In this case, you need to be clicking the other button, something like "Run Unit Tests". You're likely only being evaluated on whether the code passes the unit tests they wrote- not your ability to refactor code to fewer LOC or your coding style.
I want to create a simple reservation application, like when you want to book movie ticket online, except this is a much simplified version.
I have a class names reservation with 8 toggle buttons and 1 button, the user will get to input number of seat they want to book in another class, let's just say 4.
In reservation, after the user choose 4 seats, the rest of the toggle button will be disabled to prevent user from choosing more seats. The user will then click the save button, and the data will then be inputted in the Database. Next time when another user open reservation, all of the toggle buttons will be re-enabled except those 4 seats previous user has booked earlier, obviously.
Everything is working so far except the part that i bold.
Here's my code :
private static final JToggleButton[] btn = new JToggleButton[8];
private int totalrow, a;
java.sql.Connection connection = null;
Statement statement4 = null;
ResultSet resultSet = null;
private void formWindowActivated(java.awt.event.WindowEvent evt) { // This is where i load the information that has been saved in the database before.
try {
connection = DriverManager.getConnection("jdbc:derby:
statement4 = (Statement) connection.createStatement();
resultSet = statement4.executeQuery("SELECT reservationid, seatno, validator FROM reservation WHERE reservationid = '" + time.id + "'");
String seatno, validator, rsid, id;
btn[0] = seat1;
btn[1] = seat2;
btn[2] = seat3;
btn[3] = seat4;
btn[4] = seat5;
btn[5] = seat6;
btn[6] = seat7;
btn[7] = seat8;
int i = 0;
while(resultSet.next()){
seatno = resultSet.getString("seatno");
validator = resultSet.getString("validator");
rsid = resultSet.getString("reservationid");
if(seatno.equals(btn[i].getText()) && validator.equals("reserved")){ //checking if seat i is reserved
btn[i].setSelected(true);
}
else{
btn[i].setSelected(false);
}
i++;
}
} catch (SQLException ex) {
Logger.getLogger(seat.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void actionPerformed(ActionEvent e) { // This actionlistener is used to count, if User has choose 4 seats, the code will disable the rest of the button
try {
connection = DriverManager.getConnection("jdbc:derby://localhost:1527/Project","root","root");
statement = (Statement) connection.createStatement();
btn[0] = seat1;
btn[1] = seat2;
btn[2] = seat3;
btn[3] = seat4;
btn[4] = seat5;
btn[5] = seat6;
btn[6] = seat7;
btn[7] = seat8;
a = 0;
for(JToggleButton btns : btn){
if (btns.isSelected()){
a++;
System.out.println(a);
if (! btns.isSelected()){
System.out.println(a);
a--;
}
}
}
for(JToggleButton btns : btn){
if (a >= Integer.parseInt(BookingUI.passenger)){ // This is number of Passenger taken from another class. In this case is 4.
if(! btns.isSelected()){
btns.setEnabled(false);
}
}
else{
btns.setEnabled(true);
}
}
} catch (SQLException ex) {
Logger.getLogger(seat.class.getName()).log(Level.SEVERE, null, ex);
}
}
Regarding my title, based on my current code and logic, it might not be possible to re-enable the toggle button. So the better way to do it might be to use another logic to disable the toggle button instead.
In the image, seat 1,4,5,8 are chosen by the user. And the rest of the buttons are disabled
Since it's a long implementation, it's difficult to answer with a code solution. There for I will mention my suggestion here.
You have to declare your JToggleButton Array like this btn = new JToggleButton[8]; in a public static context where every class could access it. And when the user reserved a seat you should save the data to DB (or keep in the program memroy, eg : HashMap) on which user reserved the which seat.
After that when you loading the data from the Database you should loop through and get the booked seats which previously inserted and then by accessing that predefined JToggleButton Array and set each booked seat by doing this btns[i].setEnabled(false); (i for the booked index of the seat).
Hope this helps
I'm new to using Java, but I have some previous experience with C#. The issue I'm having comes with reading user input from console.
I'm running into the "java.util.NoSuchElementException" error with this portion of code:
payment = sc.next(); // PromptCustomerPayment function
I have two functions that get user input:
PromptCustomerQty
PromptCustomerPayment
If I don't call PromptCustomerQty then I don't get this error, which leads me to believe I am doing something wrong with scanner. Below is my full code sample. I appreciate any help.
public static void main (String[] args) {
// Create a customer
// Future proofing the possabiltiies of multiple customers
Customer customer = new Customer("Will");
// Create object for each Product
// (Name,Code,Description,Price)
// Initalize Qty at 0
Product Computer = new Product("Computer","PC1003","Basic Computer",399.99);
Product Monitor = new Product("Monitor","MN1003","LCD Monitor",99.99);
Product Printer = new Product("Printer","PR1003x","Inkjet Printer",54.23);
// Define internal variables
// ## DONT CHANGE
ArrayList<Product> ProductList = new ArrayList<Product>(); // List to store Products
String formatString = "%-15s %-10s %-20s %-10s %-10s %n"; // Default format for output
// Add objects to list
ProductList.add(Computer);
ProductList.add(Monitor);
ProductList.add(Printer);
// Ask users for quantities
PromptCustomerQty(customer, ProductList);
// Ask user for payment method
PromptCustomerPayment(customer);
// Create the header
PrintHeader(customer, formatString);
// Create Body
PrintBody(ProductList, formatString);
}
public static void PromptCustomerQty(Customer customer, ArrayList<Product> ProductList) {
// Initiate a Scanner
Scanner scan = new Scanner(System.in);
// **** VARIABLES ****
int qty = 0;
// Greet Customer
System.out.println("Hello " + customer.getName());
// Loop through each item and ask for qty desired
for (Product p : ProductList) {
do {
// Ask user for qty
System.out.println("How many would you like for product: " + p.name);
System.out.print("> ");
// Get input and set qty for the object
qty = scan.nextInt();
}
while (qty < 0); // Validation
p.setQty(qty); // Set qty for object
qty = 0; // Reset count
}
// Cleanup
scan.close();
}
public static void PromptCustomerPayment (Customer customer) {
// Initiate Scanner
Scanner sc = new Scanner(System.in);
// Variables
String payment = "";
// Prompt User
do {
System.out.println("Would you like to pay in full? [Yes/No]");
System.out.print("> ");
payment = sc.next();
} while ((!payment.toLowerCase().equals("yes")) && (!payment.toLowerCase().equals("no")));
// Check/set result
if (payment.toLowerCase().equals("yes")) {
customer.setPaidInFull(true);
}
else {
customer.setPaidInFull(false);
}
// Cleanup
sc.close();
}
This has really puzzled me for a while but this is what I found in the end.
When you call, sc.close() in first method, it not only closes your scanner but closes your System.in input stream as well. You can verify it by printing its status at very top of the second method as :
System.out.println(System.in.available());
So, now when you re-instantiate, Scanner in second method, it doesn't find any open System.in stream and hence the exception.
I doubt if there is any way out to reopen System.in because:
public void close() throws IOException --> Closes this input stream and releases any system resources associated with this stream. The general contract of close is that it closes the input stream. A closed stream cannot perform input operations and **cannot be reopened.**
The only good solution for your problem is to initiate the Scanner in your main method, pass that as argument in your two methods, and close it again in your main method e.g.:
main method related code block:
Scanner scanner = new Scanner(System.in);
// Ask users for quantities
PromptCustomerQty(customer, ProductList, scanner );
// Ask user for payment method
PromptCustomerPayment(customer, scanner );
//close the scanner
scanner.close();
Your Methods:
public static void PromptCustomerQty(Customer customer,
ArrayList<Product> ProductList, Scanner scanner) {
// no more scanner instantiation
...
// no more scanner close
}
public static void PromptCustomerPayment (Customer customer, Scanner sc) {
// no more scanner instantiation
...
// no more scanner close
}
Hope this gives you some insight about the failure and possible resolution.
The problem is
When a Scanner is closed, it will close its input source if the source implements the Closeable interface.
http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html
Thus scan.close() closes System.in.
To fix it you can make
Scanner scan static
and do not close it in PromptCustomerQty. Code below works.
public static void main (String[] args) {
// Create a customer
// Future proofing the possabiltiies of multiple customers
Customer customer = new Customer("Will");
// Create object for each Product
// (Name,Code,Description,Price)
// Initalize Qty at 0
Product Computer = new Product("Computer","PC1003","Basic Computer",399.99);
Product Monitor = new Product("Monitor","MN1003","LCD Monitor",99.99);
Product Printer = new Product("Printer","PR1003x","Inkjet Printer",54.23);
// Define internal variables
// ## DONT CHANGE
ArrayList<Product> ProductList = new ArrayList<Product>(); // List to store Products
String formatString = "%-15s %-10s %-20s %-10s %-10s %n"; // Default format for output
// Add objects to list
ProductList.add(Computer);
ProductList.add(Monitor);
ProductList.add(Printer);
// Ask users for quantities
PromptCustomerQty(customer, ProductList);
// Ask user for payment method
PromptCustomerPayment(customer);
// Create the header
PrintHeader(customer, formatString);
// Create Body
PrintBody(ProductList, formatString);
}
static Scanner scan;
public static void PromptCustomerQty(Customer customer, ArrayList<Product> ProductList) {
// Initiate a Scanner
scan = new Scanner(System.in);
// **** VARIABLES ****
int qty = 0;
// Greet Customer
System.out.println("Hello " + customer.getName());
// Loop through each item and ask for qty desired
for (Product p : ProductList) {
do {
// Ask user for qty
System.out.println("How many would you like for product: " + p.name);
System.out.print("> ");
// Get input and set qty for the object
qty = scan.nextInt();
}
while (qty < 0); // Validation
p.setQty(qty); // Set qty for object
qty = 0; // Reset count
}
// Cleanup
}
public static void PromptCustomerPayment (Customer customer) {
// Variables
String payment = "";
// Prompt User
do {
System.out.println("Would you like to pay in full? [Yes/No]");
System.out.print("> ");
payment = scan.next();
} while ((!payment.toLowerCase().equals("yes")) && (!payment.toLowerCase().equals("no")));
// Check/set result
if (payment.toLowerCase() == "yes") {
customer.setPaidInFull(true);
}
else {
customer.setPaidInFull(false);
}
}
On a side note, you shouldn't use == for String comparision, use .equals instead.
You need to remove the scanner closing lines: scan.close();
It happened to me before and that was the reason.
the reason of the exception has been explained already, however the suggested solution isn't really the best.
You should create a class that keeps a Scanner as private using Singleton Pattern, that makes that scanner unique on your code.
Then you can implement the methods you need or you can create a getScanner ( not recommended ) and you can control it with a private boolean, something like alreadyClosed.
If you are not aware how to use Singleton Pattern, here's a example:
public class Reader {
private Scanner reader;
private static Reader singleton = null;
private boolean alreadyClosed;
private Reader() {
alreadyClosed = false;
reader = new Scanner(System.in);
}
public static Reader getInstance() {
if(singleton == null) {
singleton = new Reader();
}
return singleton;
}
public int nextInt() throws AlreadyClosedException {
if(!alreadyClosed) {
return reader.nextInt();
}
throw new AlreadyClosedException(); //Custom exception
}
public double nextDouble() throws AlreadyClosedException {
if(!alreadyClosed) {
return reader.nextDouble();
}
throw new AlreadyClosedException();
}
public String nextLine() throws AlreadyClosedException {
if(!alreadyClosed) {
return reader.nextLine();
}
throw new AlreadyClosedException();
}
public void close() {
alreadyClosed = true;
reader.close();
}
}
For anyone who arrived here while taking an online exam on a site like HackerRank-
You might receive this if you are trying to test your (possibly) perfectly fine code by clicking the button to execute main() with custom input.
In this case, you need to be clicking the other button, something like "Run Unit Tests". You're likely only being evaluated on whether the code passes the unit tests they wrote- not your ability to refactor code to fewer LOC or your coding style.
What I want to do is read a text file that has humans and animals. It will compile but has an error when I try to run it. I think I need a for loop to read the stringtokenizer to decipher between the human and animal in the txt file so far this is my driver class.
txt file:
Morely,Robert,123 Anywhere Street,15396,4,234.56,2
Bubba,Bulldog,58,4-15-2010,6-14-2011
Lucy,Bulldog,49,4-15-2010,6-14-2011
Wilder,John,457 Somewhere Road,78214,3,124.53,1
Ralph,Cat,12,01-16-2011,04-21-2012
Miller,John,639 Green Glenn Drive,96258,5,0.00,3
Major,Lab,105,07-10-2012,06-13-2013
King,Collie,58,06-14-2012,10-05-2012
Pippy,cat,10,04-25-2015,04-25-2015
Jones,Sam,34 Franklin Apt B,47196,1,32.09,1
Gunther,Swiss Mountain Dog,125,10-10-2013,10-10-2013
Smith,Jack,935 Garrison Blvd,67125,4,364.00,4
Perry,Parrot,5,NA,3-13-2014
Jake,German Shepherd,86,11-14-2013,11-14-2013
Sweetie,tabby cat,15,12-15-2013,2-15-2015
Pete,boa,8,NA,3-15-2015
Source:
import java.util.Scanner;
import java.util.StringTokenizer;
import java.io.File;
import java.io.IOException;
/**
* This is my driver class that reads from a txt file to put into an array and uses the class refrences so it can use the menu and spit out
*
* #author ******
* #version 11/25/2015
*/
public class Driver
{
/**
* Constructor for objects of class Driver, what it does is read in the txt file gets the two class refrences and loops through to read through the whole file looking for string tokens to go to the next line
* and closes the file at the end also uses for loop to count number of string tokens to decipher between human and pets.
*/
public static void main(String[] args) throws IOException
{
Pet p;
Human h;
Scanner input;
char menu;
input = new Scanner(new File("clientdata.txt"));
int nBalance;
int id;
/**
* this while statement goes through each line looking for the string tokenizer ",". I want to count each "," to decipher between Human and Animal
*/
while(input.hasNext())
{
StringTokenizer st = new StringTokenizer(input.nextLine(), ",");
h = new Human();
h.setLastName(st.nextToken());
h.setFirstName(st.nextToken());
h.setAddress(st.nextToken());
h.setCiD(Integer.parseInt(st.nextToken()));
h.setVisits(Integer.parseInt(st.nextToken()));
h.setBalance(Double.parseDouble(st.nextToken()));
p = new Pet(st.nextToken(), st.nextToken(), Integer.parseInt(st.nextToken()), st.nextToken(), st.nextToken());
}
/**
* this is my seond while statement that loops the case switch statements and asks the user for client ID
*/
menu = 'Y';
while(menu == 'y' || menu == 'Y') {
System.out.print("\nChose one:\n A- client names and outstanding balance \n B- client's pets, name, type and date of last visit\n C-change the client's outstanding balance: ");
menu = input.next().charAt(0);
System.out.print("Enter client ID: ");
id = input.nextInt();
h = new Human();
if(id == h.getCiD())//if the id entered up top is equal to one of the id's in the txt file then it continues to the menu
{
p = new Pet();
switch(menu)
{ case 'A':
System.out.println("client name: " + h.getFirstName() + "outstanding balance: " + h.getBalance());
break;
case 'B':
System.out.println("pet's name: " + p.getName() + "type of pet: " + p.getTanimal() + "date of last visit: " + p.getLastVisit());
break;
case 'C':
System.out.println("what do you want to change the clients balances to?");
input.close();
}
}
else// if not then it goes to this If statement saying that the Client does not exist
{
System.out.println("Client does not exist.");
}
}
}
}
You have a number of issues you need to overcome...
For each line, you need to determine the type of data the line represents
You need some way to keep track of the data you've loaded (of the clients and their pets)
You need some way to associate each pet with it's owner
The first could be done in a number of ways, assuming we can change the data. You could make the first token meaningful (human, pet); you could use JSON or XML instead. But lets assume for the moment, you can't change the format.
The key difference between the two types of data is the number of tokens they contain, 7 for people, 5 for pets.
while (input.hasNext()) {
String text = input.nextLine();
String[] parts = text.split(",");
if (parts.length == 7) {
// Parse owner
} else if (parts.length == 5) {
// Parse pet
} // else invalid data
For the second problem you could use arrays, but you would need to know in advance the number of elements you will need, the number of people and for each person, the number of pets
Oddly enough, I just noticed that the last element is an int and seems to represent the number of pets!!
Morely,Robert,123 Anywhere Street,15396,4,234.56,2
------------^
But that doesn't help us for the owners.
For the owners, you could use a List of some kind and when ever you create a new Human, you would simply add them to the List, for example...
List<Human> humans = new ArrayList<>(25);
//...
if (parts.length == 7) {
// Parse the properties
human = new Human(...);
humans.add(human);
} else if (parts.length == 5) {
Thirdly, for the pets, each Pet should associated directly with the owner, for example:
Human human = null;
while (input.hasNext()) {
String text = input.nextLine();
String[] parts = text.split(",");
if (parts.length == 7) {
//...
} else if (parts.length == 5) {
if (human != null) {
// Parse pet properties
Pet pet = new Pet(name, type, age, date1, date2);
human.add(pet);
} else {
throw new NullPointerException("Found pet without human");
}
}
Okay, so all this does, is each time we create a Human, we keep a reference to the "current" or "last" owner created. For each "pet" line we parse, we add it to the owner.
Now, the Human class could use either a array or List to manage the pets, either will work, as we know the expected number of pets. You would then provide getters in the Human class to get a reference to the pets.
Because out-of-context code can be hard to read, this is an example of what you might be able to do...
Scanner input = new Scanner(new File("data.txt"));
List<Human> humans = new ArrayList<>(25);
Human human = null;
while (input.hasNext()) {
String text = input.nextLine();
String[] parts = text.split(",");
if (parts.length == 7) {
String firstName = parts[0];
String lastName = parts[1];
String address = parts[2];
int cid = Integer.parseInt(parts[3]);
int vists = Integer.parseInt(parts[4]);
double balance = Double.parseDouble(parts[5]);
int other = Integer.parseInt(parts[6]);
human = new Human(firstName, lastName, address, cid, vists, balance, other);
humans.add(human);
} else if (parts.length == 5) {
if (human != null) {
String name = parts[0];
String type = parts[1];
int age = Integer.parseInt(parts[2]);
String date1 = parts[3];
String date2 = parts[4];
Pet pet = new Pet(name, type, age, date1, date2);
human.add(pet);
} else {
throw new NullPointerException("Found pet without human");
}
}
}
What about using split() function instead of using StringTokenizer?
Say, You can change your first while loop like below:
while (input.hasNext()) {
// StringTokenizer st = new StringTokenizer(input.nextLine(), ",");
String[] tokens = input.nextLine().split(",");
if (tokens.length == 7) {
h = new Human();
h.setLastName(tokens[0]);
h.setFirstName(tokens[1]);
h.setAddress(tokens[2]);
h.setCiD(Integer.parseInt(tokens[3]));
h.setVisits(Integer.parseInt(tokens[4]));
h.setBalance(Double.parseDouble(tokens[5]));
} else {
p = new Pet(tokens[0], tokens[1], Integer.parseInt(tokens[2]), tokens[3], tokens[4]);
}
}
And for keeping track of which pet belongs to which human, you can append an arrayList of type Pet in Human class like below:
ArrayList<Pet> pets = new ArrayList<>();
And say you have another ArrayList of type Human named humans in the main function. So, you could append in if block like:
humans.add(h);
and in the else section, you could append in else block:
humans.get(humans.size()-1).pets.add(p);
You can try something like this -
Populate a map and then using that you can assign values according to your requirement.
public void differentiate(){
try {
Scanner scan=new Scanner(new BufferedReader(new FileReader("//your filepath")));
Map<String,List<String>> map=new HashMap<String, List<String>>();
while(scan.hasNextLine()){
List<String> petList=new ArrayList<String>();
String s=scan.nextLine();
String str[]=s.split(",");
String name=str[1]+" "+str[0];
int petCount=Integer.parseInt(str[str.length-1]);
for(int i=1;i<=petCount;i++){
String petString=scan.nextLine();
petList.add(petString);
}
map.put(name, petList);
}
Set<String> set=map.keySet();
for(String str:set){
System.out.println(str+" has "+map.get(str)+" pets");
}
}
catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I'm in a beginner Java course...and my latest assignment seems super difficult for some reason. The assignment is focused around File I/O. It involves many parts, but this part is what is really confusing me. It involves:
Reading a Sales file where each line is supposed to have first the
Division (N, S, E or W), then the Quarter (1-4) on the next line,
then the Sales Amount on the third line and then repeats. (can do
this ok)
We have to validate that each line has one of the allowable
options/file types (can do this ok)
Then dump any errors to an error file and ask the user to supply
another file & repeat until the input file is in the right format.
(have this working)
Once it is in the right format, we have to be able to generate a
sales report in the below format (can't figure out how I am
supposed to be able to get this collected...)
Sales Report
Total Sales by division:
North $total
South $total
East $total
West $total
Total Sales by quarter:
Jan-Mar $total
Apr-Jun $total
Jul-Sep $total
Oct-Dec $total
Year End Total: $total
One menu option has to be to Validate the file is in the correct format, and then another menu option appears to generate the Sales Report.
Note: We aren't expected to use arrays yet (that's the next assignment).
import java.io.*;
import java.util.Scanner;
public class SalesReporting {
public static void main(String[] args) throws Exception {
String inputData;
String divisionRead;
int qtrRead;
double salesRead;
String filePath;
String menuChoice;
File fileName;
PrintWriter outputErrorLog;
PrintWriter outputSalesReport;
Scanner keyboard = new Scanner(System.in);
Scanner inputFile;
String overwriteFile;
Boolean isDataFileError;
Boolean isValidSalesFile = false;
Boolean isValidMenuChoice;
Boolean isToOverwrite = false;
System.out.println("\tHarmway Corporation" +
"\nQuarterly Sales Report Generator");
do {
do {
isValidMenuChoice = true;
// display main menu options to user & prompt for choice
System.out.print("\n\tMain Menu" +
"\n" +
"\n[V]alidate Sales Data");
// only display generate sales report option if a sales report has been validated
if (isValidSalesFile) {
System.out.print("\n[G]enerate Sales Report");
}
System.out.print("\nE[x]it program" +
"\n");
System.out.print("\nEnter choice: ");
menuChoice = keyboard.next();
if (!menuChoice.equalsIgnoreCase("v") &&
!menuChoice.equalsIgnoreCase("g") &&
!menuChoice.equalsIgnoreCase("x")) {
isValidMenuChoice = false;
System.out.print("\n\t**Error** - Invalid menu item");
}
} while (isValidMenuChoice == false);
if (menuChoice.equalsIgnoreCase("v")) {
do {
// prompt user for the sales data file path
System.out.print("\nSales data file path: ");
filePath = keyboard.next();
fileName = new File(filePath);
// if the file path doesn't exist, error displayed
if (!fileName.exists()) {
System.out.println("\n" + fileName + " not found");
}
} while (!fileName.exists());
// create a scanner for the input file
inputFile = new Scanner(fileName);
// create an error log to dump invalid sales data errors to
fileName = new File("errorlog.txt");
outputErrorLog = new PrintWriter(fileName);
// resets boolean to allow for error free data file check
isDataFileError = false;
// validate and store the sales data
while (inputFile.hasNext())
{
// first line must be division N, S, E or W to be valid
inputData = inputFile.next();
try
{
divisionRead = inputData;
if (isValidDivision(divisionRead)) {
System.out.println("ok division");
}
else
{
isDataFileError = true;
outputErrorLog.println(divisionRead + ",Invalid division value");
}
}
catch (Exception e)
{
isDataFileError = true;
outputErrorLog.println(inputData + ",Invalid division value");
}
// if second line is a valid quarter
inputData = inputFile.next();
try
{
qtrRead = Integer.parseInt(inputData);
if (isValidQuarter(qtrRead)) {
System.out.println("ok quarter");
}
else
{
isDataFileError = true;
outputErrorLog.println(qtrRead + ",Invalid quarter value");
}
}
catch (Exception e)
{
isDataFileError = true;
outputErrorLog.println(inputData + ",Invalid quarter value");
}
inputData = inputFile.next();
try
{
salesRead = Double.parseDouble(inputData);
if (isValidSales(salesRead)) {
System.out.println("ok sales");
}
else
{
isDataFileError = true;
outputErrorLog.println(salesRead + ",Invalid sales amount value");
}
}
catch (Exception e)
{
isDataFileError = true;
outputErrorLog.println(inputData + ",Invalid sales amount value");
}
}
// close the input sales data file
inputFile.close();
// close the error log file to write
outputErrorLog.close();
if (isDataFileError)
{
// there was an error in the sales file; not a valid sales data file
isValidSalesFile = false;
System.out.print("\nThe data file contains data errors"
+ " (See error log for details)");
}
else
{
// there were no errors; valid sales data file
isValidSalesFile = true;
System.out.print("\nThe data file is validated");
}
}
if (menuChoice.equalsIgnoreCase("g")) {
System.out.println("\ntemp g");
do {
// prompt user for file path to save sales report
System.out.print("\nSave report as: ");
filePath = keyboard.next();
// identify file to be used
fileName = new File(filePath);
// file already exists; ask whether to overwrite file or not
if (fileName.exists())
{
do
{
System.out.print("\nOverwrite (y/n): ");
overwriteFile = keyboard.next();
if (overwriteFile.equalsIgnoreCase("y"))
{
isToOverwrite = true;
// create printwriter for the sales data file
outputSalesReport = new PrintWriter(filePath);
System.out.println("\nGenerating sales report...");
// add code to write to the results to the file
// close sales report
outputSalesReport.close();
System.out.println("\nReport generated successfully!");
}
} while (!overwriteFile.equalsIgnoreCase("n") &&
!overwriteFile.equalsIgnoreCase("y"));
}
// file doesn't already exist; save the file
else if (!fileName.exists())
{
// create printwriter for the sales data file
outputSalesReport = new PrintWriter(filePath);
System.out.println("\nGenerating sales report...");
// add code to write to the results to the file
// close sales report
outputSalesReport.close();
System.out.println("\nReport generated successfully!");
}
} while (!isToOverwrite);
}
if (menuChoice.equalsIgnoreCase("x")) {
System.out.println("\nProgram has been terminated.");
System.exit(0);
}
} while (!menuChoice.equalsIgnoreCase("x"));
}
public static boolean isValidDivision(String divisionRead) {
if (divisionRead.equalsIgnoreCase("N") ||
divisionRead.equalsIgnoreCase("S") ||
divisionRead.equalsIgnoreCase("E") ||
divisionRead.equalsIgnoreCase("W")) {
return true;
}
else {
return false;
}
}
public static boolean isValidQuarter(int qtrRead) {
if (qtrRead >= 1 && qtrRead <= 4) {
return true;
}
else {
return false;
}
}
public static boolean isValidSales(double salesRead) {
if (salesRead >= 0) {
return true;
}
else {
return false;
}
}
}
I'm not asking for specific code, but just some guidance on how I should best proceed. Any advice would be appreciated. I've been stuck on this assignment for over a month!
Example of what the Sales Data file would look like if in a valid format (each would be on its own line...the forum is ignoring the breaks):
N
1
35.50
N
2
26.99
N
3
77.45
N
4
58.30
S
1
132.15
S
2
81.19
S
3
159.06
S
4
83.55
E
1
99.40
E
2
25.39
E
3
50.25
E
4
43.21
W
1
120.89
W
2
392.11
W
3
105.76
W
4
299.95
N
2
66.15
N
3
38.22
N
4
27.66
E
2
135.32
E
3
37.50
E
4
9.10
Since you are not dealing (not want to deal ) with arrays or collections, you can consider creating varianles for North, South, East, West, year end total and the 4 quarters update the values of these variables as you iterate through the file.
Using collection this task would be a little streamlined, but would be prettymuch the same amount of work.
to generate the actual report you can use the String classes for concatenation such as
String.format("%s:%s",strDivision, doubleAmount);
which will give you the info in the desired format, this can be extended to address the rest of the report.
I dont think you need assistance in parsing the file since you say that you are able to validate it.
Create yourself a class that contains the information you need to store (SalesData for example), as you validate each section of the file, create a new SalesData object, seeding it with the values you just read. From there you add it to a java.util.List implementation, like ArrayList.
This will allow you to loop over the list once the file has been read and produce the report.
You other choice would be to maintain a running tally of each division and quarter. This is a little simpler and less memory intensive, but less flexible should the contents of the report change.