1 Replies - 302 Views - Last Post: 07 April 2014 - 09:50 AM Rate Topic: -----

#1 kid_coder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 03-April 14

Empty Strings

Posted 07 April 2014 - 09:45 AM

I've been trying to get my program to not accept empty strings when the user inputs, but I'm having some difficulties. I've tried for loops, as well as while loops.

import java.util.Scanner;
public class GasMileage
{
    /**
     * displayIntro
     * This method displays the introduction to the user
     */
    public static void displayIntro()
    {
        System.out.println("==============================================================");
        System.out.println("This program will compute gas mileage for two vehicles \n");
    }
    
    /**
     * displayHeader
     * This method displays the header to the user
     * 
     * @param   s   
     * @param   m
     * @param   g
     * @param   mpg
     */
    public static String displayHeader(String s, double m, double m1, String s1, double g, double g1)
    {
        System.out.println("====================================================");
        System.out.println("                 Vehicle MPG Chart                 \n");
        System.out.printf("%10s %15s %15s", "Vehicle", "#Miles", "#Gallons");
        System.out.println("\n====================================================");
        System.out.printf("%10s %15f %15f", s, m, g);
        System.out.println(" ");
        System.out.printf("%10s %17f %14f", s1, m1, g1);
        return s;
    }
    
    /**
     * getString
     * Prompt for and read in the String
     * Trim the string 
     * return the string
     * 
     * @param   s    String designates purpose of string to be read in
     * @return       The string, trimmed
     */
    public static String getString(String s)    // "Catches" a String; Returns a String
    {
        Scanner stdIn = new Scanner (System.in); 
        // Prompt the user for the string needed
        System.out.print("Enter the "+s+": ");
        return stdIn.nextLine().trim();
    }
    
    /**
     * getMilesDriven
     * This will prompt for and read in the miles driven
     * 
     * @param   m   Miles driven
     * @return      The value read in by the user as the miles driven
     */
    public static Double getMilesDriven(String m)
    {
        Scanner keyboard = new Scanner (System.in);
        //User input number of miles driven
        System.out.print("Enter the "+m+": ");
        double entry = keyboard.nextDouble();
        do
        {
            if (entry <=0) //Anything less than 0 will enter the do-while loop
            {
                System.out.println("\nHEY! That's not right!");
                System.out.print("Enter the "+m+": ");
                entry = keyboard.nextDouble();
            }
        }while (entry <=0);
        return entry;
    }
    
    /**
     * getMilesDriven
     * This will prompt for and read in the miles driven
     * 
     * @param   m1  Miles driven
     * @return      The value read in by the user as the miles driven
     */
    public static Double getMilesDriven1(String m1)
    {
        Scanner keyboard = new Scanner (System.in);
        //User input for miles driven on second car model
        System.out.print("Enter the "+m1+": ");
        double entry = keyboard.nextDouble();
        do
        {
            if (entry <=0) //Less than 0, you go in the do-while loop
            {
                System.out.println("\nHEY! That's not right!");
                System.out.print("Enter the "+m1+": ");
                entry = keyboard.nextDouble();
            }
        }while (entry <=0);
        return entry;
    }
    
    /**
     * getGallonsUsed
     * This will prompt for and read in the gallons used
     * 
     * @param   g   Gallons used
     * return       The value read in by the user as the gallons used
     */
    public static Double getGallonsUsed(String g)
    {
        Scanner keyboard = new Scanner (System.in);
        System.out.print("Enter the "+g+": ");
        double entry = keyboard.nextDouble();
        do
        {
            if (entry <=0)
            {
                System.out.println("\nHEY! That's not right!");
                System.out.print("Enter the "+g+": ");
                entry = keyboard.nextDouble();
            }
        }while (entry <=0);
        return entry;
    }
    
    /**
     * getGallonsUsed
     * This will prompt for and read in the gallons used
     * 
     * @param   g1   Gallons used
     * return       The value read in by the user as the gallons used
     */
    public static Double getGallonsUsed1(String g1)
    {
        Scanner keyboard = new Scanner (System.in);
        System.out.print("Enter the "+g1+": ");
        double entry = keyboard.nextDouble();
        do
        {
            if (entry <=0)
            {
                System.out.println("\nHEY! That's not right!");
                System.out.print("Enter the "+g1+": ");
                entry = keyboard.nextDouble();
            }
        }while (entry <=0);
        return entry;
    }
    
    /**
     * getMPG
     * This will bring the MPG from the miles driven and gallons used
     * 
     * @param   mpg     Miles per gallon
     * @return          Return the value of MPG (miles driven / gallons used)
     */
    public static Double getMPG(double m, double g)
    {
        return (m/g);
    }
    
    public static void main (String [] args)
    {
        displayIntro();
        
        String name = getString("user's name");
        String model1 = getString("first car model");
        
        double miles = getMilesDriven("miles driven");
        double gallons = getGallonsUsed("number of gallons used");
        
        System.out.println(" ");        
        String model2 = getString("second car model");
        
        double miles1 = getMilesDriven1("miles driven");
        double gallons1 = getGallonsUsed1("number of gallons used");
        
        System.out.println("\n");
        displayHeader(model1, miles, gallons, model2, miles1, gallons1);
    }
}


If someone could let me know what I'm doing wrong that would be excellent

This post has been edited by macosxnerd101: 07 April 2014 - 09:47 AM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Empty Strings

#2 peepsrct  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 121
  • Joined: 29-March 09

Re: Empty Strings

Posted 07 April 2014 - 09:50 AM

public static String getString(String s) // "Catches" a String; Returns a String
{

Scanner stdIn = new Scanner (System.in);
// Prompt the user for the string needed
System.out.print("Enter the "+s+": ");
return stdIn.nextLine().trim();
}



Try to replace this with this;

public static String getString(String s) // "Catches" a String; Returns a String
{

Scanner stdIn = new Scanner (System.in);
// Prompt the user for the string needed
String validateStr = "";
while(validateStr.length() < 1)
{
System.out.print("Enter the "+s+": ");
validateStr = stdIn.nextLine().trim();
}
return validateStr;
}


This will go until they have inputed a non-blank value.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1