如何从文本文件读取,并根据不同的组显示特定的文字(数字)从该文本文件 [英] How to read from textfile and display specific words(numbers) from that txtfile based on diffrent groups

查看:141
本文介绍了如何从文本文件读取,并根据不同的组显示特定的文字(数字)从该文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从文本文件中读取并根据不同的组显示特定的文本文件(数字)?



我有一个包含这个文本文件: p>

  [电脑] 
键盘= 3
鼠标= 5

[动物]
cow = 10
pig = 5

另一个程序。



我想制作一个可以显示如下的表单:

 汽车:3个键盘/ n 
5个鼠标

动物10个母牛
5个猪

我不知道该怎么做。我知道如何从文件中读取,但未来...

这是我的框架将如何看$ b $包nioCount;

  import java.awt.BorderLayout; 
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class FrameTest extends JFrame {

private JPanel contentPane;

/ **
*启动应用程序。

public static void main(String [] args){
EventQueue.invokeLater(new Runnable(){
public void run(){
try {
FrameTest frame = new FrameTest();
frame.setVisible(true);
} catch(Exception e){
e.printStackTrace();
}
}
});
}

/ **
*创建框架。
* /
public FrameTest(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100,100,450,300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5,5,5));
setContentPane(contentPane);
contentPane.setLayout(null);

JButton btnCount = new JButton(Count);
btnCount.setBounds(80,202,89,23);
contentPane.add(btnCount);

JButton btnResetCount = new JButton(Reset);
btnResetCount.setBounds(268,202,89,23);
contentPane.add(btnResetCount);

JTextArea txtrComputers = new JTextArea();
txtrComputers.setText(Computers:);
txtrComputers.setBounds(24,59,84,22);
contentPane.add(txtrComputers);

JTextArea txtrKeyboards = new JTextArea();
txtrKeyboards.setText(3 keyboards);
txtrKeyboards.setBounds(147,43,130,23);
contentPane.add(txtrKeyboards);

JTextArea txtrMouses = new JTextArea();
txtrMouses.setText(10 mouses);
txtrMouses.setBounds(147,77,130,22);
contentPane.add(txtrMouses);

JTextArea txtrAnimals = new JTextArea();
txtrAnimals.setText(Animals:);
txtrAnimals.setBounds(24,133,84,22);
contentPane.add(txtrAnimals);

JTextArea txtrDogs = new JTextArea();
txtrDogs.setText(3 Dogs);
txtrDogs.setBounds(147,110,130,23);
contentPane.add(txtrDogs);

JTextArea txtrCats = new JTextArea();
txtrCats.setText(15 cats);
txtrCats.setBounds(147,152,130,23);
contentPane.add(txtrCats);

code










$ b $ p


$ b

而当我按计数按钮,它会计为每个类别..

这是我的.txt文件包含:

  [电话] 
坏= 1
好​​= 30

[动物]
坏= 10
good = 30


解决方案

