4 Replies - 1146 Views - Last Post: 09 December 2012 - 08:53 AM Rate Topic: -----

#1 teamsilvamma  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 153
  • Joined: 17-January 12

chosing numbers from a list

Posted 08 December 2012 - 07:28 PM

I need some guidance guys. I'm reading some numbers from a string of numbers:

12345.67 890123.45

I need to break this number in two sets (before and after the space between them) then do some calculations and blah blah blah.

I'm having trouble phrasing this in the web.so I can't even find a good tutorial. I looked into ParseInt and CharAt() but it didn't work.

I would post the code if you want me to, but I think it's unnecessary. All I need is to know what command java uses to choose the first x numbers in a string of numbers and the last x numbers in the string of numbers.

Thank you

Is This A Good Question/Topic? 0
  • +

Replies To: chosing numbers from a list

#2 hhpaet  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 10-November 12

Re: chosing numbers from a list

Posted 08 December 2012 - 07:54 PM

Check out the StringTokenizer class and/or the String class's split method.
Was This Post Helpful? 1
  • +
  • -

#3 teamsilvamma  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 153
  • Joined: 17-January 12

Re: chosing numbers from a list

Posted 08 December 2012 - 09:12 PM

View Posthhpaet, on 08 December 2012 - 09:54 PM, said:

Check out the StringTokenizer class and/or the String class's split method.



Not exactly what I need tho. For what I can get from this, this breaks the string in words. I need to be able to select the values from both sides of the numbers but also if I wanted to use just the first three number I want to be able to do so. But great concept tho.

Thanks anyway.
Was This Post Helpful? 0
  • +
  • -

#4 darek9576  Icon User is online

  • D.I.C Lover

Reputation: 198
  • Posts: 1,689
  • Joined: 13-March 10

Re: chosing numbers from a list

Posted 09 December 2012 - 03:27 AM

I dont understand what you are trying to accomplish.
Why this would not work?


public class MainClass {
	
	public static void method(String string){
		
		String[] array = string.split(" ");
		long[] array2 = new long[array.length];
		
		for(int i=0; i < array.length; i++){
			array2[i] = Long.parseLong(array[i]);
		}
		
	}
}



Was This Post Helpful? 1
  • +
  • -

#5 teamsilvamma  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 153
  • Joined: 17-January 12

Re: chosing numbers from a list

Posted 09 December 2012 - 08:53 AM

View Postdarek9576, on 09 December 2012 - 05:27 AM, said:

I dont understand what you are trying to accomplish.
Why this would not work?


public class MainClass {
	
	public static void method(String string){
		
		String[] array = string.split(" ");
		long[] array2 = new long[array.length];
		
		for(int i=0; i < array.length; i++){
			array2[i] = Long.parseLong(array[i]);
		}
		
	}
}




I tried implementing that but I couldn't figure it out. I'll post the code so that you can see what I am doing. This code is to be ran in a linux OS. The file path you see there right now is a simple test.txt file with some numbers in it that I stored in my CPU so I can test the code but it will actually be reading a /proc/uptime file path in a linux system that gives real time uptime and idletime of the CPU in this format: "4613536.51 4599887.19". The first double is the uptime and the double after the space is the idletime.

After I extract this file content, the program activates a work.java program that performs a series of simple calculations for about 40000 times (a simple 2 * 3 = 6 calculation). The purpose of this is to make the CPU busy.

The program automatically reads the uptime/idletime again and compares both numbers.

Right up to here, I am good. Here's the problem I am having, which I think it's pretty simple. In order for me to perform the calculations I need, I first need to separate the two doubles, because right now my program is calculating the entire string of numbers as one whole number. So, I need to be able to store the first double in a variable called beginTotalTime, and the second double in a beginIdleTime. Read it again after calculation took place and call these endTotalTime and endIdleTime, and do the rest .

sorry about the long explanation. hopefully I covered it

here's the code I have

class FileData extends ReadFile {
    public static void main(String args[]) {
            
            //Just a pretty label
            System.out.println("\n\tUPTIME/IDLETIME CALCULATOR");
                            
            /*calls the getContent method from ReadFile.java runs the 
             * calculation method from work.java and returns the reading again*/
                System.out.print("Your uptime/idletime beginning time is: ");
                ReadFile firstReading = new ReadFile();
                double beginTotalTime = firstReading.getContent();
                firstReading.getBusy();
                System.out.print("Your uptime/idletime after calculation took place in the CPU is: ");
                ReadFile lastReading = new ReadFile();
                                                       
                double endTotalTime = lastReading.getContent();
                double programTotalTime = endTotalTime - beginTotalTime;
            
                System.out.println(programTotalTime); // I'm just checking my calculation
                /* This is the calculations I need
                 * programTotalTime = endTotalTime - beginTotalTime;
                 * programIdleTime = endIdleTime - beginIdleTime;
                 * programWorkTime = programTotalTime - programIdleTime;
                 * percentage = (programWorkTime / programTotalTime)* 100;
                 */
              
        }//end main
}//end FileData



import java.io.*;

public class ReadFile extends work {
    
    public double getContent(){
        
        try {
            // file path openning
            FileInputStream fileData;
            fileData = new FileInputStream("C:/Users/Luis/Desktop/test1.txt");
  
            // declaring the objects
            DataInputStream input = new DataInputStream(fileData);
            BufferedReader br = new BufferedReader(new InputStreamReader(input));
  
            String fContent;
        
            //Read File content and stop when there are no more lines
            while ((fContent = br.readLine()) != null){
                System.out.println (fContent);
            }//end while
                
            //Close the input
            input.close();
        }//end try
    
        //Catch exception in case file is wrong
        catch (Exception e){
            System.out.println("Sorry, uptime and idletime was not captured " + 
                "\nPlease use correct file path");
        }//end catch
    
        return 0;
    }//end getContent
    
    /***************************************************************** 
     * This method calls the math calculations in work.java
     * This is called from this class because this is the class that extends
     * the subclass work, where the calculation method resides
     * 
     * I could have included the math method here but good java  programming 
     * includes making sure that your work is divided for easy maintenance. In
     * this case, if the calculations need to be fixed, there is only one class
     * that needs to be reviewed.
     ******************************************************************/
    public void getBusy(){
        work calc = new work();
        calc.calculation(); 
    }//end getBusy
    
}//end ReadFile


public class work {
        
    public double calculation() { 
        
        double y = 0.0;
        double x = 3.0; 
        double e = 2.0; 
        int i,j; 
        for (i = 0; i < 5; i++) { 
            for (j = 0; j < 400000; j++) { 
               y = Math.pow(x,e); 
            }//end inner for loop 
            System.out.printf("Loop %d of work cycle\n", i ); 
        }//end outer for loop
    return y;
    }//end calculation
}//end work



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1