BH09
BH09

Reputation: 11

Creating a File that will read a txt file

So i am currently trying to figure out how my code can read my txt file. My objective is to prompt what ever i have for initialization, then ask me to type a number but 0 to get a message that i have written on my txt file. Then finally by finishing by typing 0 and getting what ever message i have for the finish. I have read online articles but i still have trouble. This is what i have so far:

import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import java.util.ArrayList;     

public class FortuneFile
{ 
   static Scanner keyboard;   
   static int inputLine;
   static Scanner inputFile;   
   static boolean done;       


   public static void main(String[] args) throws Exception
   {
      initialization(); 
      while (inputFile.hasNext())
        while (!done)           
      {
         mainLoop();    
      }
      finish();               
   }

   public static void initialization() throws Exception
   { 
      Scanner inputFile = new Scanner(new File("FortuneCookie.txt"));
      keyboard = new Scanner(System.in);                      
      done = false;
      System.out.println("");     
      System.out.println("Welcome to the Command Box FortuneCookie game!");
      System.out.println("====================================================================");
      System.out.println("Dare to try your luck?... You could be a Winner or a Looser!");
      System.out.println("");     
      System.out.println("Enter \"0\" if you are scared, or if you are brave, try any number: ");
      System.out.println("====================================================================");
   }

   public static void mainLoop() throws Exception
   {

         inputLine = inputFile.nextInt();
         i++;        
            if (keyboard.equals("0"))       
         {
            done = true;                  
         }
         else
         {
             {
                System.out.println("");
             }
             System.out.print("Care to try again? ");
             System.out.println("");             
         } 
   }

   public static void finish()
   {
      System.out.println("====================================================================");
      System.out.println("Thanks for playing along. I hope you are not traumatised!");
   }
}

Thank you!! :)

Upvotes: 0

Views: 100

Answers (4)

Ravindra babu
Ravindra babu

Reputation: 38910

keyboard is a Scanner object. It can't be compared like String with equals("0").

When you are reading nextInt(), store the value in int variable and compare thar value is 0 or not to end the loop.

Upvotes: 1

Mohammad
Mohammad

Reputation: 6138

Try this:

import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import java.util.ArrayList;     

public class FortuneFile
{ 
   static Scanner keyboard;   
   static int inputLine;
   static Scanner inputFile;   
   static boolean done;
   static String myMessage;


   public static void main(String[] args) throws Exception
   {
      initialization(); 

      //create an object that reads integers:
      Scanner Cin = new Scanner(System.in); 
      inputLine = Cin.nextInt();

      while(inputLine != 0){
             System.out.println("");
             System.out.print("Care to try again? ");
             System.out.println("");
             System.out.println("Enter another integer: ");
             inputLine = Cin.nextInt();
      }

      System.out.println(myMessage);

      finish();               
   }

   public static void initialization() throws Exception
   { 
      Scanner inputFile = new Scanner(new File("C:/FortuneCookie.txt"));
      //keyboard = new Scanner(System.in);  
      //Read the first line
      myMessage = inputFile.nextLine();
      //System.out.println(myMessage);
      done = false;
      System.out.println("");     
      System.out.println("Welcome to the Command Box FortuneCookie game!");
      System.out.println("====================================================================");
      System.out.println("Dare to try your luck?... You could be a Winner or a Looser!");
      System.out.println("");     
      System.out.println("Enter \"0\" if you are scared, or if you are brave, try any number: ");
      System.out.println("====================================================================");
   }

   public static void finish()
   {
      System.out.println("====================================================================");
      System.out.println("Thanks for playing along. I hope you are not traumatised!");
   }
}

You can remove my main loop and put it in another procedure if you want. I am checking the input with 0. If it is zero, it is the end. If not, it will remain in the loop. I put my file in drive c. You can change your address to your file location. This is the result:

The result

And this was the body of my text file:

The text file

I hope this solves your problem. Please let me know if you have any other question.

Upvotes: 0

Ron7
Ron7

Reputation: 395

You are using keyboard.equals() which is wrong as keyboard is a Scanner object and not a String. You should use keyboard.nextLine() to get the input from the user and store this in a String. So you'll have something like, String holder = keyboard.nextLine();

An easier way to to this would be to read in the integer using something like, int holder = keyboard.nextInt();

and then compare this integer with 0 using ==

Check out this link for more information on Scanners in Java; http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

Also in initialize() you're making a new local Scanner inputFile that you use in the method. The problem with this is that when you make calls to inputFile outside initialize() you'll run into problems as inputFile outside initialize() is not defined to operate on the file you're using. This is a scope resolution issue. You'd just want to do, inputFile = new Scanner(new File("FortuneCookie.txt"));

Also make sure that this text file is in the same directory as your project's, otherwise you'll have to describe the complete path. Make sure you understand your scope resolution as this can cause various problems.

I hope this was helpful!

Good luck!

Upvotes: 0

kenshinji
kenshinji

Reputation: 2081

There are some problems in your code:

  1. First, You missed declaring i in your mainLoop() method so that it can't compile successfully.
  2. Second, keyboard is a Scanner object which can't be compared with String object 0 by equals()

Upvotes: 1

Related Questions