Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: True False Bitmap Program

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default True False Bitmap Program

    the program should ask questions from a txt file, input true or false, then write the entered information into a bitmap to be compared with another later.
    package lab3;
     
    import java.io.BufferedOutputStream;
    import java.io.BufferedReader;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.ObjectOutputStream;
    import java.util.ArrayList;
     
    public class CreateTest 
    {
     
    	  public static void main(String[] args) throws IOException
    	  {
    		  int i = 0;
    		  int lengthoftest = 2;
    		  String line;
    		  String userentered;
    		  int torf = 0;
     
    		  FileOutputStream fos = new FileOutputStream("ans.txt");
    		  BufferedOutputStream bos = new BufferedOutputStream(fos);
    		  ObjectOutputStream oos = new ObjectOutputStream(bos);
    		  BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
    		  BufferedReader br = new BufferedReader(new FileReader("quizquestions.txt"));
     
    		  while (i < lengthoftest)
    			{
    				line = br.readLine();
    				System.out.println(line);
    				System.out.println("T or F");
    				userentered = userInput.readLine();
     
    				switch(userentered)
    				{
    				case "t": torf =1;
    				break;
    				case "T": torf =1;
    				break;
    				case "f": torf =0;
    				break;
    				case "F": torf =0;
    				break;
    				default: System.out.println("Try Again");
    				break;
    				}
     
    				oos.write(torf);
     
    	           i++;
     
    	        }
    		  oos.close();
     
    	  }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,717
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: True False Bitmap Program

    did you have a question or problem?
    Please explain.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: True False Bitmap Program

    I am trying to write a 1 or 0 to a bit map and save it to a file. The code below is what I am working with now. My question is what am I doing wrong! I am having problems writing to a bitmap and then being able to check to see if it is putting the right value and amount of values in.



    package lab3;
     
    import java.io.BufferedOutputStream;
    import java.io.BufferedReader;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.ObjectOutputStream;
    import java.util.ArrayList;
    import java.util.BitSet;
     
    public class CreateTest 
    {
     
    	  public static void main(String[] args) throws IOException
    	  {
     
    		int i = 1;
    		  String line;
    		  String userentered;
    		  int torf = 0;
    		  BitSet bitset = new BitSet();
     
    		  FileOutputStream fos = new FileOutputStream("ans.bin");
    		  BufferedOutputStream bos = new BufferedOutputStream(fos);
    		  ObjectOutputStream oos = new ObjectOutputStream(bos);
    		  BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
    		  BufferedReader br = new BufferedReader(new FileReader("quizquestions.txt"));
     
    		  while (i <3)
    			{
    				line = br.readLine();
    				System.out.println(line);
    				System.out.println("T or F");
    				userentered = userInput.readLine();
     
    				switch(userentered)
    				{
    				case "t": torf =1;
    				break;
    				case "T": torf =1;
    				break;
    				case "f": torf =0;
    				break;
    				case "F": torf =0;
    				break;
    				default: System.out.println("Try Again");
    				break;
    				}
     
    				bitset.set(torf);
     
    				i++;
    	        }
    		  System.out.println(bitset);
     
    		  oos.close();
     
    	  }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,717
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: True False Bitmap Program

    what am I doing wrong
    Please explain why you think there is something wrong. Post the program's output and add some comments saying what is wrong.

    What do you mean by "Bitmap"?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: True False Bitmap Program

    I finally think i figured it out after lots of searching and reading... This asks questions, stores a bitmap of true questions. How can I compare this to a bitmap created from a user taking the test? I figure most of it is the same code except comparing the two.

    package lab3;
     
    import java.io.BufferedOutputStream;
    import java.io.BufferedReader;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.ObjectOutputStream;
    import java.util.BitSet;
     
    public class Test 
    {
     
    	  public static void main(String[] args) throws IOException
    	  {
     
     
    		  String line;
    		  String userentered;
    		  BitSet bitset = new BitSet();
     
    		  FileOutputStream fos = new FileOutputStream("userattempt.bin");
    		  BufferedOutputStream bos = new BufferedOutputStream(fos);
    		  ObjectOutputStream oos = new ObjectOutputStream(bos);
    		  BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
    	    	BufferedReader br = new BufferedReader(new FileReader("quizquestions.txt"));
    	   	   System.out.println("Quiz Attempt");
     
    		  for (int i= 1; i <8; i++)
    			{
    				line = br.readLine();
    				System.out.println(line);
    				System.out.println("T or F");
    				userentered = userInput.readLine();
     
    				switch(userentered)
    				{
    				case "t": bitset.set(i, true);
    				break;
    				case "T":;bitset.set(i, true);
    				break;
    				case "f":bitset.set(i, false);
    				break;
    				case "F": bitset.set(i, false);
    				break;
    				default: System.out.println("Try Again");
    				break;
    				}
     
    	        }
     
    		  oos.writeObject(bitset);
    		  oos.close();
     
    	  }
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,717
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: True False Bitmap Program

    Please post the code where you are trying to compare.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Nov 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: True False Bitmap Program

    I cannot figure out how to use the created answer bitset with the one created when the user is taking the test. I put some random data into where it should use the old bitset to see if it worked at all. The code will show the questions the user entered correctly.

    1. How do I read in my bitset file to use it in this program?
    2. How can I count how many times the users guess matches the answer key?

    Thank you so much for helping so far! I have been back and forth on here and Dream in Code for about a week with random questions and I have learned so much more than in class.
     
    		  oos.writeObject(bitsetguess);
    		  oos.close();
     
    		  bitsetanswer.and(bitsetguess);
    		  System.out.println("Your Correct Questions Are: ");
    		  System.out.println(bitsetanswer);
    		  System.out.println("Your Grade Is: ");
    		 // System.out.println()

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,717
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: True False Bitmap Program

    How do I read in my bitset file
    The write method and its class that you used to write the object has a corresponding method and class for reading in what was written.
    How can I count how many times the users guess matches the answer key?
    Use a counter. When the answers match, add one to the counter.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Nov 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: True False Bitmap Program

    Thank you! Just realized the my main problem was not storing the file into a variable ... I needed to use bitsetanswer=oosa.readObject(); it just seemed that it could not just be a simple readObject(). As far as i can tell this works great, could be more efficient but it gets the job done

    package lab3;
     
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.util.BitSet;
     
    public class Test 
    {
     
    	  public static void main(String[] args) throws IOException
    	  {
     
     
    		  String line;
    		  String userentered;
    		  int counter =0;
    		  double testscore;
    		  BitSet bitsetguess = new BitSet();
    		  Object bitsetanswer = new BitSet();
    		  //	bitsetanswer.set(1);
    		  //	bitsetanswer.set(2);
     
     
    		  FileOutputStream fos = new FileOutputStream("guess.bin");
    		  BufferedOutputStream bos = new BufferedOutputStream(fos);
    		  ObjectOutputStream oos = new ObjectOutputStream(bos);
    		  BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
     
    		  FileInputStream fosa = new FileInputStream("ans.bin");
    		  BufferedInputStream bosa = new BufferedInputStream(fosa);
    		  ObjectInputStream oosa = new ObjectInputStream(bosa);
     
    		  @SuppressWarnings("resource")
    		  BufferedReader br = new BufferedReader(new FileReader("quizquestions.txt"));
     
    	    	System.out.println("Take the Quiz");
     
     
     
    		  for (int i= 1; i != 26; i++)
    			{
    				line = br.readLine();
    				System.out.println(line);
    				System.out.println("T or F");
     
    				userentered = userInput.readLine();
     
    				switch(userentered)
    				{
    				case "t": bitsetguess.set(i, true);
    				break;
    				case "T":;bitsetguess.set(i, true);
    				break;
    				case "f":bitsetguess.set(i, false);
    				break;
    				case "F": bitsetguess.set(i, false);
    				break;
    				default: System.out.println("Try Again");
    				break;
    				}
     
    	        }
     
    		  oos.writeObject(bitsetguess);
    		  oos.close();
     
     
    		  try 
    		  {
    				bitsetanswer = oosa.readObject();
    				 for (int a =1; a != 26; a++)
    				  {
    						if (((BitSet) bitsetanswer).get(a) == bitsetguess.get(a))
    						{
    							counter = counter +1;
    						}
    				  }
    		  }
    		  catch (ClassNotFoundException e) 
    		  {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    		   }
     
    		  oosa.close();
     
    		  System.out.println("Your Grade Is: ");
    		  testscore=100*(((double)counter)/25);
    		  System.out.println(testscore +"%");
    	  }
     
     
    	}

Similar Threads

  1. Objects, classes, lists true and false HELP!
    By klskl in forum Object Oriented Programming
    Replies: 4
    Last Post: October 18th, 2013, 07:05 AM
  2. Replies: 0
    Last Post: January 22nd, 2013, 09:15 PM
  3. [SOLVED] Answer always false.
    By tyb97 in forum Loops & Control Statements
    Replies: 5
    Last Post: October 7th, 2011, 10:56 AM
  4. would this expression be true or false??
    By robertsbd in forum Java Theory & Questions
    Replies: 1
    Last Post: October 24th, 2010, 10:00 PM
  5. why is this statement evaluating false??
    By humdinger in forum Object Oriented Programming
    Replies: 2
    Last Post: November 3rd, 2009, 03:28 PM