go panic: runtime error: index out of range in Go(已解决)

我有以下函数从终端获取命令并根据输入打印一些东西。看起来很简单,如果用户键入’add’,系统会打印一行,如果用户没有输入任何内容,则会输出其他内容。

每当用户键入add时,它都有效。如果用户没有输入它抛出的任何内容

panic:运行时错误:GoLang中的索引超出范围

为什么是这样?

func bootstrapCmd(c *commander.Command, inp []string) error {


     if inp[0] == "add" {
                  fmt.Println("you typed add")
              } else if inp[0] == "" {
                  fmt.Println("you didn't type add")
              }


          return nil

    }

解决办法:
如果用户未提供任何输入,则该inp数组为空。这意味着即使索引0超出范围,即inp[0]无法访问。

您可以在检查之前检查inpwith 的长度。这样的事情可能会:len(inp)inp[0] == “add”

if len(inp) == 0 {
    fmt.Println("you didn't type add")
} else if inp[0] == "add" {
    fmt.Println("you typed add")
}

你必须检查第一个长度inp:

func bootstrapCmd(c *commander.Command, inp []string) (err error) {
    if len(inp) == 0 {
        return errors.New("no input")
    }
    switch inp[0] {
    case "add":
        fmt.Println("you typed add")
    case "sub":
        fmt.Println("you typed sub")
    default:
        fmt.Println("invalid:", inp[0])
    }
    return nil

}

String index out of range: -91 怎么解决

07-27

String files = "[2010-07-19 08:20:51] Uin=1072, UserName=林立, Addr=XXXX:1047, Action=login, Mode=local, Result=0(success)\n"rn+ "[2010-07-19 22:55:08] Uin=1017, UserName=伟文, Addr=XXXX:1070, Action=logout, Mode=local";rn rn String loginTime="";rn int uin=0;rn String userName="";rn String addr="";rn String action="";rn String mode="";rn String result="";rn String[] lines = files.split("\n");rn for(String line:lines)rn rn vo = new LogVO();rnrn int index1 = line.indexOf("[");rn int index2 = line.indexOf("]");rn loginTime = line.substring(index1+1, index2);rn rn int index3 = line.indexOf("Uin=");rn int index4 = line.indexOf(",", index3);rn uin = Integer.parseInt(line.substring(index3+4, index4));rnrn int index5 = line.indexOf("UserName=");rn int index6 = line.indexOf(",", index5);rn userName = line.substring(index5+9, index6);rnrn int index7 = line.indexOf("Addr=");rn int index8 = line.indexOf(",", index7);rn addr = line.substring(index7+5, index8);rnrn int index9 = line.indexOf("Action=");rn int index10 = line.indexOf(",", index9);rn action = line.substring(index9+7, index10);rnrn int index11 = line.indexOf("Mode=");rn int index12 = line.indexOf(",", index11);rn mode = line.substring(index11+5, index12);//到了这里就说下标越界了rn if(line.contains("Result="))rn rn int index13 = line.indexOf("Result=");rn int index14 = line.indexOf("\n", index13);rn result = line.substring(index13+7, 8);rn rn rn vo.setLoginTime(loginTime);rn vo.setUin(uin);rn vo.setUserName(userName);rn vo.setAddr(addr);rn vo.setAction(action);rn vo.setMode(mode);rn vo.setResult(result);rn list.add(vo);rn rnrn我现在是从日志里面解析出各个字段信息,但是到mode = line.substring(index11+5, index12);//这一行就报错:String index out of range: -91,下面一个result也是越界,这个要怎么改呢?rn

Parameter index out of range 谢谢!

05-27

谢谢大家!帮我看看这个错误怎么回事,谢谢!!rnrnjava.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).rn at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:2493)rn at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:3213)rn at database.LinkDB.addBook(LinkDB.java:35)rn at action.addBookAction.execute(addBookAction.java:37)rn at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)rn at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:226)rn at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)rn at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:415)rn at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)rn at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)rn at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)rn at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)rn at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)rn at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)rn at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)rn at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)rn at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)rn at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)rn at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)rn at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)rn at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)rn at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)rn at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)rn at java.lang.Thread.run(Thread.java:595)rn

Row index out of range问题

06-18

