7 Replies - 234 Views - Last Post: 06 October 2012 - 08:28 PM Rate Topic: -----

#1 iburres  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 05-September 12

Qestionnaire and FileReader

Posted 06 October 2012 - 02:47 PM

I am creating a program that gives a user the option to either load a saved Q&A or fill out their own survey. I have the first part down fine. If the user types in yes, regardless of case, the File is read into the program and displayed to the user; however, I cannot seem to differentiate the users option to fill out their own survey. I am assuming I need a "break;" statement, but if I implement it in the while loop, It still prints out the first question in the saved file. I simply want the user to be able to type "no," which should bring up the myMenu() method. I'm pretty sure I can handle it from there.


import java.util.Scanner;
import java.io.*;

public class MySurvey {
	private static Scanner option;
	private static String choice;


	public static void main(String[] args) throws IOException {
		displayMenu();
		
		
	}
	
	public static void displayMenu() {
		System.out.print("Would you like to load a previously saved file with Questions and Answers?");
		
		option = new Scanner(System.in);
		choice = option.nextLine();
		choice.toLowerCase();
		
		while(choice == "yes");
		try {
		
			File myFile = new File("C:/Users/Infosec3000/My Documents/Q&A.txt");
			FileReader fileReader = new FileReader(myFile);
			
			BufferedReader reader = new BufferedReader(fileReader);
			
			String line = null;
			
			while( (line = reader.readLine()) != null) {
				System.out.println(line);
			}
			reader.close();
			
		}catch(Exception ex) {
			ex.printStackTrace();

		
		
		}
		while(choice == "no"); {
			myMenu();
		}
}
	
	public static void myMenu() {
		System.out.print("1. What is your favorite color?");
		option = new Scanner(System.in);
		choice = option.nextLine();
		choice.toLowerCase();
		System.out.print("" + option);
		
	}
	
}
	






Is This A Good Question/Topic? 0
  • +

Replies To: Qestionnaire and FileReader

#2 Ytry  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 120
  • Joined: 25-July 12

Re: Qestionnaire and FileReader

Posted 06 October 2012 - 03:11 PM

In order to differentiate between the two choices I would do an if else like so
if(choice.matches("yes")){
// your code to load already made q/a here
}else if(choice.matches("no")){
// your code to make them take survey here
}else
System.out.println("Invalid choice, please enter yes, or no.");
// code to set choice equal to what ever they enter.



I hope this helps

This post has been edited by Ytry: 06 October 2012 - 03:15 PM

Was This Post Helpful? 0
  • +
  • -

#3 iburres  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 05-September 12

Re: Qestionnaire and FileReader

Posted 06 October 2012 - 03:23 PM

I tried an else if but it would not compile. That's what is confusing me.
Was This Post Helpful? 0
  • +
  • -

#4 Ytry  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 120
  • Joined: 25-July 12

Re: Qestionnaire and FileReader

Posted 06 October 2012 - 03:29 PM

Why wouldn't it compile, what errors are you getting with that if else?
Was This Post Helpful? 0
  • +
  • -

#5 iburres  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 05-September 12

Re: Qestionnaire and FileReader

Posted 06 October 2012 - 03:45 PM

Well, this:


package project1;
import java.util.Scanner;
import java.io.*;

public class MySurvey {
	private static Scanner option;
	private static String choice;


	public static void main(String[] args) throws IOException {
		displayMenu();
		
		
	}
	
	public static void displayMenu() {
		System.out.print("Would you like to load a previously saved file with Questions and Answers?");
		
		option = new Scanner(System.in);
		choice = option.nextLine();
		choice.toLowerCase();
		
		while(choice == "yes");
		try {
		
			File myFile = new File("C:/Users/Infosec3000/My Documents/Q&A.txt");
			FileReader fileReader = new FileReader(myFile);
			
			BufferedReader reader = new BufferedReader(fileReader);
			
			String line = null;
			
			while( (line = reader.readLine()) != null) {
				System.out.println(line);
			}
			reader.close();
			
		}catch(Exception ex) {
			ex.printStackTrace();

		
		
		}
		else if(choice == "no"); {
			myMenu();
		}
}
	
	public static void myMenu() {
		System.out.print("1. What is your favorite color?");
		option = new Scanner(System.in);
		choice = option.nextLine();
		choice.toLowerCase();
		System.out.print("" + option);
		
	}
	
}



"delete this token.. No matter where I put it, I can;t get it to work. So I gave up on the option and tried something else.

Sorry, this:

import java.util.Scanner;
import java.io.*;

public class MySurvey {
	private static Scanner option;
	private static String choice;


	public static void main(String[] args) throws IOException {
		displayMenu();
		
		
	}
	
	public static void displayMenu() {
		System.out.print("Would you like to load a previously saved file with Questions and Answers?");
		
		option = new Scanner(System.in);
		choice = option.nextLine();
		choice.toLowerCase();
		
		if(choice == "yes");
		try {
		
			File myFile = new File("C:/Users/Infosec3000/My Documents/Q&A.txt");
			FileReader fileReader = new FileReader(myFile);
			
			BufferedReader reader = new BufferedReader(fileReader);
			
			String line = null;
			
			while( (line = reader.readLine()) != null) {
				System.out.println(line);
			}
			reader.close();
			
		}catch(Exception ex) {
			ex.printStackTrace();

		
		
		}
		else if(choice == "no"); {
			myMenu();
		}
}
	
