Solved

String not counting correctly

Posted on 2004-10-13
8
255 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy 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 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

636 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