public class Table1 extends JFramern String rid[] = new String[100];rn int count = 0;rn /*rn * public static void main(String a[]) new Table1(); rn */rn rn public void center(JFrame frame) rn double x = frame.getWidth(); // 居中困难rn double y = frame.getHeight();rn Toolkit kit = Toolkit.getDefaultToolkit();rn Dimension screenSize = kit.getScreenSize();rn double xx = screenSize.getWidth();rn double yy = screenSize.getHeight();rn frame.setLocation((int) (xx / 2 - x / 2), (int) (yy / 2 - y / 2));rn rn public Table1() rn JPanel zuida = new JPanel(new BorderLayout(0, 10));rn JPanel jp1 = fangzi();rn JPanel jp2 = dingdan();rn jp2.setBorder(BorderFactory.createTitledBorder("订单信息"));rn JTabbedPane tp = new JTabbedPane();rn tp.addTab("房间预定", jp1);rn tp.addTab("订单信息", jp2);rn // zuida.add(jp1);rn zuida.add("North", tp);rn JFrame frame = new JFrame("酒店");rn frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);rn //zuida.setBackground(new Color(102,204,255)); 设置背景颜色rn zuida.setBorder(new EmptyBorder(5, 5, 5, 5));rn frame.setContentPane(zuida);rn frame.setVisible(true);rn frame.pack();rn center(frame);rn System.out.println("123");rn // frame.setLocationRelativeTo(null);//这个也是居中的方法rn rn JPanel fangzi() rn JTable jiu = jiudian();rn JTable table = order();rn JScrollPane pane = new JScrollPane(jiu); // 加滑轮rn JPanel panel = new JPanel();//rn JPanel panel2 = new JPanel(new FlowLayout(FlowLayout.CENTER, 30, 10));rn JPanel panel3 = new JPanel(new GridLayout(1, 10));rn JPanel panel4 = new JPanel(new BorderLayout(0, 10));rn panel.setPreferredSize(new Dimension(600, 400));rn panel.setLayout(new BorderLayout());rn JButton b1 = new JButton("预订");rn JButton b2 = new JButton("加至购物车");rn JButton b3 = new JButton("预定多个");rn JButton b4 = new JButton("生成订单");rn panel2.add(b1);rn panel2.add(b2);rn panel2.add(b3);rn panel2.add(b4);rn ButtonListener bListener = new ButtonListener(jiu,rid);rn rn JButton b5 = new JButton("查询空房");rn panel3.add(b5);rn rn rn bListener.jPanel = panel;rn bListener.jPanel2 = pane;rn ButtonListener bListener2 = new ButtonListener(table);rn b5.addActionListener(bListener2);rn rn b1.addActionListener(bListener);rn b2.addActionListener(bListener);rn b3.addActionListener(bListener);rn b4.addActionListener(bListener);rnrn panel.add("Center", pane);rn panel.add("South", panel2);rn panel.add("North", panel3);rn panel.setBorder(BorderFactory.createTitledBorder("房间信息"));rn rn rn rn return panel;rn rn JPanel dingdan() rn JTable dingdan = order();rn JScrollPane pane = new JScrollPane(dingdan);rn JPanel jp10 = new JPanel(new BorderLayout());rn jp10.add("Center", pane);rn JButton b1 = new JButton("确定");rn JButton b2 = new JButton("刷新");rn jp10.add("South", b1);rn jp10.add("North",b2);rn ButtonListener bListener1 = new ButtonListener(dingdan);rn b1.addActionListener(bListener1);rn b2.addActionListener(bListener1);rn return jp10;rn rn JTable order() //生成订单的表格rn Vector lieV = new Vector();rn Vector hangV = new Vector();rn try rn Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");rn Connection con;rn con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DataBaseName=Hotel", "sa", "123");rn PreparedStatement statement = con.prepareStatement("Select * from 订单 where Userid =? ");rn statement.setString(1, new MainClass().getname());rn ResultSet rs = statement.executeQuery();rn while (rs.next()) rn Vector hangv = new Vector();rn hangv.add(Integer.parseInt(rs.getString(1)));rn hangv.add(rs.getString(2)); rn hangv.add((rs.getString(3))); rn hangv.add((rs.getString(4))); rn hangv.add((rs.getInt(5))); rn hangv.add((rs.getString(6))); rn hangv.add((rs.getInt(7)));rn hangV.add(hangv);rn rn catch (Exception e) rn System.out.println("Error" + e);rn rn final Object Lie[] = "订单编号", "用户", "房间编号", "订单日期","数量", "状态","价格" ;rn lieV.add("订单编号");rn lieV.add("用户");rn lieV.add("房间编号");rn lieV.add("订单日期");rn lieV.add("数量");rn lieV.add("状态");rn lieV.add("价格");rn rn //JTable jiu = new JTable(row, Lie); // 建立表格rn DefaultTableModel tableModel = new DefaultTableModel(hangV, lieV);rn JTable jiu = new JTable(tableModel); rn jiu.setPreferredScrollableViewportSize(new Dimension(400, 300));rn jiu.setRowHeight(30);rn jiu.setRowMargin(5);rn jiu.setRowSelectionAllowed(true);rn jiu.setSelectionBackground(Color.WHITE);rn jiu.setSelectionForeground(Color.red);rn jiu.setGridColor(Color.BLACK);rn // jiu.selectAll();rn jiu.setRowSelectionInterval(0, 0);rnrn //jiu.clearSelection();rn jiu.setDragEnabled(false);rn jiu.setShowGrid(true);rn jiu.setShowHorizontalLines(false);rn jiu.setShowVerticalLines(false);rn //jiu.doLayout();rn jiu.setBackground(Color.LIGHT_GRAY);rn //ButtonListener bListener = new ButtonListener(jiu, rid);rn //bListener.otherTable = jiu;rn return jiu;rn rn //!!!!rn JTable jiudian() //生成酒店的表格 rn // 用向量来实现动态的大小rn //Object 可以不用了rn Vector lieV = new Vector();rn Vector hangV = new Vector();rn Object row[][] = new Object[30][30];rn try rn Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");rn Connection con;rn con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DataBaseName=Hotel", "sa", "123");rn Statement statement = con.createStatement();rn ResultSet rs = statement.executeQuery("Select * from 房间");rn int x = 0;rn int y = 0;rn rn while (rs.next()) rn Vector hangv = new Vector();rn row[x][y] = Integer.parseInt(rs.getString("rid"));rn hangv.add(row[x][y]);rn y++;rn row[x][y] = rs.getInt("Rprice");rn hangv.add(row[x][y]);rn y++;rn row[x][y] = (rs.getString("raddress"));rn hangv.add(row[x][y]);rn y++;rn row[x][y] = (rs.getString("rtype"));rn hangv.add(row[x][y]);rn y++;rn row[x][y] = (rs.getString("rstate"));rn hangv.add(row[x][y]);rn x++;rn y = 0;rn hangV.add(hangv);rn rn catch (Exception e) rn System.out.println("Error" + e);rn rn final Object Lie[] = "编号", "价格", "位置", "类型", "状态" ;rn lieV.add("编号");rn lieV.add("价格");rn lieV.add("位置");rn lieV.add("类型");rn lieV.add("状态");rn rn //JTable jiu = new JTable(row, Lie); // 建立表格rn DefaultTableModel tableModel = new DefaultTableModel(hangV, lieV);rn JTable jiu = new JTable(tableModel); rn jiu.setPreferredScrollableViewportSize(new Dimension(400, 300));rn jiu.setRowHeight(30);rn jiu.setRowMargin(5);rn jiu.setRowSelectionAllowed(true);rn jiu.setSelectionBackground(Color.WHITE);rn jiu.setSelectionForeground(Color.red);rn jiu.setGridColor(Color.BLACK);rn // jiu.selectAll();rn jiu.setRowSelectionInterval(0, 0);rnrn //jiu.clearSelection();rn jiu.setDragEnabled(false);rn jiu.setShowGrid(true);rn jiu.setShowHorizontalLines(false);rn jiu.setShowVerticalLines(false);rn jiu.doLayout();rn jiu.setBackground(Color.LIGHT_GRAY);rnrn return jiu;rn rnrnrnrnrnErrorjava.lang.IllegalArgumentException: Row index out of rangern实在不知道怎么修改rn

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试