1

I am writing a program that writes a letter using specific parts of a string.

Here is what I have so far (I am only a beginner)

import java.util.Scanner;

public class AutoInsurance {

    public static void main (String[] args)
    {
        Scanner scan=new Scanner (System.in);
        System.out.print("Enter Name:");
        String Name;
        Name=scan.nextLine();
        System.out.print("Enter Street Address:");
        String Address;
        Address=scan.nextLine();
        System.out.print("Enter city, state, and zip code:");
        String Location;
        Location=scan.nextLine();
        System.out.println();
        System.out.println();
        System.out.println("Dear "+Name+",");
        System.out.println(" You have been selected to receive this offer of auto insurance from");
        System.out.println("Allspam Insurance Company! Drivers from "++" saved an average "); // I just want it to print the city here, between ++

        // I will finish coding once I figure this out, but I'm stumped
    }
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
CyborGamer
  • 13
  • 1
  • 1
  • 3
  • 1
    Why don't you just save the city to a different variable when you get the user's input – Mureinik Apr 11 '14 at 17:35
  • You have to split the Location depending on how you enter it... I would suggest you doing a scan.nextLine() for each information (city / state / zip_code) unless you establish a clear way to split the strings (a separator), but the user might be confused and add extra unwanted characters – naab Apr 11 '14 at 17:37
  • I need to keep the city, state, and zip in the same string, otherwise I'd have to have them enter three different variables. Is there a way I can do that but keep the same format? – CyborGamer Apr 11 '14 at 17:37
  • @CyborGamer have you tried something like While(Scan.hasNext()) and assigning the variables that way? – David MacNeil Apr 11 '14 at 17:44

3 Answers3

3

The best you can do here is to split your Adress string, by commas, and grab the first value from the resulting array.

Take a look at this question for more details on splitting a string in Java.

I suggest the following in your code:

String[] AddressParts = Address.split(",");
String City = AddressParts[0];
System.out.println("Allspam Insurance Company! Drivers from "+City+" saved an average ");

Cheers!

Community
  • 1
  • 1
JeanLescure
  • 1,029
  • 9
  • 19
  • Thank you so much for all this help! I did not expect such quick help! Thank you! – CyborGamer Apr 11 '14 at 17:43
  • No problem, that's what the StackExchange community is here for, be sure to pick an answer and up vote it (if possible) once you achieve what you wanted. Also try to keep in mind to pick the answer that fits the question the better even if your goal changes as you recieve more input. Good riddance ;) – JeanLescure Apr 11 '14 at 17:46
0

It would be a better style to use different variables for each part (city, postal and zip code).

Otherwise you might

  • Change the order of the elements, take postal into the middle and than do String city = Location.split("[0-9]")[0];
  • Define a token that the users inputs to seperate the data (e.g. #) and than do String city = Location.split(#`)[0];
ifloop
  • 8,079
  • 2
  • 26
  • 35
0

To break a string apart use this method

String s = "abcde";
String p = s.substring(2, s.length());

From here, you can find out which parts of the string you want.

You could also use StringUtils like this if you want only n characters from it:

StringUtils.substring("abcde", 0, 2) => "ab"      
Ash
  • 168
  • 7
David MacNeil
  • 126
  • 11