如何实现, 从控制台里输入一个字符串,一个整数,分别保存到变量中?

caddor2011 2014-06-04 06:07:53
public class Test {

private static String readString(String prompt)
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = null;
try
{
str = br.readLine();
int a = br.read();
a++;
}
catch(IOException e)
{
}
return str;
}
public static void main(String[] args) {
String str = readString("请输入字符串:");
System.out.println("readString 方法的输入:" + str);
}
}

adsgsadh
2223

debug后,发现a的值为50




我的需求,从控制台里输入一个字符串,一个整数,分别保存到变量中, 这是需求。


如何实现?
...全文
441 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
亦山 2014-06-05
  • 打赏
  • 举报
回复
引用 4 楼 caddor2011 的回复:
[quote=引用 3 楼 magi1201 的回复:] [quote=引用 2 楼 caddor2011 的回复:] 可以提供一个 BufferedReader版本的嘛?

    public static String str = null;
    public static Integer num = null;
    
    public static void main(String[] args) throws IOException {
        System.out.println("请输入字符串");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        str = br.readLine();
        System.out.println("请输入整数");
        num = Integer.parseInt(br.readLine());
        System.out.println("字符串为 " + str + "\n" + "整数为 " + num);
    }
[/quote] public class Test { public static void main(String[] args) { String str = null; Integer num = null; System.out.println("请输入字符串"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); str = br.readLine(); System.out.println("请输入整数"); num = Integer.parseInt(br.readLine()); System.out.println("字符串为 " + str + "\n" + "整数为 " + num); } } Exception in thread "main" java.lang.Error: Unresolved compilation problems: Unhandled exception type IOException Unhandled exception type IOException at Test.main(Test.java:14) java 版本是1.7的, 是不是必须捕捉异常才可以编译过去? java语法这么奇怪,难道异常必须捕捉? [/quote]

package com.foo.proxy;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Test1 {
	public static void main(String[] args) throws IOException {
		String str = null;
		Integer num = null;
		System.out.println("请输入字符串");
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		str = br.readLine();
		System.out.println("请输入整数");
		num = Integer.parseInt(br.readLine());
		System.out.println("字符串为 " + str + "\n" + "整数为 " + num);
	}
}
你的代码没问题啊! 你看一下你的14行对应的是哪一条语句~
姜小白- 2014-06-05
  • 打赏
  • 举报
回复
引用 4 楼 caddor2011 的回复:
public class Test { public static void main(String[] args) { String str = null; Integer num = null; System.out.println("请输入字符串"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); str = br.readLine(); System.out.println("请输入整数"); num = Integer.parseInt(br.readLine()); System.out.println("字符串为 " + str + "\n" + "整数为 " + num); } } Exception in thread "main" java.lang.Error: Unresolved compilation problems: Unhandled exception type IOException Unhandled exception type IOException at Test.main(Test.java:14) java 版本是1.7的, 是不是必须捕捉异常才可以编译过去? java语法这么奇怪,难道异常必须捕捉?
楼主可以捕获异常,我的代码写的简单,直接将异常抛出去了。
public static void main(String[] args) throws IOException {
caddor2011 2014-06-05
  • 打赏
  • 举报
回复
引用 3 楼 magi1201 的回复:
[quote=引用 2 楼 caddor2011 的回复:] 可以提供一个 BufferedReader版本的嘛?

    public static String str = null;
    public static Integer num = null;
    
    public static void main(String[] args) throws IOException {
        System.out.println("请输入字符串");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        str = br.readLine();
        System.out.println("请输入整数");
        num = Integer.parseInt(br.readLine());
        System.out.println("字符串为 " + str + "\n" + "整数为 " + num);
    }
[/quote] public class Test { public static void main(String[] args) { String str = null; Integer num = null; System.out.println("请输入字符串"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); str = br.readLine(); System.out.println("请输入整数"); num = Integer.parseInt(br.readLine()); System.out.println("字符串为 " + str + "\n" + "整数为 " + num); } } Exception in thread "main" java.lang.Error: Unresolved compilation problems: Unhandled exception type IOException Unhandled exception type IOException at Test.main(Test.java:14) java 版本是1.7的, 是不是必须捕捉异常才可以编译过去? java语法这么奇怪,难道异常必须捕捉?
姜小白- 2014-06-04
  • 打赏
  • 举报
回复
引用 2 楼 caddor2011 的回复:
可以提供一个 BufferedReader版本的嘛?

    public static String str = null;
    public static Integer num = null;
    
    public static void main(String[] args) throws IOException {
        System.out.println("请输入字符串");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        str = br.readLine();
        System.out.println("请输入整数");
        num = Integer.parseInt(br.readLine());
        System.out.println("字符串为 " + str + "\n" + "整数为 " + num);
    }
caddor2011 2014-06-04
  • 打赏
  • 举报
回复
引用 1 楼 magi1201 的回复:
楼主的代码中,整数a 为局部变量,并不是你的输入 不知道下面这个是否符合楼主的要求

    public static String str = null;
    public static Integer num = null;
    
    public static void main(String[] args) {
        System.out.println("请输入字符串");
        Scanner sc = new Scanner(System.in);
        str = sc.nextLine();
        System.out.println("请输入整数");
        num = sc.nextInt();
        System.out.println("字符串为 " + str + "\n" + "整数为 " + num);
    }
可以提供一个 BufferedReader版本的嘛?
姜小白- 2014-06-04
  • 打赏
  • 举报
回复
楼主的代码中,整数a 为局部变量,并不是你的输入 不知道下面这个是否符合楼主的要求

    public static String str = null;
    public static Integer num = null;
    
    public static void main(String[] args) {
        System.out.println("请输入字符串");
        Scanner sc = new Scanner(System.in);
        str = sc.nextLine();
        System.out.println("请输入整数");
        num = sc.nextInt();
        System.out.println("字符串为 " + str + "\n" + "整数为 " + num);
    }

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