编程论坛's Archiver

tlx119 发表于 2008-4-24 23:19

怎么读取有空格的字符串

System.out.println( "Input Address: ");
String street = scan.next();
System.out.println(street);

Input Address:
fds fds
fds
请问怎么才能读取整段字符"fds fds".

hanzhu3366 发表于 2008-4-25 01:48

转换成string试试~~

tlx119 发表于 2008-4-25 07:38

谢谢2楼。
但该怎么转呢?next或者nextLine读的都是String嘛,还是不知道,请大家帮忙

okchenpeng 发表于 2008-4-25 07:52

这么读不就行了嘛 !!?

import java .util.*;
public class Main {
    public static void main(String[] args) {
        System.out.println( "Input Address: ");
        Scanner in =new Scanner(System.in);
String street = in.nextLine();
   System.out.println(street);

tlx119 发表于 2008-4-25 08:07

这样还是不行,
System.out.println( "Input ID: ");
String street = scan.nextInt();
System.out.println( "Input Address: ");
String street = scan.nextLine();
System.out.println( "Input Job: ");
String street = scan.next();

这样的话,Address不用输入就给值了,如果把nextLine换成next,输入空格就出现只能读取空格前内容的情况了

longrm 发表于 2008-4-25 16:37

不要用next(),用nextLine()

tlx119 发表于 2008-5-4 10:43

Thanks for all!

tlx119 发表于 2008-5-4 10:46

Problem fixed. Thanks for all

页: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.