Announcement

Collapse
No announcement yet.

Reading numbers from text files in Java

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • #16
    Brother, you can't create a variable of type integer, and save String's in it.


    Create a 'for loop' to print out each of it's element. Let me know if you need further assistance.
    Even Adobe Photoshop can't change me.

    A line[Durrand line] of hatred that raised a wall between the two brothers -Hamid Karzai

    For generations, the Hindus of India prayed for deliverance from "the venom of the cobra, the teeth of the tiger and the vengeance of the Afghan."


    The men of Kábul and Khilj also went home; and whenever they were questioned about the Musulmáns of the Kohistán (the mountains), and how matters stood there, they said, "Don't call it Kohistán, but Afghánistán; for there is nothing there but Afgháns and disturbances." Thus it is clear that for this reason the people of the country call their home in their own language Afghánistán, and themselves Afgháns. The people of India call them Patán; but the reason for this is not known. But it occurs to me, that when, under the rule of Muhammadan sovereigns, Musulmáns first came to the city of Patná, and dwelt there, the people of India (for that reason) called them Patáns—but God knows!

    -Ferishta, 1560–1620

    Comment


    • #17
      public class Afghanistan2010 {

      public static void main(String[] args){


      String Paktia[]= new String [3];

      Paktia[0]= "apple";
      Paktia[1] = "mango";
      Paktia[2] = "banana";
      //3 elements have been saved in the above array, of type String.


      System.out.println(Paktia[0]);
      System.out.println(Paktia[1]);
      System.out.println(Paktia[2]);
      //As you can see, it would be silly to constantly type system.out.print for each and every element in the array.
      //Suppose we have 1million elements in the array, we can't waste months type System.out.print. Therefore, we use a power loop
      //which will continously iterate, till it reaches the length of the array.

      for(int i = 0 ; i<Paktia.length; i++){

      System.out.println(Paktia[i]);
      }
      //The above will now print out all the elements in Paktia.


      }

      }
      Even Adobe Photoshop can't change me.

      A line[Durrand line] of hatred that raised a wall between the two brothers -Hamid Karzai

      For generations, the Hindus of India prayed for deliverance from "the venom of the cobra, the teeth of the tiger and the vengeance of the Afghan."


      The men of Kábul and Khilj also went home; and whenever they were questioned about the Musulmáns of the Kohistán (the mountains), and how matters stood there, they said, "Don't call it Kohistán, but Afghánistán; for there is nothing there but Afgháns and disturbances." Thus it is clear that for this reason the people of the country call their home in their own language Afghánistán, and themselves Afgháns. The people of India call them Patán; but the reason for this is not known. But it occurs to me, that when, under the rule of Muhammadan sovereigns, Musulmáns first came to the city of Patná, and dwelt there, the people of India (for that reason) called them Patáns—but God knows!

      -Ferishta, 1560–1620

      Comment


      • #18
        Originally posted by Admin Khan View Post
        public class Afghanistan2010 {

        public static void main(String[] args){


        String Paktia[]= new String [3];

        Paktia[0]= "apple";
        Paktia[1] = "mango";
        Paktia[2] = "banana";
        //3 elements have been saved in the above array, of type String.


        System.out.println(Paktia[0]);
        System.out.println(Paktia[1]);
        System.out.println(Paktia[2]);
        //As you can see, it would be silly to constantly type system.out.print for each and every element in the array.
        //Suppose we have 1million elements in the array, we can't waste months type System.out.print. Therefore, we use a power loop
        //which will continously iterate, till it reaches the length of the array.

        for(int i = 0 ; i<Paktia.length; i++){

        System.out.println(Paktia[i]);
        }
        //The above will now print out all the elements in Paktia.


        }

        }

        Manana for your support , yes i am aware of String for the fruits it was just example,

        as you said , i have an array where i have many things,but i want to get some random part of the array out in a textfield where i can further use it , i dont want all numbers at one, but one random from the numbers i saved in my array,

        txfRandom1.setText(String.valueOf( Math.floor( Math.random() * arNumbers.length)));

        i used this but it doesn't work as i wanted it, it gives not out my numbers i saved in the array but random numbers which are some in my array []= {2,3,5,7,11,13,17,19}

        for example i want these numbers not any others.

        the first is at array[0] = 2, array[1]=3 and so on.....

        now i want to get random one from these numbers i saved in the array
        High achievement always takes place in the framework of high expectation.

        Comment


        • #19
          I think this is what you mean, if I understood you properly.

          import java.util.Random;
          public class Afghanistan2010 {

          public static void main(String[] args){

          Random randomgen = new Random();
          //using the random class
          int afghan = randomgen.nextInt(6);
          //afghan will now print random numbers;

          int Paktia[]= new int [6];
          Paktia[0]=15;
          Paktia[1]=22;
          Paktia[2]=453;
          Paktia[3]=43;
          Paktia[4]=555;
          Paktia[5]=632;
          //save 5 random numbers in the Paktia array

          for(int i =afghan ; i<Paktia.length; i++){
          //it will start with afghan, and afghan in it's self is a random number, hence the starting will be random.
          System.out.println(Paktia[i]);
          break;
          }
          //The above will now print out ONE random element, of the Paktia array.


          }

          }
          Even Adobe Photoshop can't change me.

          A line[Durrand line] of hatred that raised a wall between the two brothers -Hamid Karzai

          For generations, the Hindus of India prayed for deliverance from "the venom of the cobra, the teeth of the tiger and the vengeance of the Afghan."


          The men of Kábul and Khilj also went home; and whenever they were questioned about the Musulmáns of the Kohistán (the mountains), and how matters stood there, they said, "Don't call it Kohistán, but Afghánistán; for there is nothing there but Afgháns and disturbances." Thus it is clear that for this reason the people of the country call their home in their own language Afghánistán, and themselves Afgháns. The people of India call them Patán; but the reason for this is not known. But it occurs to me, that when, under the rule of Muhammadan sovereigns, Musulmáns first came to the city of Patná, and dwelt there, the people of India (for that reason) called them Patáns—but God knows!

          -Ferishta, 1560–1620

          Comment


          • #20
            oh thank you very much i will try it out inshaAllah as soon im finish with my work, i even manged to contact my old professor regarding this because i tried everything out i was getting confused more and more.
            High achievement always takes place in the framework of high expectation.

            Comment


            • #21
              Do it like below.

              import java.util.Random;

              public class fruitsClass{

              public static void main(String[] args) {
              String[] Fruit = new String[3];
              Fruit[0] = "apples";
              Fruit[1] = "mango";
              Fruit[2] = "banana";

              System.out.println("Your luck is: "+getFruit(Fruit));
              }//main(...)

              public static String getFruit(String[] Fruit){
              return Fruit[(new Random()).nextInt(Fruit.length)];
              }//getFruit(...)

              }//fruitsClass

              Notice the call to the getFruit(...) function in main(...), which returns a fruit as a string randomly selected from the list of fruits. Each time, you run this program, you get a new fruit displayed to you (println statement in main()).

              If you don't want to use another function and want every thing done in main(...), you can shift the Fruit[(new Random()).nextInt(Fruit.length)] to main(...) and substitute getFruit (...) in the println(...) with it.

              Are you taking some course in CSc?

              Comment


              • #22
                If you want to continue selecting fruit without re-running the program, then use the following code.

                import java.util.*;
                public class fruitsClass {
                public static void main(String[] args) {


                String[] Fruit = new String[3];
                Fruit[0] = "apples";
                Fruit[1] = "mango";
                Fruit[2] = "banana";

                Scanner in = new Scanner(System.in);
                String resp = null;
                System.out.println("Want some fruit <press y for yes, n for no>");
                while ( (resp=in.next()).equalsIgnoreCase("y")){
                System.out.println("You said <YES>");
                System.out.println("Your luck is: "+getFruit(Fruit));
                System.out.println("Want more <press y for yes, n for no>");
                }
                System.out.println("You said <NO>...so will finish it");

                }//main(...)
                public static String getFruit(String[] Fruit){
                return Fruit[(new Random()).nextInt(Fruit.length)];
                }//getFruit(...)
                }//fruitsClass

                So, as long as you wouldn't click n or N, the program would radomly be selecting a fruit and displaying it to you. A shorther version of the above program could be,

                import java.util.*;
                public class fruitsClass {
                public static void main(String[] args) {


                String[] Fruit = new String[3];
                Fruit[0] = "apples";
                Fruit[1] = "mango";
                Fruit[2] = "banana";

                Scanner in = new Scanner(System.in);
                String resp = null;
                System.out.println("Want some fruit <press y for yes, n for no>");
                while ( (resp=in.next()).equalsIgnoreCase("y")){
                System.out.println("You said <YES>");
                System.out.println("Your luck is: "+Fruit[(new Random()).nextInt (Fruit.length)]);
                System.out.println("Want more <press y for yes, n for no>");
                }
                System.out.println("You said <NO>...so will finish it");

                }//main(...)
                }//fruitsClass

                In this case, you don't need any getFruit(...) function.
                Last edited by Toramana; 11-24-2011, 11:27 PM.

                Comment


                • #23
                  Very appreciate,

                  i wanted to get this random value into a textfield for example

                  txfNumbers (String.valueOf(......))

                  alhamdulillah i will try this as soon as i have time...

                  brother Adminkhan,

                  i have no course but its part of my study.... and i want to learn robotics beside that what i am studying.


                  there was an error with

                  "loss of precision in Netbeans"
                  "int required , double found"
                  i have successfully overhelmed it with following

                  txfNumber1.setText(String.valueOf( arNumbers [ (int) Math.floor( Math.random() *
                  arNumbers.length)]));

                  txfNumber2.setText(String.valueOf( arNumbers [ (int) Math.floor( Math.random() *
                  arNumbers.length )]));


                  My programm has to do with primes (mathematical calculation),im working on the "Goldbach solution" which has to do with the prime numbers that the summa of two primes is equal a even number,if any one is interessted in the programm i can share it.
                  Last edited by Afghanistan2010; 11-27-2011, 06:30 AM.
                  High achievement always takes place in the framework of high expectation.

                  Comment


                  • #24
                    Originally posted by Afghanistan2010 View Post
                    Very appreciate,

                    i wanted to get this random value into a textfield for example

                    txfNumbers (String.valueOf(......))
                    Hi, it is nice that you've learnt from that example.

                    And you get that error when you try to assign a double value to an integer variable e.g., like below.

                    int inVal = Math.random();

                    Now Math.Randon() generates a double-type value but you are assigning it to an integer variable i.e. to inVal. The compiler gives you a loss of precision error because double-type values are internally represented differently (from integer-type values). You can handle this by casting the data type of the source item to that of the destination item as below.

                    int inVal = (int) Math.random();

                    But for the assignment to work, the source and destination items must also be size-compatible. The problem here is, the size of a double value is 4 bytes whereas the size of integer value is 2 bytes. Now you cannot store a larger value (4-byte) into a smaller space (2-byte).

                    If this is the case (problem of type-compatibility as well as size-compatibility), then make both the source and destination items the same type i.e., double in this case as below.

                    double inVal = Math.random();

                    But it would depend on your requirements as well as which type do you need.

                    As I can see it, in the statement below,

                    txfNumber2.setText(String.valueOf( arNumbers [ (int) Math.floor( Math.random() *
                    arNumbers.length )]));

                    you have an array named "arNumbers" and you are mapping the value " (int) Math.floor( Math.random() *
                    arNumbers.length )" into one of its indexis. Nice logic and I admire your idea but there is a more secure way of doing that. You can make use of java.util.Random class and do it as below.



                    import java.util.Random;

                    Random rnd = new Random();

                    txfNumber2.setText(String.valueOf( arNumbers [ rnd.nextInt(artNumbers.length)]));



                    Now if you have declared artNumbers to be an array of size 10 for example as below,

                    double[] artNumbers = new double[10];

                    then rnd.nextInt(artNumbers.length) is going to return you a random number between 0 and 10 (0 included 10 excluded). This is because artNumbers.length represents the size of the array, artNumbers (10 in this case).
                    Last edited by Toramana; 11-27-2011, 01:44 PM.

                    Comment


                    • #25
                      I very appreciate your understanding of Java,
                      inshaAllah its maybe that i will need help further in developing my robot with java...i will ask then inshaAllah.
                      High achievement always takes place in the framework of high expectation.

                      Comment


                      • #26
                        Example :

                        Random rnd = new Random();
                        System.out.println( rnd.nextInt(5));
                        //prints a random number between 0 and 5 (0 included 5 excluded)

                        System.out.println( rnd.nextInt(8));
                        //prints a random number between 0 and 8 (0 included 8 excluded)

                        String[] myCity = new String [3];
                        myCity[0] = "Jalalabad";
                        myCity[1] = "Paktiya";
                        myCity[2] = "Kandahar";

                        int arrIndex= rnd.nextInt(myCity.length);

                        //myCity.length is 3 so rnd.nextInt(myCity.length) generates a random
                        //number between 0 and 3 i.e., either 0 or 1 or 2, which is then assigned
                        //to arrIndex

                        System.out.println( myCity[arrIndex]);

                        //would print a city depending on which random value was assigned to
                        //arrIndex by the statement above...

                        Comment


                        • #27
                          Also, when casting from double to int or vice versa, be careful. In ordinary computation, the imapct might not be that significant but in other scenarios it may lead to disastrous consequences.

                          For example, imagine you were designing your robbot to control some industrial or scientific process involving careful handling of temprature. You round the temprature and you get 1000 Celcious instead of the required threshold 999.5. That may result into disastrous consequences.

                          Another example: You were firing a missile at a target, which required careful calculation of distance to the target but you rounded or truncated in the process of casting and re-casting values and the resulting location is off the target by a small amount and your missile hits a house.

                          But anyhow, in normal calculations, where precision is not that important it is ok.
                          Last edited by Toramana; 11-27-2011, 02:21 PM.

                          Comment


                          • #28
                            If you need assistance in other areas e.g., C/C++, SQL, SQL-Plus, PL/SQL, or other CSc core subjects or some CSc specialized areas, feel free to ask questions.

                            Comment

                            Working...
                            X