Jump to content

Major java problem (while loop)

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
12 replies to this topic

#1
spelly

spelly

    Newbie

  • Members
  • Pip
  • 7 posts
i've just started with java, and got some problem regarding while loops.
I have the following code(after the paragraph), this calculate how long a object has fallen - give time / distance.
The code are going to do following when I run it:
Denote time in second: 10.5 s
Distance after 10.5s in free fall: 540.8meter
Time interval in second: 20.00
Conversion table: Time [sec] | Distance [m]
20,0 | 1962,0
40,0 | 7848,0
60,0 | 17658,0
80,0 | 31392,0
100,0 | 49050,0

The code:
import java.util.Scanner;  // <- metode for å konvertere bytes til primitive verdier.

public class Distansee {

  public static void main(String[] args) {

    

    Scanner tastatur = new Scanner(System.in);

    System.out.println("Angi tid i sekunder: "); // <- skriver ut

    final double g = 9.81; //konstant til g

    int test  = 0;

    

    double[][] dblArrUtskrift = new double[6][2];

    

    while (test <=5) {

      test ++;

      double tid = tastatur.nextDouble(); //leser tastaturet

      double distanse = (g* Math.pow(tid,2)/2.0); //Gjør regneoperasjoner. Math.pow, opphøyer verdien i tid i 2 (tid^2)

      

      dblArrUtskrift[test][5] = tid; // her velger du seff selv noe annet

      dblArrUtskrift[test][1] = distanse; // ----------"----------------



      }


      for (int i = 0; i <= 5; i++) {

   System.out.println(dblArrUtskrift[i][0] + "|" + dblArrUtskrift[i][1]);

    }

    System.out.println(".    Angi distanse i meter:"); //skriver ut

    double distanse2 = tastatur.nextDouble(); //leser tastaturet

    double tid2 = Math.sqrt((2*distanse2)/g); //gjør regneoperasjon 

    System.out.printf("Tiden i fritt fall: %.1f sekunder", tid2); //skriver ut tekst og svaret på regneoperasjonen

    

    

  }

} 

First question:
I can compile the code, but i get the following error when i run it :
java.lang.ArrayIndexOutOfBoundsException: 5
at Distansee.main(Distansee.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)


Whats the reason?

Thanks for the help!

#2
dbug

dbug

    Programmer

  • Members
  • PipPipPipPip
  • 155 posts
The error is caused by an index out of bounds in line 17:

dblArrUtskrift[test][5] = tid; // her velger du seff selv noe annet
dblArrUtskrift is created as an array of doubles with 6 positions for the first index (from 0 to 5) and 2 positions for the second index (0 and 1). You are trying to access position 5 in second index, that is out of the defined bounds fot that index.

#3
spelly

spelly

    Newbie

  • Members
  • Pip
  • 7 posts
Thank you! really appreciate the help
changed the value in that cell to 0.
dblArrUtskrift[test][0] = tid;
The code will now compile and run, its altso possible to type letters in the input box, and that more or less what the code does.(type time in seconds:1, output:1) its suppost to do like this:

Quote

Denote time in second: 10.5 s
Distance after 10.5s in free fall: 540.8meter
Time interval in second: 20.00
Conversion table: Time [sec] | Distance [m]
20,0 | 1962,0
40,0 | 7848,0
60,0 | 17658,0
80,0 | 31392,0
100,0 | 49050,0

what have i done wrong ?

#4
dbug

dbug

    Programmer

  • Members
  • PipPipPipPip
  • 155 posts
Sorry, I don't understand what is your problem now...

#5
spelly

spelly

    Newbie

  • Members
  • Pip
  • 7 posts
Sorry for my English.

As i said: the program will now run, however the only thing it does when i run is:
Posted Image

This is what the program is designed to do:

Denote time in second: 10.5 s
Distance after 10.5s in free fall: 540.8meter
Time interval in second: 20.00
Conversion table: Time [sec] | Distance [m]
20,0 | 1962,0
40,0 | 7848,0
60,0 | 17658,0
80,0 | 31392,0
100,0 | 49050,0

What have i done wrong ?

#6
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
See, if you write your programs in your native language, not English, no one in the global community can understand what your program is supposed to do...

#7
spelly

spelly

    Newbie

  • Members
  • Pip
  • 7 posts

Sinipull said:

See, if you write your programs in your native language, not English, no one in the global community can understand what your program is supposed to do...

Sorry, didn't think about that. Will never happen again.

Did edit my code from Norwegian to English:
Code:

import java.util.Scanner;  

public class Distansee {

  public static void main(String[] args) {

    

    Scanner tastatur = new Scanner(System.in);

    System.out.println("time in sec: ");

    final double g = 9.81; 

    int test  = 0;

    

    double[][] dblArrOutput = new double[6][2];

    

    while (test <=5) {

      test ++;

      double time = tastatur.nextDouble(); 

      double distance = (g* Math.pow(tid,2)/2.0); 

      

      dblArrOutput[test][5] = time; 

      dblArrOutput[test][1] = distance; 


   for (int i = 0; i <= 5; i++) {

   System.out.println(dblArrOutput[i][0] + "|" + dblArrOutput[i][1])

      }



    }

    System.out.println(".    Distance in meter:"); 

    double distance2 = tastatur.nextDouble(); 

    double time2 = Math.sqrt((2*distance2)/g); 

    System.out.printf("Time in free fall: %.1f sec", time2); 

    

    

  }

}

As I said in post 5.
The program will run, however its not doing what I designed it to do.
What I want the program to do:
Denote time in second: 10.5 s
Distance after 10.5s in free fall: 540.8meter
Time interval in second: 20.00
Conversion table: Time [sec] | Distance [m]
20,0 | 1962,0
40,0 | 7848,0
60,0 | 17658,0
80,0 | 31392,0
100,0 | 49050,0

And what it does:
Posted Image

#8
dbug

dbug

    Programmer

  • Members
  • PipPipPipPip
  • 155 posts
Sorry, but I still don't understand the problem...

Is it related to the input of data, to the treatment of it or to the computation of the results ? the output you posted is real or it is what the program should write ? if it is a theoretical output, what is the actual output of the program ?

#9
spelly

spelly

    Newbie

  • Members
  • Pip
  • 7 posts
I guess the problem is related to the treatment of the input data.

The output of the program: (Angi tid I sekunder = denote time in second)
Posted Image

And the following is what I want it to do:
Denote time in second: 10.5 s
Distance after 10.5s in free fall: 540.8meter
Time interval in second: 20.00
Conversion table: Time [sec] | Distance [m]
20,0 | 1962,0
40,0 | 7848,0
60,0 | 17658,0
80,0 | 31392,0


Again: thanks for helping me.

#10
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
What i think you want:
import java.util.Scanner; 

public class Distansee {
  public static void main(String[] args) {
    final double g = 9.81; 
    Scanner scanner= new Scanner(System.in);
    
    System.out.println("Time in sec: ");
    double time = scanner.nextDouble();
    
    System.out.println("Distance after " + time + "s in free fall: " + Math.pow(time,2)*g / 2.0 +"m");  
    
    System.out.println("Distance in meter: ");
    double distance = scanner.nextDouble();
    
    System.out.println("time after falling " + distance + "m in free fall: " + Math.sqrt(2*distance/g) +"s");   
    	
    for(time=20 ; time<=100 ; time+=20.0){      
      System.out.println(time + " | " + Math.pow(time,2)*g / 2.0);
    } 
  }    
}


#11
spelly

spelly

    Newbie

  • Members
  • Pip
  • 7 posts
thanks alot oxano, almost there.

the program shall type out value for t, t2, t3, t4 and t5 seconds , the user must be able to type the time interval.
Like this:
when the user type in 10.5 in time interval, this shall be the output:

(input)
Give time in sec: 10.5

(output)
Distance after 10.5sec in free fall : 540.8m

(input)
time interval in sec: 20.00
(output)
20,0 | 1962,0
40,0 | 7848,0
60,0 | 17658,0
80,0 | 31392,0
100,0 | 49050,0

you see there are two operations. One were there is a simple math operation. (calculate how long distance a object has fallen after 10.5 sec.
and the other operation where you type in a time interval and the program calculate the distance a object has fallen after (in this example) 20, 40, 60, 80 and 100s.
Get it?



20,0 | 1962,0
40,0 | 7848,0
60,0 | 17658,0
80,0 | 31392,0

the program calculate time distance in free fall in 20s interval like this:

Time interval = 20,00s

#12
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
Then remove the last for-loop and replace by
    System.out.println("Time interval in sec: ");
    double interval= scanner.nextDouble();
    	
    for(time=interval; time<=interval*5 ; time+=interval){      
      System.out.println(time + " | " + Math.pow(time,2)*g / 2.0);
    }