追踪。
$ b $ ol

  • 类别(即电脑,动物)

  • 描述符(即键盘,小猪)
  • 计数(即5,10)

    我们有以下依赖关系:


    1. 一个 Category 可以有一个或多个描述符 s

    2. 描述符有一个 Count

    有几种方法可以做到这一点,但是我会根据您的描述在这里简单介绍一下。


    数字rs会一直被另一个程序所改变

    我想这意味着我们需要能够跟踪描述符并更改它,可能不知道 Category 是什么。例如, setValue(Keyboard,10);



    的示例方法我们不知道键盘是与电脑相关的,并通过搜索所有类别来查找键盘是时间密集型的。

    既然你说你知道如何读取文件,我们将留给你,而是描述如何处理伪代码。



    首先,我们需要两个地图。为了简单起见,我将使用HashMaps。

      HashMap< String,ArrayList< String>> categoryToDescriptor = new HashMap<>(); 
    HashMap< String,Integer> descriptorToCount = new HashMap<>();

    第一张地图存储我们所有的类别,第二张地图存储我们所有的计数。



    假设我们接受文本输入,并遇到[Computer]。我们需要把这个添加到我们的类别映射中:
    $ b $ $ $ $ $ $ $ $ $ $ $> categoryToDescriptor.put(Computer,new ArrayList< String>);

  • 在你的情况中,你需要一个变量来解析出Computer和所有的变量。 / p>

    之后,我们得到描述符和值,例如keyboard = 5



    我们用这个信息做两件事。你可以把它解析出来,但是我会硬编码的。

     字符串描述符=keyboard; 
    int count = 5;

    categoryToDescriptor.get(Computer).add(keyboard);
    descriptorToCount.put(keyboard,5);

    现在,我们将类别映射到描述符列表,并将描述符映射到与其关联的计数。



    使用

      for(Map.Entry< ; String,ArrayList< String>> entry:categoryToDescriptor.entrySet()){
    System.out.println(Category:+ entry.getKey());
    ArrayList< String> listOfDescriptors = entry.getValue();
    for(String descriptor:listOfDescriptors){
    System.out.println(Descriptor:+ descriptor +Value:+ descriptorToCount.get(descriptor);)
    }
    System.out.println(); //类别之间的换行
    }

    另外,要更新一个描述符的值,我们所要做的就是:

    $ p $ $ $ $ $ descriptorToCount.put(keyboard, NEWVALUE);


    How to read from a textfile and display specific words(numbers) from that textfile based on different groups?

    I have a txt file that contains this:

    [computers]
    keyboards =3
    mouse  =5
    
    [animals]
    cow =10
    pig =5
    

    The numbers will always be changed by another program.

    I want to make a form that can display the like this:

    Cars:    3 keyboards/n
             5 mouse
    
    animals 10 cows
            5  pigs
    

    I don`t know how to do that. I know how to read from file but the next...

    This is how my frame will look package nioCount;

    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import javax.swing.JButton;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    
    public class FrameTest extends JFrame {
    
    private JPanel contentPane;
    
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    FrameTest frame = new FrameTest();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    
    /**
     * Create the frame.
     */
    public FrameTest() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
    
        JButton btnCount = new JButton("Count");
        btnCount.setBounds(80, 202, 89, 23);
        contentPane.add(btnCount);
    
        JButton btnResetCount = new JButton("Reset");
        btnResetCount.setBounds(268, 202, 89, 23);
        contentPane.add(btnResetCount);
    
        JTextArea txtrComputers = new JTextArea();
        txtrComputers.setText("Computers:");
        txtrComputers.setBounds(24, 59, 84, 22);
        contentPane.add(txtrComputers);
    
        JTextArea txtrKeyboards = new JTextArea();
        txtrKeyboards.setText("3 keyboards");
        txtrKeyboards.setBounds(147, 43, 130, 23);
        contentPane.add(txtrKeyboards);
    
        JTextArea txtrMouses = new JTextArea();
        txtrMouses.setText("10 mouses");
        txtrMouses.setBounds(147, 77, 130, 22);
        contentPane.add(txtrMouses);
    
        JTextArea txtrAnimals = new JTextArea();
        txtrAnimals.setText("Animals:");
        txtrAnimals.setBounds(24, 133, 84, 22);
        contentPane.add(txtrAnimals);
    
        JTextArea txtrDogs = new JTextArea();
        txtrDogs.setText("3 Dogs");
        txtrDogs.setBounds(147, 110, 130, 23);
        contentPane.add(txtrDogs);
    
        JTextArea txtrCats = new JTextArea();
        txtrCats.setText("15 cats");
        txtrCats.setBounds(147, 152, 130, 23);
        contentPane.add(txtrCats);
    }
    

    }

    and when i press count button it will count for each category..

    This is what my .txt file contains:

    [phone]
    bad=1
    good=30
    
    [animals]
    bad=10
    good=30
    

    解决方案

    We have the following objects to keep track of.

    1. Category (i.e. Computer, Animal)
    2. Descriptor (i.e. Keyboard, Pig)
    3. Count (i.e. 5, 10)

    We have the following dependencies.

    1. A Category can have 1 or more Descriptors.
    2. A Descriptor has one Count.

    There are several approaches to doing this, but I'll go with the one easiest based on your description here.

    The numbers will always be changed by another program

    I assume this means we need to be able to track Descriptor and change it, possibly without knowing what the Category is. For example, an example method of setValue("Keyboard", 10);

    We don't know the Keyboard is related to the computer, and searching through all the categories to find the Keyboard is time-intensive.

    Since you say you know how to read the file, I'll leave that up to you, and instead describe what to do with pseudocode.

    First of all, we need two Maps. For simplicity, I'll use HashMaps.

    HashMap<String, ArrayList<String>> categoryToDescriptor = new HashMap<>();
    HashMap<String, Integer> descriptorToCount = new HashMap<>();
    

    The first map stores all of our categories, the second map stores all of our counts.

    Let's say we take the text input and come across "[Computer]". We need to add this to our category map:

    categoryToDescriptor.put("Computer", new ArrayList<String>);
    

    In your case, you'd have a variable to parse out Computer, and all the variables.

    After that, we get descriptors and values, such as "keyboard=5"

    We do two things with this info. You can parse it out, but I'll hardcode it.

    String descriptor = "keyboard";
    int count = 5;
    
    categoryToDescriptor.get("Computer").add("keyboard");
    descriptorToCount.put("keyboard",5);
    

    We are now mapping categories to a list of descriptors, and descriptors to their associated counts.

    Using entryset iteration, we can do something like this to print out all the values.

    for (Map.Entry<String, ArrayList<String>> entry : categoryToDescriptor.entrySet()) {
        System.out.println("Category: " + entry.getKey());
        ArrayList<String> listOfDescriptors = entry.getValue();
        for(String descriptor: listOfDescriptors) {
            System.out.println("Descriptor: " + descriptor + " Value: " + descriptorToCount.get(descriptor);)
        }
        System.out.println("");//line feed between categories
    }
    

    Additionally, to update a value of a descriptor, all we have to do is:

    descriptorToCount.put("keyboard", newValue);
    

    这篇关于如何从文本文件读取,并根据不同的组显示特定的文字(数字)从该文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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