Solved

String not counting correctly

Posted on 2004-10-13
8
218 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
 
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now