简体   繁体   中英

need some guidance in reference to spaces in strings generated from scanner in java

My string description crashes the Console because of the spaces generated in normal sentence structure. i am looking for some guidance in reference to why this happens and futhermore if i am going about this the wrong way how should i be approaching it.

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
    System.out.println("-/- Job ticket  -/-");
    Scanner MyScanner = new Scanner (System.in);

    String FirstName;
    String LastName;
    String Phone;
    String Address;
    String Description;
    int ticketNumber;
    int orderMonth ;
    int orderDay;
    int orderYear;
    int requestedDay;
    int requestedMonth;
    int requestedYear;

    System.out.println("First Name = ?");
    FirstName = MyScanner.next();
    System.out.println("Last Name = ?");
    LastName = MyScanner.next();
    System.out.println("Phone = ?");
    Phone = MyScanner.next();
    System.out.println("Address = ?");
    Address = MyScanner.next();
    System.out.println("Description = ?");
    Description = MyScanner.next();
    System.out.println("Ticket = ?");
    ticketNumber = MyScanner.nextInt();
    System.out.println("Order Date Month = ?");
    orderMonth = MyScanner.nextInt();
    System.out.println("Order Date Day");
    orderDay = MyScanner.nextInt();
    System.out.println("Order Date Year");
    orderYear = MyScanner.nextInt();
    System.out.println("Requested Date Month");
    requestedMonth = MyScanner.nextInt();
    System.out.println("Requested Date Day");
    requestedDay = MyScanner.nextInt();
    System.out.println("Requested Date Year");
    requestedYear = MyScanner.nextInt();

    System.out.println("=====================================================");
    System.out.print("Ticket :   ");
    System.out.println(ticketNumber);
    System.out.print("Customer:   ");
    System.out.print(FirstName);
    System.out.print(" ");
    System.out.println(LastName);
    System.out.print("Home Phone: ");
    System.out.println(Phone);
    System.out.print("Order Date: ");
    System.out.print(orderMonth);
    System.out.print('/');
    System.out.print(orderDay);
    System.out.print('/');
    System.out.println(orderYear);
    System.out.print("Requested Date:   ");
    System.out.print(requestedMonth);
    System.out.print('/');
    System.out.print(requestedDay);
    System.out.print('/');
    System.out.println(requestedYear);
    System.out.println("-----------------------------------------------------");
    System.out.println("Address");
    System.out.println(Address);
    System.out.println("-----------------------------------------------------");
    System.out.println("Description");
    System.out.println(Description);
    System.out.println("=====================================================");
    System.out.println();
   }
}

I see a problem in your code here.

When you use Scanner.next(), Scanner looks for the next whitespace " " and returns you all the text before that whitespace.

This is due to the concept of delimiters in the Scanner class and the default delimiter is a 'whitespace or next line' (You might want to read up on delimiters).

How does the delimiter work for you?

Eg: Your input is "Hello World\\r\\n"

When you invoke Scanner.next(), Scanner will find the whitespace, and return you everything before that, so it will return "Hello". When you invoke Scanner.next() again, Scanner will find the \\r\\n (next line) and return you everything before that, so it will return "World".

So for your code, what will happen is that:

Address = MyScanner.next();
System.out.println("Description = ?");
Description = MyScanner.next();
System.out.println("Ticket = ?");

If my input for Address is "123 ABC Avenue", Address will only get the input "123", while Description will automatically pull the value "ABC" from the input which is not empty yet.

Thus, you will observe that the program appears to "skip" the Description user prompt.

You should use the Scanner.nextLine() method if you want to pull the entire user input out from the Scanner, instead of Scanner.next()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM