Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

String not counting correctly

Posted on 2004-10-13
8
246 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
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.

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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 52
Apps blocked by Java 9 88
jsp login check 12 42
ejb message driven bean mdb creation steps 2 20
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

856 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