标签: indexoutofboundsexception

字符串索引超出范围错误7

可能重复:
字符串索引超出范围:n

我正在编写一个程序来根据用户的输入(名字,中间名和姓氏)生成用户名。我应该从每个名称(名字,中间名和姓氏)中获取第一个字符,以及姓氏的最后一个字符,以便生成用户名。我已经成功编写了程序以生成每个名称的第一个字符,但是当我尝试让程序生成姓氏的最后一个字符时,会出现此错误:

线程“主”中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:7

at java.lang.String.charAt(String.java:658)
at UsernameGenerator.main(UsernameGenerator.java:39)
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

    import java.util.Scanner;
    /**
    UsernameGenerator.java
    Generates a username based on the users inputs.
    @author: Evan Fravert
    */
public class UsernameGenerator {
/**
  * Generates a username based on the users inputs.
  *@param args command line argument
  */
  public static void main(String[] args)
{ // abcde
  String first;
  String middle;
  String last;
  String password1;
  String password2;
  int randomNum;
  randomNum = (int) (Math.random() * 1000) + 100;
  Scanner userInput = new Scanner(System.in);
  System.out.println("Please enter …
Run Code Online (Sandbox Code Playgroud)

java string indexoutofboundsexception

0
推荐指数
1
解决办法
5508
查看次数

ArrayList和IndexOutOfBounds异常

所以我在写一些代码时得到一个索引超出范围的异常.我不明白的是,我知道我正在尝试使用的索引元素存在.

这是代码:

我有一个数组列表的构造函数

    public StixBoard(int number)
{
    stixGame = new ArrayList<Integer>(number);

    for (int i = 0; i < number; i++)
    {
        stixGame.add(i);
    }

}
Run Code Online (Sandbox Code Playgroud)

该块生成随机变量1-3

public int computeMove()
{

    int numberOfStix = (int) (3.0 * Math.random()) + 1;

    return numberOfStix;
}
Run Code Online (Sandbox Code Playgroud)

真的很直接,现在我在这里有一个方法,它接受提供的参数,并尝试从数组列表中删除这些数量的元素.如您所见,参数必须介于1和3之间,且必须小于或等于数组列表的大小.否则,提示用户输入另一个号码

public boolean takeStix(int number)
{
    boolean logicVar = false;
    placeHolder = stixGame.size();

    if ((number >= 1 && number <= 3) && number <= placeHolder)
    {
        for (int i = 0; i < number; i++)
        {
            stixGame.remove(i);
            logicVar = …
Run Code Online (Sandbox Code Playgroud)

java indexoutofboundsexception

0
推荐指数
1
解决办法
1617
查看次数

我尝试获取SQLite数据时的StringIndexOutOfBoundsException

当我尝试从我的Activity中的数据库SQLite获取数据时

DataBaseAdapterCars mDbHelper = new DataBaseAdapterCars(this);
        mDbHelper.createDatabase();
        mDbHelper.open();

        Car[] carsList= mDbHelper.getDataDBCarsArray(uso, eta,
        category, class);
Run Code Online (Sandbox Code Playgroud)

我收到这个错误

12-10 17:14:47.069: E/AndroidRuntime(17936): FATAL EXCEPTION: main
12-10 17:14:47.069: E/AndroidRuntime(17936): java.lang.StringIndexOutOfBoundsException
12-10 17:14:47.069: E/AndroidRuntime(17936):    at android.app.ContextImpl.validateFilePath(ContextImpl.java:1649)
12-10 17:14:47.069: E/AndroidRuntime(17936):    at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:555)
12-10 17:14:47.069: E/AndroidRuntime(17936):    at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
12-10 17:14:47.069: E/AndroidRuntime(17936):    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118)
12-10 17:14:47.069: E/AndroidRuntime(17936):    at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187)
12-10 17:14:47.069: E/AndroidRuntime(17936):    at com.my.app.db.DataBaseHelper.createDataBase(DataBaseHelper.java:36)
12-10 17:14:47.069: E/AndroidRuntime(17936):    at com.my.app.db.DataBaseAdapterCars.createDatabase(DataBaseAdapterCars.java:42)
12-10 17:14:47.069: E/AndroidRuntime(17936):    at com.my.app.Cars.initializeData(Cars.java:111)
12-10 17:14:47.069: E/AndroidRuntime(17936):    at com.my.app.Cars$MyAsync$1.run(Cars.java:156)
12-10 17:14:47.069: E/AndroidRuntime(17936):    at android.os.Handler.handleCallback(Handler.java:587)
12-10 17:14:47.069: E/AndroidRuntime(17936):    at android.os.Handler.dispatchMessage(Handler.java:92) …
Run Code Online (Sandbox Code Playgroud)

sqlite android indexoutofboundsexception android-sqlite

0
推荐指数
1
解决办法
1071
查看次数

JTable不会删除行

我需要从中删除行JTable.我写了这样的代码:

DefaultTableModel dtm = (DefaultTableModel) jTable1.getModel();
int x = 0;
int row = dtm.getRowCount();

while(row>=x){

    dtm.removeRow(x);
    x++;

}
Run Code Online (Sandbox Code Playgroud)

但它会产生错误,例如:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 
  24 >= 24
Run Code Online (Sandbox Code Playgroud)

java arrays swing jtable indexoutofboundsexception

0
推荐指数
1
解决办法
578
查看次数

java.lang.ArrayIndexOutOfBoundsException真的很奇怪

我知道我在日志猫中获得的异常有一个非常严格的名字.我唯一的问题是我无法确定导致我的异常的原因.我创建了一个PC-Android多客户聊天.

PC版本与服务器和客户端100%一起工作.我实现了从PC客户端到Android客户端和聊天工作相同的技术.

我得到的唯一问题是,当我连接android,clientArea(跟踪所有连接的用户)时,不跟踪其他名称,而是跟踪android客户端用户名的倍数.即.我只使用用户名:Jurko连接到android,clientArea将有2个人称为Jurko而不是只有一个.

logcat的:

10-11 17:19:08.221: ERROR/AndroidRuntime(12379): FATAL EXCEPTION: main
        java.lang.ArrayIndexOutOfBoundsException: length=2; index=2
        at com.example.JurkoAndroidChat.MyActivity$ServerTask.onProgressUpdate(MyActivity.java:175)
        at com.example.JurkoAndroidChat.MyActivity$ServerTask.onProgressUpdate(MyActivity.java:130)
        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:647)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4895)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
        at dalvik.system.NativeStart.main(Native Method)
Run Code Online (Sandbox Code Playgroud)

Android客户端:

package com.example.JurkoAndroidChat;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View;

import java.net.*;
import java.io.*;
import java.util.*;



public class MyActivity extends Activity {
    /**
     * Called when the activity is first created.
     */ …
Run Code Online (Sandbox Code Playgroud)

java sockets android android-asynctask indexoutofboundsexception

0
推荐指数
1
解决办法
4338
查看次数

IndexOutOfBoundsException:索引:15,大小:19

所以我前几天从我们的网络服务器上梳理了一些日志(寻找其他东西),当一些奇怪的东西引起了我的注意.

java.lang.IndexOutOfBoundsException: Index: 15, Size: 19
    at java.util.ArrayList.rangeCheck(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
Run Code Online (Sandbox Code Playgroud)

这对我来说似乎不可能.

我查找了ArrayList#rangeCheck的源代码,这引起了我的注意

private void rangeCheck(int index) {
    if (index >= size)
        throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
Run Code Online (Sandbox Code Playgroud)

基于我收到的信息应该永远不会发生.

不幸的是,我不知道目前内容是什么(这是从用于很多事情的代码中调用的).我一直无法重现它.我甚至不知道我从哪里开始.

问题:这是否应该发生?这是Java中的一个小故障吗?或者只是一个怪胎事故.

我认识到这可能不合时宜.我希望我能了解更多关于发生了什么的信息,但我不知道.

java indexoutofboundsexception

0
推荐指数
1
解决办法
911
查看次数

为什么我在这个程序中遇到"ArrayIndexOutOfBounds"错误?

我编写了一个程序,用户输入二维数组的维度,然后计算机打印出具有这些维度的表格,并用0到9的随机整数填充它.然后,如果有四个连续的相等整数,出现在表格的任何地方,计算机将返回"True".如果表中没有连续的相等整数,则返回"False".例如:

2 5 8 7 1
3 2 9 4 7
5 1 2 0 3
8 0 1 2 7
Run Code Online (Sandbox Code Playgroud)

在该表中,两个从第一个点对角地连续出现.它也可以是这样的:

9 5 3 7 0
2 5 7 3 1
8 5 0 2 9
4 5 1 7 5
Run Code Online (Sandbox Code Playgroud)

在此表中,五个从第二个点垂直向下显示.

这是程序的代码:

/*MyName*/
package fourconsecutivenumbers;
import java.util.Random;
import java.util.Scanner;
public class FourConsecutiveNumbers {

public static void main(String[] args) {

    Scanner rowDimension = new Scanner(System.in);
    System.out.print("Enter the number of rows: ");
    int firstInput = rowDimension.nextInt();

    Scanner columnDimension = new …
Run Code Online (Sandbox Code Playgroud)

java arrays exception nullpointerexception indexoutofboundsexception

0
推荐指数
1
解决办法
169
查看次数

toCharArray,Index out of bounds

有人可以告诉我为什么.toCharArray()行给出了一个Index Out of Bounds异常吗?为什么它甚至为String提供索引0大小为0的异常?我还没有定义一个字符串数组?!

此外,此代码应该在O(n)时间运行吗?

import java.util.ArrayList;
import java.util.List;


public class FirstNonRepeating {


public static List<Character> list = new ArrayList<Character>();
public static List<Character> list2 = new ArrayList<Character>();

public static String res(String x) {
    char[] y = x.toCharArray();

    for(char c:y){
        list2.add(c);
        if(list.contains(c)){
            list2.remove(c);
        }
        else{
            list2.add(c);
        }
    }

    if(list2.isEmpty()){
    return "NONE";
    }

    else return (""+list.get(0));

    }

public static void main(String[] args) {

    String a="Google";
    String z=res(a);
    System.out.println(z);
}

}
Run Code Online (Sandbox Code Playgroud)

java indexoutofboundsexception

0
推荐指数
1
解决办法
498
查看次数

java.lang.ArrayIndexOutOfBoundsException:length = 12; recyclerview适配器中的index = -1

我正在使用回收站视图。这是适配器

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<Contact> myContacts;
private Activity activity;

public MyAdapter(List<Contact> contacts,Activity activity)
{
    this.myContacts = contacts;
    this.activity = activity;
}


public class ViewHolder extends RecyclerView.ViewHolder {
    public TextView mTextView;
    public ImageView mColorView;
    public LinearLayout linearLayout;
    public ViewHolder(View v,MyAdapter adapter) {
        super(v);
        mTextView =  v.findViewById(R.id.name);
        mColorView = v.findViewById(R.id.image);
        linearLayout = v.findViewById(R.id.listItemLinearLayout);
        v.setOnLongClickListener(onLongClickListener(myContacts.get(getAdapterPosition()).getID(),getAdapterPosition(),adapter));
    }
}

@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {
    // create a new view
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.my_text_view, parent, false);
    // set …
Run Code Online (Sandbox Code Playgroud)

android indexoutofboundsexception recycler-adapter android-recyclerview

0
推荐指数
1
解决办法
2121
查看次数

为什么我们不能在将元素添加到第(n-1)个索引之前将元素添加到List到第n个索引中,即使提供了初始容量也是如此

假设我声明了一个int大小为10 的数组,我可以在其第四个索引中添加一个元素,我可以毫无例外地运行代码.

int[] ar = new int[10];
ar[4] = 8;      
System.out.println(Arrays.toString(ar)); //works fine
Run Code Online (Sandbox Code Playgroud)

那是因为当我说大小为10时,将为该数组分配那么多的内存空间,其类型的初始值保存在每个索引中.

但案件并不相同List.假设我声明了一个初始容量为10的列表,并尝试将一个元素添加到它给出的第四个索引中

java.lang.IndexOutOfBoundsException:索引:4,大小:0

List<Integer> list = new ArrayList<Integer>(10);
list.add(4, 8); //exception
Run Code Online (Sandbox Code Playgroud)

当然,0即使给出了初始容量,列表的大小也会返回.为什么它不像Array,我认为没有为列表的10个元素分配内存?

我猜测,一旦像数组一样给出容量,有没有办法用List填充默认值.

java arrays list indexoutofboundsexception

0
推荐指数
1
解决办法
75
查看次数