Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

String not counting correctly

Posted on 2004-10-13
8
Medium Priority
?
257 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
[X]
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
  • 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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
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 200 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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…
Suggested Courses

705 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