如何读取包含文本和数字的文件并单独读取并将它们相乘? [英] how do I read a file which contains text and numbers and read them seperately and Multiply the together ?

查看:67
本文介绍了如何读取包含文本和数字的文件并单独读取并将它们相乘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个程序应该读取名称,然后乘以后面的数字,但它一直让我输入不匹配异常错误,我似乎无法弄清楚为什么它给我这个错误,

谢谢

I currently have a program which is supposed to read the name and then multiply the numbers which follow but it keeps throwing me the input mismatch exception error,I cant seem to figure out why its giving me this error ,
thank you

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

/**
 *
 * @author Adam
 */
import java.util.Scanner;

public class Incomes2 {


    public static void main(String[] args) {


        int numberRead = 0;
        int numberRead2 = 0;
        int numRead = 0;
        String nameRead; //numberread will find the next number after the word
        int EmmaTotal = 0, SofiaTotal = 0, OliviaTotal = 0;
        int EmmaSum = 0, SofiaSum = 0 ,OliviaSum = 0  ;

        Scanner scan = new Scanner(Incomes2.class.getResourceAsStream("pay.txt"));



        while(scan.hasNext()) {

            nameRead = scan.next();
            numberRead= scan.nextInt();
            numberRead2 = scan.nextInt();

                //scan.nextInt();

            if (nameRead.equals ("Emma"))
            {
                EmmaTotal += numberRead;
                EmmaTotal += numberRead2;




                System.out.println("the Scanner is at this point " + numberRead + " " + numberRead2);



            }


             if (nameRead.equals ("Sofia"))
            {
                SofiaTotal += numberRead;
                SofiaTotal += numberRead2;



            }
             //scan.next();

              if (nameRead.equals ("Olivia"))
            {
                OliviaTotal += numberRead;
                OliviaTotal += numberRead2;




            }




        }

       EmmaSum = (numberRead * numberRead2);
       SofiaSum = (numberRead * numberRead2);
       OliviaSum = (numberRead * numberRead2);
       System.out.println("Emma's total is" + EmmaSum);
       System.out.println("Sofia's total is" + SofiaSum);
       System.out.println("Olivia's total is" + OliviaSum);
       System.out.println("Emma: " +  EmmaTotal);
       System.out.println("Sofia: " +  SofiaTotal);
       System.out.println("Olivia: "  +  OliviaTotal);
    }
    }

推荐答案

文件pay.txt中的人名后面有两个数字。但是你只在循环中读取一个字符串和一个整数。因此,在第一次循环执行后,扫描仪的位置在第一个和第二个数字之间(介于8和2之间)。因此,对 scan.next()的下一次(第二次)调用将2读取为字符串,然后对 scan.nextInt()的调用无法读取Sofia为一个整数。

解决方案:在每个循环中调用 scan.nextInt()两次。



编辑:这个解决方案是指问题的第一个版本。
You have two numbers following a persons name in your file "pay.txt". But you only read a string and one integer in your loop. So after the first loop-execution the scanner's position is between the first and the second number (between 8 and 2). So the next (second) call to scan.next() reads the 2 as a string and then the call to scan.nextInt() fails to read "Sofia" as an integer.
Solution: Call scan.nextInt() twice in each loop.

edit: this solution refers to the first version of the question.


这篇关于如何读取包含文本和数字的文件并单独读取并将它们相乘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