Concerns about creating classes, set methods, and the String data type using the Scanner class

StackOverflow https://stackoverflow.com/questions/22650690

  •  21-06-2023
  •  | 
  •  

Domanda

So i'm writing a little program to practice for my exam coming up, and it involves creating a class file as well as an application file. Basically, i'm not sure if my setup will work because user input is required from the Scanner class, and i'm not aware of a method for Strings similar to, say, the nextInt() or nextDouble() methods. Here is a snippet of what I have written so far, and I was basically wondering how I could make the set methods i'm using work when I need to take user input(some of my set methods use Strings and not primitive data types like an int or double). Just curious if my current format can work once I get around to the application class, or if I need to change my methods to use a numerical input instead of Strings and then use something like a switch or if statement later on to change those numerical values to a String. Here is what i've written so far, from the class file:

import java.text.DecimalFormat;

public class GuitarStore
{

   DecimalFormat dollar = new DecimalFormat("$#,##0.00");

   private int stockNumber;
   private int modelID;
   private int pickupType;
   private String color;
   private String brand;
   private String pickupType
   private double totalValueOfStock;

   public GuitarStore()
   {

   stockNumber = 0;
   modelID = 0;
   pickupID = 0;
   color = "";
   brand = "";
   pickupType = "";
   totalValueOfStock = 0.0;

   }

   public GuitarStore(int stkNum, int modID, int pickID, String clor, String brnd,
                      double totValOfStock)

   {

   setStock(stkNum);
   setModel(modID);
   setPickup(pickID);
   setColor(clor);
   setBrand(brnd);
   setTotalValue(totValOfStock); 

   }

   public void setStock(stkNum)
   {

      if (stkNum > 1 && stkNum <= 500)
      {
            stockNumber = stkNum;
      }

      else 
      {
            stockNumber = 0;
      } 
   }

   public void setModel(modID)
   {
      if (modID >= 1 && modID <= 1000)
      {      
            modelID = modID;
      }

      else                      
      {
        modelID = 0;
      }
   } 

   public void setPickup(pickID)
   {
      if (pickID > 1 && pickID <= 3)
      {
            pickupID = pickID;
      }

      else
      {
            pickupID = 0;
      }
   }  

   public void setColor(clor)
   {
      if (clor == "red") 
            color = clor;
      else if (clor == "blue")
            color = clor;
      else if (clor == "purple")
            color = clor;
      else if (clor == "green")
            color = clor;
      else if (clor == "white")
            color = clor;
      else
            System.out.println("We do not carry that color");

Basically, i'm most curious about the setColor method and if/how it will work once I get to the application part of the program. In previous projects, i've just used numbers and not strings which I then converted to a string using a switch statement. This is something totally new to me and i'm going out on a limb trying something new. Since I need to take user input to determine what color they will want, i'm not sure how to parse that since I can't use a method like nextDouble() or nextInt() like I stated above. This is total practice so if anyone can tell me if I have a solid plan or not i'd appreciate it, and also what I should do to take the user input once I get to the application process for the set methods. Thanks, everyone here is always so helpful!!! :)

È stato utile?

Soluzione

Scanner does have a next method, which reads a token regardless of type and returns it as a String, as well as a nextLine method, which does what you expect. In general, I would suggest using next or nextLine over nextInt and other type-specific methods, just because you may run into some unexpected behaviour if the input isn't exactly right.

Also, you shouldn't compare two strings with the == operator. In Java, == is a reference (or shallow) comparison, which essentially checks if two objects are stored in the same place on your computer; this works for primitive types (ints, doubles, etc.), because of how they're represented in the language. It will NOT work for Strings, because Strings are complex objects in Java. Instead you should use the equals method, which will do a deep comparison - checking if all of the data is identical.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top