Question:
Java programming question?
2011-12-05 22:23:00 UTC
10 points?!

I want to add a code that will allow me to ask the user to enter in a clothing item (such as pants, socks, shirt or or or or or etc) and if they don't enter in a clothing item, then i want it to tell them that they need to try again! i was thinking of using a boolean for this, but boolean only works with numbers..

also, how would a assign a word to a variable, i know you can use int number=input.nextInt(); to insert an integer, but how do i insert a word? such as pants? thanks
Four answers:
Teoyaoimquit
2011-12-05 22:35:15 UTC
I'd try.

boolean validInput=False;

While (!validInput) {

Output choices

if (input.equals("Socks") || input.equals("pants")) {

validInput=true;

}

else { validInput=false };

}

you have the right idea for a Boolean flag you just have to make clearto the user what input you require. Youre gonna need an if statement testing for what is valid input.
2011-12-05 23:05:33 UTC
Hi Nick,



That is a tricky one given a variable of type String can contain anything from a word to a digit.



To read user input into a String is relatively straightforward however, for example:



import java.util.Scanner;

public class Clothes {

  static String clothingItem = "";



  public static void main(String[] args) {

    Scanner input = new Scanner(System.in);



    System.out.println("Enter a clothing item");

    clothingItem = input.next();



    System.out.println("You entered " + clothingItem);

  }

}



In you wanted to you could replace clothingItem = input.next() with:



  clothingItem = input.nextLine();





In terms of testing whether someone entered a clothing item, you could make a String array to store items you deem to be clothing items, then test the input of user against the array, followed by a second conditional statement for validation. For example:



import java.util.Scanner;

public class Clothes {

  static String clothingItem = "";

  static String [] clothing = {

    "shoes", "socks", "pants", "shorts", "trousers", "jeans",

    "dress", "skirt", "stockings", "underwear", "blouse", "shirt",

    "singlet", "t-shirt", "skivvy", "jersey", "scarf", "hat"};



  public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    int x = 0;



    System.out.println("Enter a clothing item");

    clothingItem = input.next();



    /* Test whether entered item is clothingItem

     * First check Array if item is there making

     * sure user input is set to LowerCase to match

     * array of lowercase clothing items

     */

    for (int i = 0; i < clothing.length; i++) {

      if (clothingItem.toLowerCase(

          ).equals(clothing[i])) {

        x = 1;

      }

      else {

        x = 0;

      }

    }

    // Second conditional statement for validation

    if (x == 1)

      System.out.println("The clothing item you entered was " + clothingItem);

    else

      System.out.println("Error! You did not enter a clothing item");

  }

}



Regards,



Javalad
meharg
2016-12-17 21:48:07 UTC
Why roll your own while there is in all threat a calendar widget you may reuse? via the way, the Java API itself aspects many smart instructions and interfaces: Date, Calendar, GregorianCalendar, DateFormat, and SimpleDateFormat.
jay
2011-12-05 22:25:08 UTC
String can be entered as a variable



String varName = "whatever you want";



hope this helps. My turn? :D

https://answersrip.com/question/index?qid=20111205204907AAcTsMl


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...