473,507 Members | 6,295 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why does the nested while loop fail to execute in my do while loop?

9 New Member
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
Expand|Select|Wrap|Line Numbers
  1.  try{
  2.             //read in file
  3.             //input streams
  4.             FileInputStream fis = new FileInputStream("Book-text.dat");
  5.             BufferedInputStream bis = new BufferedInputStream(fis);
  6.             DataInputStream dis = new DataInputStream(bis);
  7.             System.out.println("From Book-Text.dat");
  8.  
  9.              // dis.available() returns 0 if the file does not have more lines.
  10.             System.out.println("Entering while");
  11.             //not entering while statement Why!
  12.  
  13.             while(dis.available() != 0)
  14.             {
  15.             // this statement reads the line from the file and print it to
  16.               // the console.
  17.                 System.out.println("In while");
  18.                 System.out.println(dis.readLine());
  19.             }
  20.  
  21.             // dispose all the resources after using them.
  22.             System.out.println("Exit Whie");
  23.             fis.close();
  24.             bis.close();
  25.             dis.close();
  26.             System.out.println("Close Streams");
  27.         } catch (FileNotFoundException e) {
  28.             e.printStackTrace();
  29.         } catch (IOException e) {
  30.             e.printStackTrace();
  31.         }
Feb 7 '11 #1
2 1977
Dheeraj Joshi
1,123 Recognized Expert Top Contributor
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.
Expand|Select|Wrap|Line Numbers
  1. HI
  2. how
  3. are
  4. you
  5. .
  6.  
The output was as expected.
Expand|Select|Wrap|Line Numbers
  1. From Book-Text.dat
  2. Entering while
  3. In while
  4. HI
  5. In while
  6. how
  7. In while
  8. are
  9. In while
  10. you
  11. In while
  12. .
  13. Exit Whie
  14. Close Streams
  15.  
I guess there is some issue in the input file.

