My problem is when I try to run the while loop that is nested in the do while loop it fails to even enter into it. Am I missing something here, I do not have any errors or anything.
The try catch block worked fine when I was testing it in the main method and not in the do while loop. What has changed? It is nested like this
do{
//Some user input code here
if(statement)
{
try{
code here
}catch other stuff
}
and here is the code - try{
-
//read in file
-
//input streams
-
FileInputStream fis = new FileInputStream("Book-text.dat");
-
BufferedInputStream bis = new BufferedInputStream(fis);
-
DataInputStream dis = new DataInputStream(bis);
-
System.out.println("From Book-Text.dat");
-
-
// dis.available() returns 0 if the file does not have more lines.
-
System.out.println("Entering while");
-
//not entering while statement Why!
-
-
while(dis.available() != 0)
-
{
-
// this statement reads the line from the file and print it to
-
// the console.
-
System.out.println("In while");
-
System.out.println(dis.readLine());
-
}
-
-
// dispose all the resources after using them.
-
System.out.println("Exit Whie");
-
fis.close();
-
bis.close();
-
dis.close();
-
System.out.println("Close Streams");
-
} catch (FileNotFoundException e) {
-
e.printStackTrace();
-
} catch (IOException e) {
-
e.printStackTrace();
-
}
2 1977
There is no issue in the code.Just to make sure i ran your code, and it is working properly for me.
I created a test file as input.
The output was as expected. -
From Book-Text.dat
-
Entering while
-
In while
-
HI
-
In while
-
how
-
In while
-
are
-
In while
-
you
-
In while
-
.
-
Exit Whie
-
Close Streams
-
I guess there is some issue in the input file.
Regards
Dheeraj Joshi
Thanks for the help, I have to think that the file is the problem too
Well the code does write to the file fine and it does exsit, I can open it with notepad and read it. So you think that the code is not getting the right file. Mybe if I gave the whole program that might help: - import java.io.*;
-
import java.util.Scanner;
-
-
-
-
/**
-
* @author Matthew Millar
-
* NetBeans 6.7
-
* Windows Vista 64bit
-
* January 30 2011
-
*/
-
//This program is to test working with object and text data using streams and files
-
public class Project1 {
-
public static void main(String[] args) throws IOException
-
{
-
FileWriter file = new FileWriter("Book-text.dat");
-
BufferedWriter bwrite = new BufferedWriter(file);
-
ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("Book-objects.dat"));
-
-
-
//three books to test prpogram with
-
Book book1 = new Book("King Gus","Gus",450,1250.25);
-
Book book2 = new Book("Ninja","Kim",780,55.62);
-
-
-
//the choice that will be entered by the user
-
int input = 0;
-
do{
-
//ask for user input
-
System.out.println("Please pick one of the following options.");
-
System.out.println("Press 1 to Populate");
-
System.out.println("Press 2 to View books");
-
System.out.println("Press 3 to Search");
-
System.out.println("Press 4 to Insert");
-
System.out.println("Press 5 to Quit the Program");
-
-
-
-
-
Scanner sc = new Scanner(System.in);
-
input = sc.nextInt();
-
-
-
//ask the user for their choice
-
System.out.println("Please pick a command to preform.");
-
if(input ==1)
-
{
-
//populate
-
System.out.println("Populating files");
-
book1.writeTextFile(book1, file, bwrite);
-
book1.writeObjectFile(book1, outputStream);
-
book2.writeObjectFile(book2, outputStream);
-
book2.writeTextFile(book2, file, bwrite);
-
}//end of populate if
-
-
-
if(input == 2)
-
{
-
try{
-
//read in file
-
//input streams
-
FileInputStream fis = new FileInputStream("Book-text.dat");
-
BufferedInputStream bis = new BufferedInputStream(fis);
-
DataInputStream dis = new DataInputStream(bis);
-
System.out.println("From Book-Text.dat");
-
-
// dis.available() returns 0 if the file does not have more lines.
-
System.out.println("Entering while");
-
//not entering while statement Why!
-
-
while(dis.available() != 0)
-
{
-
// this statement reads the line from the file and print it to
-
// the console.
-
System.out.println("In while");
-
System.out.println(dis.readLine());
-
}
-
-
// dispose all the resources after using them.
-
System.out.println("Exit Whie");
-
fis.close();
-
bis.close();
-
dis.close();
-
System.out.println("Close Streams");
-
} catch (FileNotFoundException e) {
-
e.printStackTrace();
-
} catch (IOException e) {
-
e.printStackTrace();
-
}
-
-
-
//read object file
-
//this works fine
-
-
try {
-
-
//Construct the ObjectInputStream object
-
ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("Book-objects.dat"));
-
Object obj = null;
-
System.out.println("From Book-Object.dat");
-
-
while ((obj = inputStream.readObject()) != null) {
-
-
if (obj instanceof Book) {
-
-
-
System.out.println(((Book)obj).toString());
-
}
-
-
}
-
inputStream.close();
-
-
-
} catch (EOFException ex) { //This exception will be caught when EOF is reached
-
System.out.println("End of file reached.");
-
} catch (ClassNotFoundException ex) {
-
ex.printStackTrace();
-
} catch (FileNotFoundException ex) {
-
ex.printStackTrace();
-
} catch (IOException ex) {
-
ex.printStackTrace();
-
}
-
-
}//end of view if
-
-
if(input == 3)
-
{
-
//search
-
}
-
-
if(input == 4)
-
{
-
//insert
-
-
//get user input for book information
-
Scanner in = new Scanner(System.in);
-
System.out.println("Enter Title");
-
String t = in.nextLine();
-
System.out.println("Enter Author name");
-
String a = in.nextLine();
-
System.out.println("Enter number of pages");
-
int n = in.nextInt();
-
System.out.println("Enter Price");
-
double p = in.nextDouble();
-
-
//crat new book with information
-
Book newBook = new Book(t,a,n,p);
-
-
//then auto add it to the files
-
newBook.writeObjectFile(newBook, outputStream);
-
newBook.writeTextFile(newBook, file, bwrite);
-
-
-
-
-
}
-
-
-
}while(input != 5);
-
System.out.println("Closeing the program Bye");
-
//end of do while loop
-
bwrite.close();
-
file.close();
-
outputStream.flush();
-
outputStream.close();
-
-
-
-
-
-
-
-
-
-
}
-
-
}
-
-
//books class
-
class Book implements Serializable
-
{
-
//variables used
-
private String title;
-
private String author;
-
private int numPage;
-
private double price;
-
//construtor
-
//defult no param constructor
-
Book(){}
-
Book(String t, String a, int n, double p)
-
{
-
title = t;
-
author = a;
-
numPage = n;
-
price = p;
-
}
-
-
//get methods
-
public String getTitle()
-
{
-
return title;
-
}
-
public String getAuthor()
-
{
-
return author;
-
}
-
public int getPage()
-
{
-
return numPage;
-
}
-
public double getPrice()
-
{
-
return price;
-
}
-
-
//set methods
-
public void setTitle(String t)
-
{
-
this.title = t;
-
}
-
-
public void setAuthor(String a)
-
{
-
this.author = a;
-
}
-
-
public void setPrice(double p)
-
{
-
this.price = p;
-
}
-
-
public void setNumPage(int n)
-
{
-
this.numPage = n;
-
}
-
-
-
-
public String toString()
-
{
-
return getClass().getName() + " Title: " + title + ", Author: " + author + ", Number of Pages: " + numPage
-
+ ", Price: " + price + "\n";
-
}
-
-
-
//methods for writing files
-
public void writeObjectFile(Object obj, ObjectOutputStream outStream)
-
{
-
//write object to file
-
try{
-
-
outStream.writeObject(obj);
-
-
System.out.println("Object wrote " + obj);
-
-
//clear the output stream
-
-
}catch(IOException ex) {
-
ex.printStackTrace();
-
}
-
}
-
-
public void writeTextFile(Object obj, FileWriter file, BufferedWriter out)
-
{
-
try{
-
// Create file
-
String textObj = obj.toString();
-
System.out.println("Write Text File " + obj);
-
out.write(textObj);
-
//Close the output stream
-
-
}catch (Exception e){//Catch exception if any
-
System.err.println("Error: " + e.getMessage());
-
}
-
}
-
-
public void creatBook()
-
{
-
System.out.println("Enter the Book Information");
-
System.out.println("Enter the Title");
-
System.out.println("Enter the Author");
-
System.out.println("Enter the Number of Pages");
-
System.out.println("Enter the Price");
-
-
}
-
-
-
}//end of book class
-
-
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: PagCal |
last post by:
Is this language missing the functionality of a C/C++ 'continue' statement?
For example:
While NOT isEof()
If condition
' a C or C++ continue would work here
' but we are forced to use a...
|
by: rlm |
last post by:
When I attempt to manage Transactions in a WHILE LOOP @@TRANCOUNT is
off. I obviously do not understand error handling as I should. In the
loop below where does the point of execution move to after...
|
by: Jan Schmidt |
last post by:
Hi,
in a nested do-while-loop structure I would like to "continue" the outer
loop. With goto this should be no problem in while-loops. However, for
do-while I cannot get it to work (without a...
|
by: Cybex |
last post by:
I am trying to get this to work but when ever I enter an proper
integer it just hangs. The Switch default seems to catch the improper
integers but the right ones are not triggering the way I...
|
by: mgcclx |
last post by:
For loop and while loop. which one is faster?
I see many articles fighting over it and different people come up with
different results.
| |
by: Rene |
last post by:
Quick question, what is the point for forcing the semicolon at the end of
the while statement? See example below:
x = 0;
do
{
x = x + 1;
}while (x < 3);
What's the point of having the...
|
by: numlock00 |
last post by:
I have a nested 'while' loop that won't repeat, no matter how many times the outer loop repeats. The outer loop reads through an array of elements; the inner loop Ithe 'while' loop) is supposed to...
|
by: nirav11 |
last post by:
# include <iostream>
# include <fstream>
# include <iomanip>
# include <cstdlib> // needed for exit ()
using namespace std;
int main()
{
ifstream inFile;
ofstream outFile;
|
by: Shilpa Sethi |
last post by:
I wanted to know which one is better in performance between a for loop and a while loop in terms of:
1. memory requirement
2. execution speed
Also wanted to know since recursion functions are...
|
by: Cebile Maseko |
last post by:
int count = 0;
while (count++ <50)
{
Cout << "Count is" << count << endl;
}
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |