Categories

JAVA DATEBASE
Technology Network Community
Oracle Database
Fusion Middleware
Development Tools
Java
Desktop
Server & Storage Systems
Enterprise Management
Berkeley DB Family
Cloud Computing
Big Data
Business Intelligence
Architecture
Migration and Modernization
E-Business Suite
Siebel
PeopleSoft Enterprise
JD Edwards World
Industries
JD Edwards EnterpriseOne
User Productivity Kit Pro (UPK) and Tutor
Governance, Risk & Compliance (GRC)
Master Data Management (MDM)
Oracle CRM On Demand
On Demand: SaaS and Managed Applications
AutoVue Enterprise Visualization
Primavera
ATG
Agile PLM
Endeca Experience Management
Fusion Applications
Archived Forums

 



Tags

New To Java


Need help with my Java code!!!!


//------------------------------------------------------ASK QUESTION 1 SECTION-------------------------------------------------------//           //QUESTION 1//           System.out.print("QUESTION 1 : ");           Scanner q1 = new Scanner(System.in);           String question1=q1.nextLine();              if (question1.equalsIgnoreCase("y"))q1YesCounter++;            else if (question1.equalsIgnoreCase("n"))q1NoCounter++;                 else if(question1.equals(""))q1NRCounter++;             //------------------------------------------------------ASK QUESTION 2 SECTION-------------------------------------------------------//           //QUESTION 2//           System.out.print("QUESTION 2 : ");           Scanner q2 = new Scanner(System.in);           String question2=q2.nextLine();              if (question2.equalsIgnoreCase("y"))q2YesCounter++;            else if (question2.equalsIgnoreCase("n"))q2NoCounter++;                 else if(question2.equals(""))q2NRCounter++;              //------------------------------------------------------ASK QUESTION 3 SECTION-------------------------------------------------------//            //QUESTION 3//             System.out.print("QUESTION 3 : ");            Scanner q3 = new Scanner(System.in);            String question3=q3.nextLine();               if (question3.equalsIgnoreCase("y"))q3YesCounter++;            else if (question3.equalsIgnoreCase("n"))q3NoCounter++;                 else if(question3.equals(""))q3NRCounter++;              //-----------------------------------------------------PRINT QUESTION STATISTICS SECTION--------------------------------------//             //DISPLAY QUESTION 1 STATISTICS//           System.out.print("QUESTION 1 :  YES : "q1YesCounter"    ");           System.out.print("NO : "q1NoCounter"    ");           System.out.println("NO RESPONSE : "q1NRCounter"    ");               //DISPLAY QUESTION 2 STATISTICS//           System.out.print("QUESTION 2 :  YES : "q2YesCounter"    ");           System.out.print("NO : "q2NoCounter"    ");           System.out.println("NO RESPONSE : "q2NRCounter"    ");             //DISPLAY QUESTION 3 STATISTICS//          System.out.print("QUESTION 3 :  YES : "q3YesCounter"    ");          System.out.print("NO : "q3NoCounter"    ");          System.out.println("NO RESPONSE : "q3NRCounter"    ");      This us just an excerpt of  the entire program. This program actually ask user to input "y", "n" or leave it null for 3 questions, Q1, Q2 &#38; Q3 respectively After which the program tabulates all the yes answers, the no answers and null answers into this format  ***********************************  QUESTIONAIRE STATISTICS  ***********************************  MALES : 1    FEMALES : 1    TOTAL : 2      AGE : <=25 : 1    26 - 45 : 1    >=46 : 0    TOTAL: 2      QUESTION 1 :  YES : 2    NO : 0    NO RESPONSE : 0     QUESTION 2 :  YES : 0    NO : 2    NO RESPONSE : 0     QUESTION 3 :  YES : 0    NO : 0    NO RESPONSE : 2      The program actually works fine for now but I need to improve on the coding, I understand that I should be using for loops but whenever I do that, I do not seem to get a correct number of yes, no and null values.   If anyone could help me I will be ever so grateful...  Thanks a million

what a waste of time.  Please put your code in the code tags. I guess you need to work a lot on your coding.

Hi,  Thank you for your opinion.   Yes... I know I have to work on my coding. That is why I posted it under "New To Java". In fact, I am really new to programming itself. I am also a student and have not done programming before.   If you could help me, I would really appreciate it. With all due respect, its disheartening to read your comments.

If you could help me, I would really appreciate it. With all due respect, its disheartening to read your comments.  Can you repost your code between code tags?   Don't mind sarvananda, he/she is having a bad day.

Hi ams,  Here's a couple thoughts:   - Why do you create a new Scanner object for each question? Why not just create a single one and then call scanner.readLine() when you need to get input?  - If you want to use a loop, you'll need a way to correctly store the responses. Have you thought of using an array? Instead of having q1YesCounter, q2YesCounter, and q3YesCounter, you could have a single yesCounter array that stores the yes responses for all the questions.

ams wrote: The program actually works fine for now but I need to improve on the coding, I understand that I should be using for loops but whenever I do that, I do not seem to get a correct number of yes, no and null values.    You don't say what it is exactly you want to do, especially since you say that the program works fine.  But I'll take a stab and guess that 1) You want the program to keep looping, so you can keep asking the questions, 2) You've tried this, but the results keep getting set back to zero.  If this is the case, you need to make a for loop like you tried, but make sure you initialize all your variables outside the loop, before it starts.  If that doesn't work or isn't your question, try to be a little more specific about what you want and what you tried. Also, it will help is you place your code between "code" tags.

1) don't pay attention to that first reply...  2) do you have code of your attempt to do a for loop? it would really help


Related Links

2 Dimensional array
how to assign a value to &lt;select&gt; box
How do I write bitmap files? or do SVG?
consequtive sequence of positive integers = target sum, must build algoryth
Need help (don't know enough Java to continue my program)
SpringLayout button icon
swing components positioning
Can't run HelloWorld!
StringBuffers??
J2SE SDK
I need a jprogress bar
Console -&gt; JTextArea
No getter method available for property address for bean under name org.apa
Should I install a private JRE with my application.
Reading an int from a particular location in a file
Creating A Website