我从网站获取源代码并将代码放入字符串中,它们将如下所示:
501252,110,34496
331550,30,14114
403186,1,18
325033,31,15750
460287,14,2384
286659,11,1366
419439,1,67
678464,1,0
505044,1,70
522192,1,75
454391,1,0
504858,1,20
505396,1,40
469927,1,0
336670,2,155
392887,5,437
403568,1,0
488324,1,0
524031,1,0
429226,1,0
389668,1,0
383021,1,0
384599,1,0
363131,1,0
-1,-1
-1,-1
-1,-1
-1,-1
-1,-1
-1,-1
-1,-1
-1,-1
-1,-1
-1,-1
-1,-1
-1,-1
-1,-1
-1,-1
-1,-1
我想分割每个数字,并将它们保存在自己的字符串中,我怎么能这样做呢?
当前代码:(如果有任何帮助?)
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class dfgdfg {
static String username;
static JTextField USERNAME = new JTextField();
static String pooo;
static JPanel panel;
static JFrame jframe;
static JButton button;
static String line;
public static void main(String args[]) throws Exception {
GUI();
}
public static void getSource() {
URL url;
InputStream is = null;
BufferedReader br;
try {
url = new URL(
"http://services.runescape.com/m=hiscore_oldschool/index_lite.ws?player="+username);
is = url.openStream(); // throws an IOException
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (MalformedURLException mue) {
mue.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
is.close();
} catch (IOException ioe) {
// nothing to see here
}
}
}
public static void GUI() {
jframe = new JFrame("HighscoreLookup");
panel = new JPanel();
USERNAME = new JTextField("Enter Username");
button = new JButton("START");
jframe.setPreferredSize(new Dimension(300, 300));
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setVisible(true);
jframe.add(panel);
panel.add(USERNAME);
panel.add(button);
panel.setBackground(Color.CYAN);
button.setVisible(true);
USERNAME.setSize(100,50);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
username = USERNAME.getText().toString();
System.out.println(username);
getSource();
}
});
jframe.pack();
}
}
答案 0 :(得分:1)
使用Scanner读取文件中的每一行,并在每行上使用String.split(",")。非常有用的实用程序。
String.split
将使用提供的regex语句将您的字符串分解为一维数组。在这里,您只需按每个逗号分隔每一行。
编辑:
根据您的评论,这仍然适合您。
鉴于输入"501252,110,34496"
输出为"501252", "110", "34496"
运行此代码以查看我的意思:
public static void main(String[] args){
String input = "501252,110,34496";
String output = input.split(",");
String[] outputContainsThis = new String[]{"501252", "110", "34496"};
}
这样就够了吗?
答案 1 :(得分:0)
使用字符串类的Split方法:
"data1,data2".Split(',');
//will return an array containing 2strings: data1 then data2
你可以用\ n分割整个东西然后用每条线分割, 或者用扫描仪或一些缓冲读卡器逐行阅读。
答案 2 :(得分:0)
"501252,110,34496".split(",") // => ["501252", "110", "34496"]
答案 3 :(得分:0)
您可以使用char[] cArray = mystring.toCharArray();
答案 4 :(得分:0)
有很多方法可以做到这一点。最简单的方法是使用split:
String inputString = "line 1\nline 2\n";
StringBuilder sb = new StringBuilder();
String[] lines = inputString.split("\\n");
for(String line : lines)
{
System.out.println(line);
}
我这样做的方式大致是这样的,因为它允许简单的错误处理,字符转义和其他处理:
List<String> lines = new ArrayList<String>();
for(int i = 0; i < inputString.length(); i++)
{
char next = inputString.charAt(i);
if(next == '\\')
{
i++;
continue;
}
if(next == '\n')
{
lines.add(sb.toString());
sb = new StringBuilder();
continue;
}
sb.append(next);
}
if(sb.length() > 0)
{
lines.add(sb.toString());
}
for(String line : lines)
{
System.out.println(line);
}
更新:问题已从行更改为字段。您仍然可以使用拆分,但要注意特殊字符,新行字符等。处理此类输入的正确方法是使用CSV解析器来处理输入(请参阅RFC 4180:http://www.ietf.org/rfc/rfc4180.txt) 。