Regards
Dheeraj Joshi
Feb 8 '11 #2
Kakashi
9 New Member
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:
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.Scanner;
  3.  
  4.  
  5.  
  6. /**
  7.  * @author Matthew Millar
  8.  * NetBeans 6.7
  9.  * Windows Vista 64bit
  10.  * January 30 2011
  11.  */
  12. //This program is to test working with object and text data using streams and files
  13. public class Project1 {
  14.     public static void main(String[] args) throws IOException
  15.     {
  16.         FileWriter file = new FileWriter("Book-text.dat");
  17.         BufferedWriter bwrite = new BufferedWriter(file);
  18.         ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("Book-objects.dat"));
  19.  
  20.  
  21.         //three books to test prpogram with
  22.         Book book1 = new Book("King Gus","Gus",450,1250.25);
  23.         Book book2 = new Book("Ninja","Kim",780,55.62);
  24.  
  25.  
  26.         //the choice that will be entered by the user
  27.         int input = 0;
  28.         do{
  29.              //ask for user input
  30.             System.out.println("Please pick one of the following options.");
  31.             System.out.println("Press 1 to Populate");
  32.             System.out.println("Press 2 to View books");
  33.             System.out.println("Press 3 to Search");
  34.             System.out.println("Press 4 to Insert");
  35.             System.out.println("Press 5 to Quit the Program");
  36.  
  37.  
  38.  
  39.  
  40.        Scanner sc = new Scanner(System.in);
  41.        input = sc.nextInt();
  42.  
  43.  
  44.         //ask the user for their choice
  45.         System.out.println("Please pick a command to preform.");
  46.         if(input ==1)
  47.         {
  48.             //populate
  49.             System.out.println("Populating files");
  50.             book1.writeTextFile(book1, file, bwrite);
  51.             book1.writeObjectFile(book1, outputStream);
  52.             book2.writeObjectFile(book2, outputStream);
  53.             book2.writeTextFile(book2, file, bwrite);
  54.         }//end of populate if
  55.  
  56.  
  57.         if(input == 2)
  58.         {
  59.             try{
  60.             //read in file
  61.             //input streams
  62.             FileInputStream fis = new FileInputStream("Book-text.dat");
  63.             BufferedInputStream bis = new BufferedInputStream(fis);
  64.             DataInputStream dis = new DataInputStream(bis);
  65.             System.out.println("From Book-Text.dat");
  66.  
  67.              // dis.available() returns 0 if the file does not have more lines.
  68.             System.out.println("Entering while");
  69.             //not entering while statement Why!
  70.  
  71.             while(dis.available() != 0)
  72.             {
  73.             // this statement reads the line from the file and print it to
  74.               // the console.
  75.                 System.out.println("In while");
  76.                 System.out.println(dis.readLine());
  77.             }
  78.  
  79.             // dispose all the resources after using them.
  80.             System.out.println("Exit Whie");
  81.             fis.close();
  82.             bis.close();
  83.             dis.close();
  84.             System.out.println("Close Streams");
  85.         } catch (FileNotFoundException e) {
  86.             e.printStackTrace();
  87.         } catch (IOException e) {
  88.             e.printStackTrace();
  89.         }
  90.  
  91.  
  92.             //read object file
  93.             //this works fine
  94.  
  95.          try {
  96.  
  97.             //Construct the ObjectInputStream object
  98.             ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("Book-objects.dat"));
  99.             Object obj = null;
  100.             System.out.println("From Book-Object.dat");
  101.  
  102.             while ((obj = inputStream.readObject()) != null) {
  103.  
  104.                 if (obj instanceof Book) {
  105.  
  106.  
  107.                     System.out.println(((Book)obj).toString());
  108.                 }
  109.  
  110.             }
  111.              inputStream.close();
  112.  
  113.  
  114.         } catch (EOFException ex) { //This exception will be caught when EOF is reached
  115.             System.out.println("End of file reached.");
  116.         } catch (ClassNotFoundException ex) {
  117.             ex.printStackTrace();
  118.         } catch (FileNotFoundException ex) {
  119.             ex.printStackTrace();
  120.         } catch (IOException ex) {
  121.             ex.printStackTrace();
  122.         }
  123.  
  124.         }//end of view if
  125.  
  126.         if(input == 3)
  127.         {
  128.             //search
  129.         }
  130.  
  131.         if(input == 4)
  132.         {
  133.             //insert
  134.  
  135.             //get user input for book information
  136.             Scanner in = new Scanner(System.in);
  137.             System.out.println("Enter Title");
  138.             String t = in.nextLine();
  139.             System.out.println("Enter Author name");
  140.             String a = in.nextLine();
  141.             System.out.println("Enter number of pages");
  142.             int n = in.nextInt();
  143.             System.out.println("Enter Price");
  144.             double p = in.nextDouble();
  145.  
  146.             //crat new book with information
  147.             Book newBook = new Book(t,a,n,p);
  148.  
  149.             //then auto add it to the files
  150.             newBook.writeObjectFile(newBook, outputStream);
  151.             newBook.writeTextFile(newBook, file, bwrite);
  152.  
  153.  
  154.  
  155.  
  156.         }
  157.  
  158.  
  159.         }while(input != 5);
  160.         System.out.println("Closeing the program Bye");
  161.         //end of do while loop
  162.         bwrite.close();
  163.         file.close();
  164.         outputStream.flush();
  165.         outputStream.close();
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.     }
  176.  
  177. }
  178.  
  179. //books class
  180. class Book implements Serializable
  181. {
  182.     //variables used
  183.     private String title;
  184.     private String author;
  185.     private int numPage;
  186.     private double price;
  187.    //construtor
  188.     //defult no param constructor
  189.     Book(){}
  190.     Book(String t, String a, int n, double p)
  191.         {
  192.             title = t;
  193.             author = a;
  194.             numPage = n;
  195.             price = p;
  196.         }
  197.  
  198.     //get methods
  199.         public String getTitle()
  200.         {
  201.             return title;
  202.         }
  203.         public String getAuthor()
  204.         {
  205.             return author;
  206.         }
  207.         public int getPage()
  208.         {
  209.             return numPage;
  210.         }
  211.         public double getPrice()
  212.         {
  213.             return price;
  214.         }
  215.  
  216.         //set methods
  217.         public void setTitle(String t)
  218.         {
  219.             this.title = t;
  220.         }
  221.  
  222.         public void setAuthor(String a)
  223.         {
  224.             this.author = a;
  225.         }
  226.  
  227.         public void setPrice(double p)
  228.         {
  229.             this.price = p;
  230.         }
  231.  
  232.         public void setNumPage(int n)
  233.         {
  234.             this.numPage = n;
  235.         }
  236.  
  237.  
  238.  
  239.         public String toString()
  240.         {
  241.              return getClass().getName() + "  Title: " + title + ",  Author: " + author + ",  Number of Pages: " + numPage
  242.             + ",  Price: " + price + "\n";
  243.         }
  244.  
  245.  
  246.         //methods for writing files
  247.         public void writeObjectFile(Object obj, ObjectOutputStream outStream)
  248.         {
  249.         //write object to file
  250.         try{
  251.  
  252.          outStream.writeObject(obj);
  253.  
  254.          System.out.println("Object wrote " + obj);
  255.  
  256.         //clear the output stream
  257.  
  258.         }catch(IOException ex) {
  259.             ex.printStackTrace();
  260.         }
  261.         }
  262.  
  263.         public void writeTextFile(Object obj, FileWriter file, BufferedWriter out)
  264.         {
  265.             try{
  266.              // Create file
  267.             String textObj = obj.toString();
  268.             System.out.println("Write Text File " + obj);
  269.             out.write(textObj);
  270.              //Close the output stream
  271.  
  272.              }catch (Exception e){//Catch exception if any
  273.                  System.err.println("Error: " + e.getMessage());
  274.             }
  275.         }
  276.  
  277.        public void creatBook()
  278.        {
  279.             System.out.println("Enter the Book Information");
  280.             System.out.println("Enter the Title");
  281.             System.out.println("Enter the Author");
  282.             System.out.println("Enter the Number of Pages");
  283.             System.out.println("Enter the Price");
  284.  
  285.        }
  286.  
  287.  
  288. }//end of book class
  289.  
  290.  
Feb 8 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

15
9127
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...
1
12224
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...
14
19395
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...
9
3929
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...
6
10790
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.
11
25494
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...
3
4465
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...
2
2420
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;
8
5872
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...
2
2339
by: Cebile Maseko | last post by:
int count = 0; while (count++ <50) { Cout << "Count is" << count << endl; }
0
7308
Oralloy
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,...
0
7371
jinu1996
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...
0
7479
tracyyun
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...
0
5617
agi2029
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,...
1
5037
isladogs
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...
0
4702
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...
0
3178
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1534
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 ...
0
410
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.