Solved

position of the number

Posted on 2010-09-08
7
483 Views
Last Modified: 2013-11-24
I had a notepad... which contains... 3489 numbers...
I can search for a particular number but I would like to know the position of that number in the notepad..

example file:
123, 1245, 124, 567, 678, 6787,56778,345,6786,234,89,897,4556,5890,456,890,489,6798...

all numbers are unique...

ok if i seach for 567 If I can find it then i can print it but i would lke to know the position of that number 567
here the position is 4

0
Comment
Question by:shragi
7 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33633876
did you tried the indexOf(); method of String class?

I.e, when you have got the contents of the file in a string, you just need to do indexOf(<string to be searched>); , it should display the index of that number in that string

For getting file contents in string
http://snippets.dzone.com/posts/show/1335
http://www.kodejava.org/examples/52.html

0
 
LVL 1

Expert Comment

by:kaushikdeb
ID: 33633891
I guess you mean, you have a text file ('I had a notepad'!) and you are trying to do the operation you mentioned using java.
If that is the case, you can use the following steps to do this (may not be the best solution, but it should be enough and easy for a 'new to java programming' person :) ):
1. Read contents of the text file in java and get the whole data in a String.
2. Use the split function of String giving ',' (comma) as a separator. This function returns String array.
3. Now you can find the number you want and the index of the array for that number would be the position.

Hope this helps.
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33633922
If you want to find the position of the number (assuming the numbers will be separated by comma), then after you have read the file into a string, split the string with commas to get the array of number.
see this link on how to get its index
http://www.java-forums.org/new-java/12254-direct-way-get-index-array.html
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 26

Expert Comment

by:ksivananth
ID: 33634180
use Scanner, which is optimal than split... check this example, http://www.javapractices.com/topic/TopicAction.do?Id=42

you would use "," as delimite than "=" in the example!
0
 
LVL 1

Accepted Solution

by:
su_mu earned 500 total points
ID: 33634200
Simple sample code using StringTokenizer:


public static void main(String[] args) {
            String findNo = "567";
            String input = "123, 1245, 124, 567, 678, 6787,56778,345,6786,234,89,897,4556,5890,456,890,489,6798";
            StringTokenizer tok = new StringTokenizer(input,",");
            int  i = 1;
            while(tok.hasMoreTokens()){
                  String temp = tok.nextToken().trim();
                  if(!(temp.equals(findNo))){
                        i++;
                        continue;
                  }
                  break;
            }
            System.out.println("The position of the number is "+i);
      }
0
 
LVL 9

Expert Comment

by:Subrat (C++ windows/Linux)
ID: 33639956
//I can search for a particular number
How r u searching the no?
Send the code. It'll be easier to find out what problem ur facing to find the index...
0
 

Author Comment

by:shragi
ID: 33664072
@su_mu

this code works perfectly.... can you tell me how to search for multiple numbers...

search three numbers at a time...

0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SHA2 certs for IIS AND Java? 2 113
Desingning Refactoring existing code 2 26
Convert from a json string array to a Java object 3 53
Problem to Alipay 10 44
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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

829 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