在文本文件中发现的3重发生的名称(人)总收入 [英] Finding the total Income of 3 re-occurring names (people) in a text file

查看:105
本文介绍了在文本文件中发现的3重发生的名称(人)总收入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力,现在解决这个问题的一个公平的几天,我只是设法解决它的一半,就是困扰我似乎有点更具挑战性的一个部分,并想知道如果我可以指出在正确的方向,我怎么能解决它。

我有3名是保持在每行(按随机顺序)的文本文件再次发生,并与各那些名字2号旁边那些重新present价格和数量。 (如下图所示)

 杰克·8 2
乔·4 2
迈克·14 5
杰克3 3
杰克·9 1
杰克2
迈克·20 6
索非亚11 3
杰克·13 6
迈克·8 5
乔·8 4
索非亚8 1
索非亚1 6
索非亚9 4


  • 我已成功地繁殖,每行这2号码我的答案在每个名字旁边。 (第一个问题解决)


  • 我有困难应对接下来的部分,就是我会如何添加所有的数字加在一起并排出现成共3个人的名字。


我已经思考我是否应该使用开关,而如果,否则循环和/或阵列,但我似乎不能让我围绕如何实现我想要的结果头。我开始怀疑是否不是我目前的code(如下图所示)已经在错误的方向迈出的一步获取的3名总收入。

\r
\r

字符串名称;\r
INT leftNum,rightNum;\r
\r
//扫描文本文件\r
扫描程序扫描=新的扫描仪(Explore.class.getResourceAsStream(pay.txt));\r
\r
而(scan.hasNext()){//找到下一行\r
  名称= scan.next(); //找到就行了名\r
  leftNum = scan.nextInt(); //获取价格\r
  rightNum = scan.nextInt(); //获取量\r
  INT ANS = leftNum * rightNum; //价格乘以数量配套和\r
  的System.out.println(名+:+ ANS);\r
}\r
\r
//如果名字是杰克,\r
//获取这个数字旁边他的名字\r
//所有的数字旁边叫杰克相加\r
//取得总杰克加在一起的数字\r
\r
//如果别人叫麦克,\r
//执行相同的步骤,上述杰克和总找\r
\r
//如果其他人的名字是乔,\r
//同上,发现总

\r

\r
\r

我最近的想法是琢磨使用的如果,如果我似乎还有循环,但不能想办法让Java来读取一个名字,让旁边的数字。然后找到它的多个同名的所有行,最后加入旁边的人的名字所有的数字。重复3名。

我的道歉,如果我做了这个看起来复杂得多,它可能是,但我已经得到了相当最近失去了感觉我已经打另一个砖墙。


解决方案

有关的 地图<弦乐,朗GT&;

 地图<弦乐,朗GT&; nameSumMap =新的HashMap<>(3);
而(scan.hasNext()){//找到下一行
    名称= scan.next(); //找到就行了名
    leftNum = scan.nextInt(); //获取价格
    rightNum = scan.nextInt(); //获取量    长总和= nameSumMap.get(名);
    如果(总和== NULL){//我们首次看到名
        nameSumMap.put(姓名,Long.valueOf(leftNum + rightNum));
    }其他{
        nameSumMap.put(姓名,金额+ leftNum + rightNum);
    }
}

目前结束时,映射包含与每个名称相关联的总和。

I have been trying to tackle this problem for a fair few days now and I have only managed to solve half of it, the next part that is troubling me seems to be a little more challenging and was wondering if I could be pointed in the right direction as to how I can tackle it.

I have 3 names that keep re-occurring in a text file on each line (in a random order) and with each of those names 2 numbers next to them that represent Price and Quantity. (As shown below)

Jack 8 2
Joe 4 2
Mike 14 5
Jack 3 3
Jack 9 1
Jack 2 2
Mike 20 6
Sofia 11 3
Jack 13 6
Mike 8 5
Joe 8 4
Sofia 8 1
Sofia 1 6
Sofia 9 4

  • I have managed to multiply those 2 numbers on each line for my answer next to each name. (First problem solved)

  • The next part I am having difficulty tackling, is how I am going to add together all the numbers next to each of the 3 individual names that appear into a total.

I have pondered whether I should use Switch, While, if, else loops and/or arrays but I can't seem to get my head around how to achieve my desired result. I have started to doubt whether or not my current code (Shown below) has gone a step in the wrong direction for getting the total income of the 3 names.

String name;
int leftNum, rightNum;

//Scan the text file
Scanner scan = new Scanner(Explore.class.getResourceAsStream("pay.txt"));

while (scan.hasNext()) { //finds next line
  name = scan.next(); //find the name on the line
  leftNum = scan.nextInt(); //get price
  rightNum = scan.nextInt(); //get quantity
  int ans = leftNum * rightNum; //Multiply Price and Quanity
  System.out.println(name + " : " + ans);
}

// if name is Jack,
//  get that number next to his name
//  and all the numbers next to name Jack are added together
// get total of the numbers added together for Jack

// if else name is Mike,
// Do the same steps as above for Jack and find total

// if else name is Joe,
// same as above and find total

My latest thoughts were pondering the use of an if, if else loop but I can't seem to think of a way to get Java to read a name and get the number next to it. Then find all lines with the same name for its number, finally adding all the numbers next to that persons name. Repeat for the 3 names.

My apologies if I've made this seem more complicated than it may be but I've gotten quite lost recently and feel I've hit another brick wall.

解决方案

What about a Map<String, Long>:

Map<String, Long> nameSumMap = new HashMap<>(3);
while (scan.hasNext()) {       //finds next line
    name = scan.next();        //find the name on the line
    leftNum = scan.nextInt();  //get price
    rightNum = scan.nextInt(); //get quantity

    Long sum = nameSumMap.get(name);
    if(sum == null) {          // first time we see "name"
        nameSumMap.put(name, Long.valueOf(leftNum + rightNum));
    } else {
        nameSumMap.put(name, sum + leftNum + rightNum);
    }
}

At the end, the map contains the sum associated with each name.

这篇关于在文本文件中发现的3重发生的名称(人)总收入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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