	public static void myMenu() {
		System.out.print("1. What is your favorite color?");
		option = new Scanner(System.in);
		choice = option.nextLine();
		choice.toLowerCase();
		System.out.print("" + option);
		
	}
	
}
	







Oh my goodness.... I used a semicolon. Now that's a heck of a syntax error.

I take that back. Now it won't execute at all. Urgh...:)


import java.util.Scanner;
import java.io.*;

public class MySurvey {
	private static Scanner option;
	private static String choice;


	public static void main(String[] args) throws IOException {
		displayMenu();
		
		
	}
	
	public static void displayMenu() {
		System.out.print("Would you like to load a previously saved file with Questions and Answers?");
		
		option = new Scanner(System.in);
		choice = option.nextLine();
		choice.toLowerCase();
		
		if(choice == "yes"){
		try {
		
			File myFile = new File("C:/Users/Infosec3000/My Documents/Q&A.txt");
			FileReader fileReader = new FileReader(myFile);
			
			BufferedReader reader = new BufferedReader(fileReader);
			
			String line = null;
			
			while( (line = reader.readLine()) != null) {
				System.out.println(line);
			}
			reader.close();
			
		}catch(Exception ex) {
			ex.printStackTrace();

		
		
		}
		}else if(choice == "no") {
			myMenu();
		}
}
	
	public static void myMenu() {
		System.out.print("1. What is your favorite color?");
		option = new Scanner(System.in);
		choice = option.nextLine();
		choice.toLowerCase();
		System.out.print("" + option);
		
	}
	
}
	


Was This Post Helpful? 0
  • +
  • -

#6 Ytry  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 120
  • Joined: 25-July 12

Re: Qestionnaire and FileReader

Posted 06 October 2012 - 03:46 PM

replace the semicolon after the first if with a { and put the closing } right before the else if, and remove the semicolon after the else if.
Was This Post Helpful? 0
  • +
  • -

#7 iburres  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 05-September 12

Re: Qestionnaire and FileReader

Posted 06 October 2012 - 04:55 PM

See this is my dilemma. The program at least executed before, even if it printed out more information than I wanted.
Was This Post Helpful? 0
  • +
  • -

#8 iburres  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 05-September 12

Re: Qestionnaire and FileReader

Posted 06 October 2012 - 08:28 PM

Came up with this finally:


package project1;
import java.util.Scanner;
import java.io.*;

public class MySurvey {
	private static Scanner option;
	private static String choice;
	private static String output1;// now I know I could have done this using an array, but I didn't think of it until about 30 minutes before the deadline
	private static String output2;// I'll rewrite it using one if you prefer.  
	private static String output3;
	private static String output4;
	private static String output5;
	private static String output6;
	private static String output7;
	private static String output8;
	
	public static void main(String[] args) throws IOException {
		displayMenu();
		
		
	}
	
	public static void displayMenu() {
		System.out.print("Would you like to load a previously saved file with Questions and Answers?");
		
		option = new Scanner(System.in);
		choice = option.nextLine();
		choice.toLowerCase();
		
		if(choice.equals("yes")) {
		try {
		
			File myFile = new File("C:/Users/Infosec3000/My Documents/Q&A.txt");
			FileReader fileReader = new FileReader(myFile);
			
			BufferedReader reader = new BufferedReader(fileReader);
			
			String line = null;
			
			while( (line = reader.readLine()) != null) {
				System.out.println(line);
				
			}
			reader.close();
			
		}catch(Exception ex) {
			ex.printStackTrace();

		}
		
		}
		else if(choice.equals("no")); {
				myMenu();
		 }
}
	
	public static void myMenu() {
		option = new Scanner(System.in);
		System.out.println("1. What kind of car do you drive?");
		output1 = option.nextLine();
		System.out.println("2. What is your favorite color?");
		output2 = option.nextLine();
		System.out.println("3. Have you ever traveled outside the US?");
		output3 = option.nextLine();
		System.out.println("4. What is your college major?");
		output4 = option.nextLine();
		System.out.println("5. How long do you spend on your labs and assignments?");
		output5 = option.nextLine();
		System.out.println("6. What is your dream job?");
		output6 = option.nextLine();
		System.out.println("7. Where do you live?");
		output7 = option.nextLine();
		System.out.println("8. How did you like my nifty questionnaire?");
		output8 = option.nextLine();
		try {
			FileWriter writer = new FileWriter("C:/Users/Infosec3000/My Documents/Answers17.txt", true);;
			BufferedWriter out = new BufferedWriter(writer);
			
			out.write("1. What Kind of car do you drive?" + "  " + output1 +  "\r\n" );
			out.write("2. What is your favorite color?" + "  " + output2 +  "\r\n");
			out.write("3. Have you ever traveled outside the US?" + "  " + output3 +  "\r\n");
			out.write("4. What is your college major?" + "  " + output4 +  "\r\n");
			out.write("5. How long do you spend on your labs and assignments?" + "  " + output5 +  "\r\n");
			out.write("6. What is your dream job?" + "  " + output6 +  "\r\n");
			out.write("7. Where do you live?" + "  " + output7 +  "\r\n");
			out.write("8. How did you like my nifty questionnaire?" + "  " + output8 +  "\r\n");
			
			out.close();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

}
	




Was This Post Helpful? 0
  • +
  • -

Page 1 of 1