习题求助!

帅帅没人爱 2011-09-24 06:59:10
代码如下:要求是输入形式是:name_quantity_price
输入时 以下划线做出区分,同时将输入的值赋给不同类型的name quantity 和price
private Product readProduct() throws IOException {

final String DELIM = "_";

String name = "";
int quantity = 0;
double price = 0.0;
/* PLACE YOUR CODE HERE */
System.out.print("Product [Name_Quantity_Price] :");
try
{
String s=new Scanner(System.in).next();
StringTokenizer st=new StringTokenizer(s,DELIM,true);


name+=st.nextToken();
quantity=Integer.parseInt(st.nextToken());
price=Double.parseDouble(st.nextToken());
}
catch(NumberFormatException e)
{
e.printStackTrace();
}
return new Product(name, quantity, price);
}
...全文
68 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
帅帅没人爱 2011-09-24
  • 打赏
  • 举报
回复
谢谢!我刚才提交了作业,方法参考了你给的,就是输入流换成了readLine赋值个name。总之特别感谢!

[Quote=引用 4 楼 ioe_gaoyong 的回复:]

用Scanner直接输入空格解析出来就有了
String s =new Scanner(System.in).next();
Sysetem.out.println(s);
你输入a b c d
可以输出输入的字符串
引用 3 楼 aecsun 的回复:
谢谢!再问一个问题,输入的时候怎么将空格键输入进一个字符串呢?
比如说 Mp3 Player
[/Quote]
风尘中国 2011-09-24
  • 打赏
  • 举报
回复
用Scanner直接输入空格解析出来就有了
String s =new Scanner(System.in).next();
Sysetem.out.println(s);
你输入a b c d
可以输出输入的字符串
[Quote=引用 3 楼 aecsun 的回复:]
谢谢!再问一个问题,输入的时候怎么将空格键输入进一个字符串呢?
比如说 Mp3 Player
[/Quote]
帅帅没人爱 2011-09-24
  • 打赏
  • 举报
回复
谢谢!再问一个问题,输入的时候怎么将空格键输入进一个字符串呢?
比如说 Mp3 Player
风尘中国 2011-09-24
  • 打赏
  • 举报
回复
使用String.split方法来做吧


import java.util.Scanner;
import java.util.StringTokenizer;
import java.io.IOException;

/**
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 2011-9-24
* Time: 19:05:34
* To change this template use File | Settings | File Templates.
*/
public class Demo1 {
private Product readProduct() throws IOException {

final String DELIM = "_";

String name = "";
int quantity = 0;
double price = 0.0;
/* PLACE YOUR CODE HERE */
System.out.print("Product [Name_Quantity_Price] :");
try
{
String s=new Scanner(System.in).next();
String[] inputs=s.split(DELIM);
// StringTokenizer st=new StringTokenizer(s,DELIM,true);
//
name+=inputs[0];
quantity=Integer.parseInt(inputs[1]);
price=Double.parseDouble(inputs[2]);
}
catch(NumberFormatException e)
{
e.printStackTrace();
}
return new Product(name, quantity, price);
}

public static void main(String[] args)throws Exception{
System.out.println(new Demo1().readProduct());
}
}

class Product{
private String name;
private int quantity;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getQuantity() {
return quantity;
}

public void setQuantity(int quantity) {
this.quantity = quantity;
}

public double getPrice() {
return price;
}

public void setPrice(double price) {
this.price = price;
}

private double price;

public Product(){}

public Product(String name,int quantity,double price){
this.name=name;
this.quantity=quantity;
this.price=price;
}

@Override
public String toString() {
return "Product{" +
"name='" + name + '\'' +
", quantity=" + quantity +
", price=" + price +
'}';
}
}
qybao 2011-09-24
  • 打赏
  • 举报
回复
for example
private Product readProduct() throws Exception {

final String DELIM = "_";

String name = "";
int quantity = 0;
double price = 0.0;
/* PLACE YOUR CODE HERE */
System.out.print("Product [Name_Quantity_Price] :");
try {
String s=new Scanner(System.in).nextLine();
if (!s.matches(".*?_\\d+_\\d+([.]\\d+)?")) { //可以做个一个判断
thorws new Exception("the format of input data is error.");
}
StringTokenizer st=new StringTokenizer(s,DELIM,true);
name+=st.nextToken();
quantity=Integer.parseInt(st.nextToken());
price=Double.parseDouble(st.nextToken());
} catch(NumberFormatException e) {
e.printStackTrace();
thorw e; //把异常抛出,否则要自己return一个null什么的
}
return new Product(name, quantity, price); //要不然这里就不对了
}

58,452

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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