如何使用init()方法将修改应用到toString()?

时间:2014-02-23 14:29:29

标签: java

当我想通过toString()方法显示我的程序结果时,我遇到了问题。

我的第二个输入结果是“0.0”,我想要输入的值。

public void init()
{
    System.out.println("Enter name: ");
    Scanner input = new Scanner(System.in);

    if (input.hasNextLine())
    {
        setName(input.nextLine());
    }

    System.out.println("Enter number (double): ");
    Scanner input2 = new Scanner(System.in);

    if (input2.hasNextDouble())
    {
        setNumber(input2.nextDouble());
    }
}

我的toString方法:

public String toString()
{
    return this.name + " - " + this.number;
}

编辑:

import java.util.*;
import java.lang.*;

public class Branche 
{
    private String name;
    private double number;


    public Branche()
    {

    }

    public String getName()
    {
        return this.name;
    }

    public double getNumber()
    {
        return this.number;
    }

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

    public void setNumber(double c)
    {
        this.number = c;
    }

    public String toString()
    {
        return this.name + " - " + this.number;
    }

    public void init()
    {
        System.out.println("Enter name: ");
        Scanner input = new Scanner(System.in);

        if (input.hasNextLine())
        {
            setName(input.nextLine());
        }

        System.out.println("Enter number (double): ");
        Scanner input2 = new Scanner(System.in);

        if (input2.hasNextDouble())
        {
            setNumber(input2.nextDouble());
        }
    }
}

识别TestClass:

import java.util.*;

public class TestBranche 
{
    public static void main(String [] args)
    {
        Branche b1 = new Branche();

        b1.init();

        System.out.println(b1);
    }
}

3 个答案:

答案 0 :(得分:2)

我已经测试了下面的代码并且它正常工作,请检查你的setname()和setNumber()函数。

您只需要一个Scanner对象来获取输入,无需来创建多个扫描程序对象

public class NewClass {

String name;
double number;

    public void init() {

        System.out.println("Enter name: ");
        Scanner input = new Scanner(System.in);

        if (input.hasNextLine()) {
            setName(input.nextLine());
        }

        System.out.println("Enter number (double): ");

        if (input.hasNextDouble()) {
            setNumber(input.nextDouble());
        }
    }

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

    void setNumber(double number) {
        this.number = number;
    }

    public String toString() {
        return this.name + " - " + this.number;
    }

    public static void main(String args[]) throws Exception {

        NewClass obj = new NewClass();
        obj.init();
        System.out.println(obj);



    }
}

输出

run:
Enter name: 
abc
Enter number (double): 
89.78
abc - 89.78
BUILD SUCCESSFUL (total time: 7 seconds)

答案 1 :(得分:1)

我猜你的问题就在这里:

if (input2.hasNextDouble())
    setNumber(input2.nextDouble());
}

扫描仪可能没有有效的双倍。

答案 2 :(得分:0)

一些修正:

// identation
public void init() {
    // You don't need and shouldn't open two scanners
    final Scanner input = new Scanner(System.in);

    // Makes your code clear by separating statements
    System.out.println("Enter name: ");  
    String iName = input.nextLine(); 
    setName(iName);

    System.out.println("Enter number (double): ");
    double iNumber = input.nextDouble();
    setNumber(input2.nextDouble());
}

这就是setNumber看起来的样子:

public void setNumber(double number) {
    this.number = number;
}

由于您没有提供任何类型的重试代码(如果您的用户输入其他内容而不是双倍的&gt; hasNext()将无法设置),我建议使用number方法。< / p>


如何保证用户输入的内容可以解析为double

public double readDouble(String message, Scanner sc) {
    Double result = null;
    do {
        System.out.print(message + " (double): ");
        String input = sc.nextLine();
        try {
            result = Double.valueOf(input);
        } catch (NumberFormatException e) {
            System.err.println("* ERROR: Input is not a number");
        }
    } while (result == null);

    return result;
}

用法:

double iDouble = readDouble("Enter number", sc);

此代码避免了hasNextDouble()nextDouble()的许多陷阱;例如,用户不能尝试以下内容:

> Enter number (double): 123 something

哪个会通过hasNextDouble测试,但会在流上留下something