Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I want to print the array in reverse order but I don't know why it's not working. I want to reverse them using the way I have mentioned below, in a single line, but my logic is not working.
void dynamicinputArraytwo() throws IOException {
int buffer;
int i,jo,io;
System.out.println("Enter size");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
buffer = Integer.parseInt(br.readLine());
float arrtwo[]=new float[buffer];
for( i=0;i< arrtwo.length;i++){
System.out.println ("Enter elements of array=");
arrtwo[i] = Float.parseFloat(br.readLine());
}
for (int jx=0; jx < arrtwo.length; jx++){
System.out.println("array before reverse "+arrtwo[jx] + " ");
}
float arrthree[];
arrthree = new float[arrtwo.length];
for(io=0,jo=arrtwo.length; jo>=0;io++,jo--){
arrthree[io] = arrtwo[jo] ;
}
for(io=0; io < arrtwo.length; io++){
arrtwo[io] = arrthree[io] ;
}
for (int jx=0; jx < arrtwo.length; jx++){
System.out.println("array after reverse "+arrtwo[jx] + " ");
}
}
Your first problem is here:
for(io=0,jo=arrtwo.length; jo>=0;io++,jo--){
arrthree[io] = arrtwo[jo] ;
}
The last index of an array is length - 1 because arrays are indexed starting at 0, so you'll have an exception (i.e. arrtwo[arrtwo.length] is not a valid index in the array). You should set jo to arrtwo.length - 1 initially instead.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
If i have an input smt. like that:
1,10;3,3;4,1. Lets say String input.
How can I split it in ";", so the result could be like this:
[1,10]
[3,3]
[4,1]
Thanks!
I think you want to do something like this.
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String input;
//your input is 1,10;3,3;4,1
input = scanner.next();
String[] splitted = input.split(";");
//save the new list or array
ArrayList list = new ArrayList();
for (int i = 0; i < splitted.length; i++) {
System.out.println(splitted[i]);
//for later usage
list.add(splitted[i]);
}
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 2 years ago.
Improve this question
final int size = 10 ;
int [] crr = new int [size];
crr [0] = -1 ;
crr [1] = 2 ;
crr [2] = 6 ;
crr [3] = 9 ;
Scanner reader = new Scanner(System.in);
for (int i = 0 ; i < crr.length ; i++ );
{
System.out.println ("The next element");
crr [i]= reader .nextInt ();
System.out.println (crr[i]);
After Execution Says this :
Exception in thread "main" java.lang.Error: Unresolved compilation
problems: I cannot be resolved to a variable I cannot be resolved
to a variable
If I put i in the crr position it shows as its a problem. Please I need some advice on this.
If not copy the code and see the issue.
You are closing your for loop block with a ;. Therefore your loop is closed and also the scope of your loop variable i ends.
The correct loop would look like:
for (int i = 0; i < crr.length; i++) {
System.out.println("The next element");
crr[i] = reader.nextInt();
System.out.println(crr[i]);
}
You should maybe look into the Java Code Conventions which also tell you more about how to format your code.
Also, if you ask any questions in the future, you should provide a Minimal, reproducible example.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
The split function does not work. for the last print statement, it gives me an arrayoutofbound error. Any help?
while (inFile.hasNext())
{
String clean = inFile.nextLine();
String[] nm = clean.split(",");
for (int i = 0; i < nm.length; i++)
{
System.out.println("at index "+ i +" string is "+nm[i]);
}
System.out.print("at index"+2+"Strin"+nm[3]);
}
text file :
input1,2,3,4,5
input2,2,3,4,5
input3,3,4,5,6
input4,3,4,5,6
input5,3,4,5,6
output:
at index 0 string is input1
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at filereader.FileReader.main(FileReader.java:33)
Java Result: 1
The code seems to be proper to me and it should not fail like this. Only reason i can see is if there is a blank line in your input file, in which case System.out.print("at index"+2+"Strin"+nm[3]); will return a ArrayOutOfBoundException at nm[3]
Alternatively you can write your code like this:
while (inFile.hasNext())
{
String clean = inFile.nextLine();
if(clean != null && clean != ""){
String[] nm = clean.split(",");
for (int i = 0; i < nm.length; i++)
{
System.out.println("at index "+ i +" string is "+nm[i]);
}
System.out.print("at index"+2+"Strin"+nm[3]);
}
}
Hope this works.
This gives you error:
System.out.print("at index"+2+"Strin"+nm[3]);
And that is because you either have one blank line or a line where there are less than 4 comma separated items. Try this:
System.out.print("at index"+2+"Strin"+nm[nm.length-1]);
Maybe the array nm does not have 4 elements at line
System.out.print("at index"+2+"Strin"+nm[3]);
Check that the arrays contains at least 4 element before printing the 4th element.
// create a new scanner with the specified String Object
Scanner inFile = new Scanner(s);
while (inFile.hasNext())
{
String clean = inFile.nextLine();
String[] nm = clean.split(",");
for (int i = 0; i < nm.length; i++)
{
System.out.println("at index "+ i +" string is "+nm[i]);
}
if (nm.length > 3) {
System.out.print("at index"+2+"Strin"+nm[3]);
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am getting the error Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at sorting.main(sorting.java:53). I am just trying to pass string integer values to an integer array but it is not working as intended. I have variables above such as count which is determined by how many integers the user wants to enter. In and another variable int[] list = new int[count]; which is the integer array I am trying to pass them to. If I enter the value of 5 for count I am trying to only accept 5 values of integers. Passing them to the int[] list array is where I am messing up.
// Prompts user to enter the integer values here
while(true){
System.out.print("\nEnter integer values here seperated by a space: ");
intValues = input.readLine();
String[] intCheck = intValues.split("\\s+");
try {
for(int j = 0; j < intCheck.length; j++){
list[j] = Integer.parseInt(intCheck[j]); // LINE 53 IS HERE
}
}
catch (NumberFormatException ex) {
System.err.println("You need to enter valid integer numbers. Try again.");
}
}
you probably have an extra space at the end so your split is returning an overlarge array. Use j < count.
I have gotten my error. I declared int count = 0 early on. Then just below that I declared int[] list = new int[count] which was taking 0 values. I should have declared the range after I prompted the user to enter the information.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How to fill an array of integers in Java by asking the user to enter the number once ?
for example :
Enter 3 integers >: 123
then I want to get the array filled like that :
arr[0]=1
arr[1]=2
arr[2]=3
I hope the question is clear :)
rgds
One way of going about this would be to take the integer in as a String. You can then use toCharArray() in order to grab an array of the characters - these characters can then be individually parsed into Integer by utilizing Character.getNumericValue().
See the String and Character APIs for more details on those methods.
Small example of what I mean:
String numbers = "123";
char[] numArray = numbers.toCharArray();
for (char c : numArray) {
int num = Character.getNumericValue(c);
// Do what you will with the number.
}
As noted, this particular implementation would only work in this format if you are assuming that each integer is single digit.
How about this:-
Scanner input = new Scanner(System.in);
System.out.println("How many numbers do you want to enter?");
int num = input.nextInt();
int array[] = new int[num];
System.out.println("Enter the " + num + " numbers now.");
for (int i = 0 ; i < array.length; i++ ) {
array[i] = input.nextInt();
}