-1

So I am creating simple program for my Java class and one of the last requirements is to have the program loop. I've done this before, but it was horribly inefficient and convoluted.

Basically, I used a switch case named start with values 0 and 1. If the user typed 0 a do-while loop would begin and if they typed 1, the program would terminate. The only problem with this is the user would have to type 0 for the program to even begin, and if they typed 1, I had to have a fail-safe changing the value of start to 3 or else an infinite loop would begin. Can someone help me find a better way of doing this? (Also, before anyone says anything about the way it is written, one of the requirements was that it HAD to be done within one executable class.) (Also, Also: Could someone tell me the rules of indentation? I'm really horrible at it.)

Here is my code below:

/*
 * Simple Java Survey Program
 */

import java.util.Scanner;

public class J2Strings {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        // variables
        String fName;
        String lName;
        String mName;
        int age;
        String say;
        String fFood;
        String fTV;

        Scanner userInput = new Scanner(System.in);

        System.out.println("Today you are going to take a small personal survey");
        System.out.println("");
        System.out.println("Begin by entering your first name: ");
        fName = userInput.next();
        System.out.println("Enter your last name:");
        lName = userInput.next();
        System.out.println("Enter your middle name:");
        mName = userInput.next();
        System.out.println("Enter your age:");
        age = userInput.nextInt();
        System.out.println("Enter your favorite saying:");
        say = userInput.next();
        System.out.println("Enter your favorite food:");
        fFood = userInput.next();
        System.out.println("Finally, enter your favorite TV show:");
        fTV = userInput.next();

        char f = fName.charAt(0);
        char l = lName.charAt(0);
        char m = mName.charAt(0);

        System.out.println("Based on the information you entered, here are your initials: " + f + "." + m + "." + l);

        System.out.println("This is how old you will be in 50 years: " + (age + 50));

    }

}
Vasan
  • 4,438
  • 3
  • 17
  • 38
CaptainAmerica16
  • 236
  • 2
  • 10
  • 2
    General rule of indentation: if a parenthesis opens, you indent all lines after it by one indentation. If a parentesis closes, you re-indent all lines after it by one indentation. One indentation is usually two or four blanks (your choice). Do not use tabulators. – Turing85 Apr 23 '18 at 17:19
  • To avoid the user-input before the first iteration, you could use a [`do { .... } while ();` loop](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html). – Turing85 Apr 23 '18 at 17:21
  • *Unrelated:* Your code fails if my "favorite saying" is more than one word, and "sayings" are usually sentences, not single words. Same for "favorite food" and "favorite TV show" – Andreas Apr 23 '18 at 17:21
  • @Andreas and here I through "*more than one word*" is your favorite saying =) – Turing85 Apr 23 '18 at 17:22
  • @Turing85 No, my favorite saying on here is "do some research", though it often can be abbreviated to "RTFD". Ok, it's a *common* saying, not really my *favorite*. My favorite is: *Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.* – Andreas Apr 23 '18 at 17:24
  • @Andreas regular expressions are wonderful. They help you solving problems which you would not have without them. Oh... wait... those were marriages.... not regular expressions... – Turing85 Apr 23 '18 at 17:36
  • Thanks for the info. Is there a way to rectify that? – CaptainAmerica16 Apr 23 '18 at 17:37
  • @CaptainAmerica16 If you're asking about the multi-word issue, use `nextLine` instead of `next`. Beware: "[Scanner is skipping nextLine() after using next() or nextFoo()](https://stackoverflow.com/q/13102045/5221149)", since you do have a `nextInt()` in there. Link has solution for that too. – Andreas Apr 23 '18 at 17:39
  • @CaptainAmerica16 only a divorce can rectify a marriage ``. What exactly do you mean? My statement about the `do { ... } while ()` loop? – Turing85 Apr 23 '18 at 17:39

1 Answers1

1

Something like this

bool test = true;
while(test)
{
    /*Your code here */
    /*At the end you ask them if they want to try again*/
    /*Then switch the boolean based on their answer*/
}
Zohir Salak
  • 5,738
  • 2
  • 9
  • 23
  • I tried it out, but it's not responding to my conditional. It just loops even if the user enters "No". – CaptainAmerica16 Apr 23 '18 at 18:21
  • if they user enter no you need to `test = false;` if they enter yes you do nothing – Zohir Salak Apr 23 '18 at 18:23
  • I have it like this: System.out.println("Would you like to restart the program?"); ans = userInput.next(); if (ans == "No") { test = false; } – CaptainAmerica16 Apr 23 '18 at 18:24
  • Sorry if this is hard to understand. I don't know how to add the snippets to comments :/ – CaptainAmerica16 Apr 23 '18 at 18:25
  • well you need to enter an exact "No" with the uppercase N. a better way to do it is to lower case what they enter and check if it's either yes or no and if they enter anything other than that close the program by switching the boolean to false – Zohir Salak Apr 23 '18 at 18:28