Click here to Skip to main content
15,905,682 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to read inputs from command line.
My Input is somewhat like this :

# the total size of physical memory (units are B, KB, MB, GB)
512MB 2 # the following are memory allocations
{
abc = alloc(1KB);
{
y_ = alloc(128MB);
x1 = alloc(128MB);
y_ = alloc(32MB);
for (i = 0; i < 256; i++) abc[i] = alloc(512kB);
x1 = alloc(32MB); x2 = alloc(32MB); x3 = alloc(32MB);
x1.next = x2; x2.next = x3; x3.next = x1;
}
abc = alloc(256MB);
}


How can I access each line ?

Any Help???
Posted
Updated 10-May-13 8:38am
v2
Comments
NSSDNS 12-May-13 22:11pm    
I have used this code but I am not sure how I can handle EOF .
Here I handled it with '}' but if multiple loops in input then it will stop executing after 1st '}'.


for(int getlnst=0; getlnst<100; getlnst++)
{
InputInstArray[getlnst] = InstStorageBuf.readLine();
LinesCounter = LinesCounter+1;
if(InputInstArray[getlnst].equals("}"))
break;
}
Filipe Marques 13-May-13 4:42am    
So, yours input came from a file? - http://stackoverflow.com/questions/7413830/java-read-line-from-file - You can get each linde from file and do what you want to do with it. Filipe
NSSDNS 13-May-13 10:36am    
No , I am collecting my all data in buffer & from there I am using it.
Filipe Marques 13-May-13 11:29am    
Why you do not check if the return buffer is empty (yourBuffer.length) or is null? I'm suppose if you reach the EOF, buffer will came null or empty.
NSSDNS 13-May-13 16:57pm    
Yes, if I am checking with EOF its not working. I could not find any method for reading buffer length.

Do you mind to share me some sample code with use of it?

1 solution

Hi,

Try this:

Java
public class Main {
	public static void main(String[] args) {
		String initialString = "line1\nline2\nline3";
		System.out.println(initialString);
		System.out.println();
		
		// Get each line of your initialString
		String[] eachString = initialString.split("\n");
		for(int i = 0; i < eachString.length; i++) {
			System.out.println("My line " + i + ": " + eachString[i]);
		}
	}
}


[UPDATE]

Java
String InputInstArray[] = new String[100];
BufferedReader InstStorageBuf= new BufferedReader(new InputStreamReader(System.in));

//Read Input Instructions
for(int getlnst=0; getlnst<100; getlnst++) {
	InputInstArray[getlnst] = InstStorageBuf.readLine();
	//LinesCounter = LinesCounter+1;
	//if(InputInstArray[getlnst].equals("}"))
	if(InputInstArray[getlnst].isEmpty()) {
		break;
	}
}


What I'm supposing what do you want is that you want to read data from command line and when the user wants not insert more data, stop the loop.

Best Regards,
Filipe
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900