程序需要计算并显示指定的charector在文本文件中出现的次数.
目前总数为零.我不是,如果我应该使用不同的循环,我也尝试使用'for'循环.
// Hold user input and sum
String fileName; // Holds the name of the file
String letter; // Letter to search for in the file
int total = 0; // Holds the total number of characters in the file
// Get the name of the file and character from the user
fileName = JOptionPane.showInputDialog("Please enter the name of a file:");
letter = JOptionPane.showInputDialog("Please enter a letter contained in the string");
// Open the file for reading
File file …Run Code Online (Sandbox Code Playgroud) 我正在使用Ruby 2.0进行Rails项目,并且在控制台中获取数组的元素长度时遇到了一些问题.
第一个例子
2.0.0-p353 :001 > search = "test"
=> "test"
2.0.0-p353 :002 > search.split
=> ["test"]
2.0.0-p353 :003 > search.length
=> 4
Run Code Online (Sandbox Code Playgroud)
第二个例子
2.0.0-p353 :001 > search = "testOne, TestTwo"
=> "testOne, TestTwo"
2.0.0-p353 :002 > search.split(/[\s,]+/)
=> ["testOne", "TestTwo"]
2.0.0-p353 :003 > search.length
=> 16
Run Code Online (Sandbox Code Playgroud)
如何返回元素计数而不是字符数?
我是Ruby的新手,并且在for循环中连接字符串时遇到了一些问题.
这是我到目前为止所拥有的
# search request
search = ["testOne", "testTwo"]
# Create base url for requests in loop
base_url = "http://example.com/"
# create an empty response array for loop below
response = []
search.each do |element|
response = "#{base_url}#{element}"
end
Run Code Online (Sandbox Code Playgroud)
我想要回复[0]来保存" http://example.com/testOne ".但是,循环执行后,response [0]只保存我的基本变量的第一个字母(h); 响应持有" http://example.com/testTwo ".
我认为这是一个简单的错误,但找不到任何有用的资源.
我正在尝试将其值已经使用JSON语法格式化的JavaScript字符串转换为使用的有效JSON对象JSON.parse.
// JSON formatted string
var string = "{'1451893035': 1.2,'1452670635':0.5,'1451720235': 2.5}";
// parse to JSON object
console.log(JSON.parse(string));
Run Code Online (Sandbox Code Playgroud)
我目前收到此错误:
未捕获的SyntaxError:意外的令牌'