Solved

String not counting correctly

Posted on 2004-10-13
8
250 Views
Last Modified: 2010-03-31
I need the final out put of this string to come out in this format: My favorite car, ford, is in position 3

the problem that i have is that its not counting correctly, its one under so instead of 3 its at 2.

using this script:

public class Program6 {

      public static void main(String[] args) {
            System.out.println("Sebastian Velasquez");
            System.out.println("enter a floating point number, eg: 123.4567");
            double x = ConsoleIn.readLineDouble();
            double y = (int) (x *100 + 0.5) / 100.0;
            double z = Math.round(100 * x) / 100.0;
            System.out.println(x + " ! " + y + " ! " + z);
            double rnd = Math.random();
            int rnd4 = (int) (rnd * 10000);
            String rndString = "000" + rnd4;
            String rndString4 = rndString.substring(rndString.length() - 4);
            System.out.println(rnd + " ! " + rnd4 + " ! " + rndString4);
            String newcar = "Mercedez  BMW       Audi      volkswagenRenault   Nissan    Ford      Jaguar    ";
            int r = (int) (8 * Math.random());
            String car = newcar.substring(10 * r, 10 * r + 10).trim();
            System.out.println("My favorite car" + ", " + car + ", is in position " + r);
      }
}

I need to get it to output correctly and count correctly.

thanks
0
Comment
Question by:sab568
  • 6
  • 2
8 Comments
 
LVL 4

Expert Comment

by:Rick_Townsend
ID: 12304653
So the only lines you want fixed are these?

          String newcar = "Mercedez  BMW       Audi      volkswagenRenault   Nissan    Ford      Jaguar    ";
          int r = (int) (8 * Math.random());
          String car = newcar.substring(10 * r, 10 * r + 10).trim();
          System.out.println("My favorite car" + ", " + car + ", is in position " + r);
0
 
LVL 4

Expert Comment

by:Rick_Townsend
ID: 12304661
Casting to an int will chop off the decimal value of the number generated by (8 * Math.random()).  So your values start at 0.  Assuming you want Mercedez to be 1, not 0, just add 1 to the value of r when you print it out.

System.out.println("My favorite car" + ", " + car + ", is in position " + r + 1);
0
 

Author Comment

by:sab568
ID: 12304663
all that is right, what i want to figure out is how to get it to count correctly.

For example: there are 8 car models( Mercedez  BMW       Audi      volkswagenRenault   Nissan    Ford      Jaguar    ), and when the output selects audi which is model #3 it outputs that it is number 2 or when it gets jaguar which is 8 it outputs 7 and so on, I want to figure out why it does that and correct it.

thanks
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:Rick_Townsend
ID: 12304685
If you're interested:

Math.random() returns a value between 0 and 1.  Multiply that by 8, and you have a number between 0 and 8.  When you chop off the decimal, the only way you'll end up with 8 is if the random generator returned exactly 1 (odds of that are approx 1 in 2^32).  So basically you have numbers between 0 and 7.
0
 
LVL 4

Expert Comment

by:Rick_Townsend
ID: 12304688
Does the comment above answer your question?  I sent it before your response came up.  (Internet lag time, I guess)
0
 
LVL 4

Accepted Solution

by:
Rick_Townsend earned 50 total points
ID: 12304697
Maybe if I explain it this way:

This is like a zero-indexed array.  Car #1 is at position zero.  Car #2 is at position one.  Car #3 is at position two...

You either have to:
1) add one to your output position (as described in the second comment), or
2) add one to r after getting the random number, and then subtract one when selecting a car from the string, or
3) add ten spaces in front of the first car, to make your "array" start at position 1.
0
 

Author Comment

by:sab568
ID: 12304702
OH!, I see so it was right all along, i just didnt realize it counted from 0 not from 1. So it was right allllll along. I realy appreciate the help.

thanks,

sab568
0
 
LVL 4

Expert Comment

by:Rick_Townsend
ID: 12309981
You're welcome.  Have some faith in your own coding skills!  :)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Weekend adv creator 3 64
spring jars download 1 52
Bot application - advice 3 58
Print Rhino Java Array in Javascript 1 30
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question