首页 \ 问答 \ 从txt文件中读取ints并存储到数组中(Reading ints from a txt file and storing to an array)

从txt文件中读取ints并存储到数组中(Reading ints from a txt file and storing to an array)

我正在尝试从文本文件中读取整数并将它们存储到数组中。 该文本文件显示如下:

4
-9
-5
4
8
25
10
0
-1
4
3
-2
-1
10
8
5
8

然而,当我运行我的代码时,我在控制台窗口中获得了[I@41616dd6 ...

public static void main(String[] args) throws IOException
    {
        FileReader file = new FileReader("Integers.txt");
        int[] integers = new int [100];
        int i=0;
        try {
            Scanner input = new Scanner(file);
            while(input.hasNext())
            {
                integers[i] = input.nextInt();
                i++;
            }
            input.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        System.out.println(integers);
    }

I am trying to read integers from a text file and store them into an array. The text file reads:

4
-9
-5
4
8
25
10
0
-1
4
3
-2
-1
10
8
5
8

Yet when I run my code I get [I@41616dd6 in the console window...

public static void main(String[] args) throws IOException
    {
        FileReader file = new FileReader("Integers.txt");
        int[] integers = new int [100];
        int i=0;
        try {
            Scanner input = new Scanner(file);
            while(input.hasNext())
            {
                integers[i] = input.nextInt();
                i++;
            }
            input.close();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        System.out.println(integers);
    }

原文:https://stackoverflow.com/questions/13716830
更新时间:2022-02-20 18:02

最满意答案

将提示提示给您期望的内容,如...提示。 是paramiko交互示例。 请注意第21行和第37行 -

PROMPT = 'vagrant@paramiko-expect-dev:~\$\s+'

interact.expect(PROMPT)

所以,当我将部分代码更新为:

interact = SSHClientInteraction(client, timeout=10, display=True)
interact.expect(PROMPT)

interact.send("ls")
interact.expect(".*Maildir.*")
file.write(interact.current_output_clean)
client.close()

我有testlog.txt填充主目录的列表,远程主机。

作为一个方面说明 - 切换到python 3.如果你开始,最好使用不知名的工具很快就会过时。 你也可以使用ipython,或者jupyter--代码更具交互性,测试速度更快。 也许netmiko会对你感兴趣?


Put prompt to something you expect, as... prompt. Here is paramiko interaction example. Please note lines 21, and 37 -

PROMPT = 'vagrant@paramiko-expect-dev:~\$\s+'

interact.expect(PROMPT)

So, when I've updated part of your code to:

interact = SSHClientInteraction(client, timeout=10, display=True)
interact.expect(PROMPT)

interact.send("ls")
interact.expect(".*Maildir.*")
file.write(interact.current_output_clean)
client.close()

I have testlog.txt filled with listing of the home directory, of remote host.

As a side note - switch to python 3. If you are starting, it is better to use tool that is not well known to be outdated soon. Also you can use ipython, or jupyter - code will be more interactive, faster to test. Maybe netmiko will be interested for you?

相关问答

更多
  • linux expect问题[2023-01-15]

    #!/usr/bin/expect set timeout 10 set host_ip [lindex $argv 0] set password [lindex $argv 1] spawn ssh ${host_ip} expect { "(yes/no)?" { send "yes\n" expect "*assword:" { send "$password\n"} } "*assword:" { send "$password\n" } timeout {exit 1} # 3: 这里超时就会返 ...
  • 可能是模态始终存在,但它的可见性是否被切换? 然后,我怀疑你应该检查元素是否可见而不是存在 : expect(modal.isDisplayed()).toBeTruthy(); 另请参见如何使用量角器检查元素是否可见? 。 然后,您可以使用@alecxe建议的预期条件,使用ExpectedConditions.visibilityOf 。 我也觉得有点怀疑click()承诺后面没有。 以下工作如何? element(by.css(merchantInfoClose)).click().then(func ...
  • 我是paramiko-expect的作者。 我刚刚在我的模块的0.2中实现了一个新功能,它允许你指定回调尾部方法,以便你可以随意处理当前行。 您可以使用它来grep输出或在显示之前进一步处理它。 预计回调函数将在完成操作当前行后返回要发送到sys.stdout.write的字符串。 这是一个例子: import traceback import paramiko from paramikoe import SSHClientInteraction def process_tail(line_prefix ...
  • 试试这个(只修改:“.timeout(5000)”被添加到“it”)。 这对我有用。 基本上你必须改变异步呼叫的默认超时时间2秒 - 如果你期望你的异步呼叫将花费2秒以上。 describe('A promise', () => { it('should not timeout', () => { return new Promise((resolve, reject) => { setTimeout(() => { resol ...
  • 不幸的是,你在WPF中创建了一个死锁。 在同步调用Stop时阻止UI线程。 服务器进程Stop请求并在返回客户端之前处理所有回调。 从服务器回调是同步处理的,因此阻止从Stop返回,直到WPF中的回调处理程序处理StatusUpdate回调。 但是StatusUpdate处理程序无法启动,因为它需要UI线程 - 并且UI线程仍在等待原始请求Stop完成。 如果您使用的是.NET 4.5,则解决方案很简单。 您将“click”处理程序标记为async并在客户端中调用await client.StopAsync ...
  • 您可以自己阅读( expect_user )用户的密码,然后send其send到spawn'ed程序。 例如: [STEP 101] # cat foo.exp proc expect_prompt {} \ { global spawn_id expect -re {bash-[.0-9]+(#|\$)} } spawn ssh -t 127.0.0.1 bash --noprofile --norc expect "password: " stty -echo expect_user ...
  • 除非这是一个学术练习,或者你有一些特定的要求使用ssh,看看有点咄咄逼人 。 我从来没有使用它,但似乎成熟了。 Unless this is an academic exercise, or you have some specific requirement to use ssh, have a look at pushy. I've never used it but it seems mature.
  • 如评论中所示,性能计时插件使用s.performanceTiming(); 触发实际的插件。 这就是说它应该如何配置才能正常工作。 s.pte是用于确定是否执行实际插件的调用。 `s.pte = 'event10,event11,event12,event13,event14,event15,event16,event17,event18,event19' //[--------------------------- 1 to 8 ---------------------------][-- 9 --][ ...
  • 将提示提示给您期望的内容,如...提示。 这是paramiko交互示例。 请注意第21行和第37行 - PROMPT = 'vagrant@paramiko-expect-dev:~\$\s+' interact.expect(PROMPT) 所以,当我将部分代码更新为: interact = SSHClientInteraction(client, timeout=10, display=True) interact.expect(PROMPT) interact.send("ls") intera ...
  • 如前所述,您必须定义您期望的提示,但也要注意它被读作正则表达式; 因此,必须转义特殊字符(如'$')。 我通常按如下方式设置提示: prompt = '.*\$\s+' sudo_prompt = '\[sudo\] password .*:\s+' root_prompt = '.*\#\s+' As already mentioned, you must define the prompt you are expecting, but also note that it's read as a reg ...

相关文章

更多

最新问答

更多
  • python的访问器方法有哪些
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。
  • 响应navi重叠h1和nav上的h1链接不起作用(Responsive navi overlaps h1 and navi links on h1 isn't working)
  • 在C中读取文件:“r”和“a +”标志的不同行为(Reading a File in C: different behavior for “r” and “a+” flags)
  • NFC提供什么样的带宽?(What Kind of Bandwidth does NFC Provide?)
  • 元素上的盒子阴影行为(box-shadow behaviour on elements)
  • Laravel检查是否存在记录(Laravel Checking If a Record Exists)
  • 设置base64图像的大小javascript - angularjs(set size of a base64 image javascript - angularjs)
  • 想学Linux 运维 深圳有哪个培训机构好一点
  • 为什么有时不需要在lambda中捕获一个常量变量?(Why is a const variable sometimes not required to be captured in a lambda?)
  • 在Framework 3.5中使用服务器标签<%=%>设置Visible属性(Set Visible property with server tag <%= %> in Framework 3.5)
  • AdoNetAppender中的log4net连接类型无效(log4net connection type invalid in AdoNetAppender)
  • 错误:发送后无法设置标题。(Error: Can't set headers after they are sent. authentication system)
  • 等待EC2实例重启(Wait for an EC2 instance to reboot)
  • 如何在红宝石中使用正则表达式?(How to do this in regex in ruby?)
  • 使用鼠标在OpenGL GLUT中绘制多边形(Draw a polygon in OpenGL GLUT with mouse)
  • 江民杀毒软件的KSysnon.sys模块是什么东西?
  • 处理器在传递到add_xpath()或add_value()时调用了什么顺序?(What order are processors called when passed into add_xpath() or add_value()?)
  • sp_updatestats是否导致SQL Server 2005中无法访问表?(Does sp_updatestats cause tables to be inaccessible in SQL Server 2005?)
  • 如何创建一个可以与持续运行的服务交互的CLI,类似于MySQL的shell?(How to create a CLI that can interact with a continuously running service, similar to MySQL's shell?)
  • AESGCM解密失败的MAC(AESGCM decryption failing with MAC)
  • SQL查询,其中字段不包含$ x(SQL Query Where Field DOES NOT Contain $x)
  • PerSession与PerCall(PerSession vs. PerCall)
  • C#:有两个构造函数的对象:如何限制哪些属性设置在一起?(C#: Object having two constructors: how to limit which properties are set together?)
  • 平衡一个精灵(Balancing a sprite)
  • n2cms Asp.net在“文件”菜单上给出错误(文件管理器)(n2cms Asp.net give error on Files menu (File Manager))
  • Zurb Foundation 4 - 嵌套网格对齐问题(Zurb Foundation 4 - Nested grid alignment issues)
  • 湖北京山哪里有修平板计算机的