I have got given a homework:

FileDescription program requests (as shown) and reads three lines of text. It then processes the data.

For each line the first string (containing no spaces) on the line is a file name – followed by a file description.
If the file name has the suffix (ends with) .txt then the name and description is printed out e.g
XXXX.txt: Stuff about the file
where XXXX.txt is the file name. Otherwise output is like this:
FFFF: Not a text file
where FFFF is the filename.


Example:
Program Name : FileDescription.java
(Example) Input/Output:
Please enter file name and description:
list.txt This my shopping list
list.txt: This my shopping list
Please enter file name and description:
batty.exe A compiled, executable file
batty.exe: Not a text file
Please enter file name and description:
Blip.class A Java byte file
Blip.class: Not a text file

Questions:

1. How can I split the user input on each line into two strings so I manipulate them when I don't know how long the first string is?

Recommended Answers

All 7 Replies

The method: String.split() Check the API:

String s = "a b c d";
String [] tok = s.split(" ");
System.out.println(tok.length);

It would be easy if you use the "contains(...)" method.

String f = "abcde";
System.out.println(f.contains("bc");
System.out.println(f.contains("de");

hai!

First you read filname like (java.txt,java.exe....) as a first argument...

then check it string.endwith(".txt");

if its then you get next argument like..("this is txt file","have a nice day"...)


split(" ") can do but it will split every space as another string...

it is better to get input as every line ...until you wish to quit...

hai!

First you read filname like (java.txt,java.exe....) as a first argument...

then check it string.endwith(".txt");

if its then you get next argument like..("this is txt file","have a nice day"...)


split(" ") can do but it will split every space as another string...

it is better to get input as every line ...until you wish to quit...

In the first post the OP said that each line has the file name and the description. I assume that this is the requirements. He said that the line would be like:

list.txt This my shopping list
Blip.class A Java byte file

As far as using this call:

String f = "abcde";
System.out.println(f.contains("bc");
System.out.println(f.contains("de");

There is always a VERY small posibility that the description might contain this: '.txt'

A better aproach, than my initial would be this:

String s = "Blip.class A Java byte file";
int index = s.indexOf(" "); // returns the first occurence of " "
String file = s.subString(0,index);
String des = s.subString(index+1);

If you use the split(" ") method at the above String, as I wronlgy suggested, the line : "Blip.class A Java byte file" would be splitted into 5 tokens. You could of course take the first token and concat the rest, but it is not very efficient.

Don't forget to check the String API or any other class's API you are using.

Hi I have written the code for this.It works fine.Hope it helps.

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class StringEx 
{
	int count = 0;
	String text;
	
	public static void main(String[] args)
	{
		StringEx s = new StringEx();	
		
		s.readFileToGetFileDescription();
	}
	
	
	private void readFileToGetFileDescription()
	{
		try
		{
			BufferedReader br = new BufferedReader(new FileReader("text.txt"));
			
			String line;
			while((line=br.readLine()) != null)
			{
				text += line;
				count++;
				
				if(count%3 == 0)
				{
					int index = text.indexOf(".");
					String fileEx = text.substring(index,index+4);
					if(fileEx.equalsIgnoreCase(".txt"))
					  System.out.println(text.substring(index+5));
					
					text = "";
				}
				
			}
		}
		catch(Exception  e)
		{
			e.printStackTrace();
		}
	}
}
Name:text.txt Description:This is a text file with long description.
Sounds good.
Lets see.
Name:anotherFile.exe Description:Whatever
Does not matter.
Wont print.
Name:another.txt Description:This will print again.
I hope so.
Lets see.

Output:
Description:This is a text file with long description.Sounds good.Lets see.
Description:This will print again.I hope so.Lets see.

commented: Thats for him to do not you. +0

>AbhikGhosh

What do you think you are doing by giving away code like this ? It is for him to do and for us to help him do it on his own, thats the whole essence. I suggest you try to avoid this in the future.

not to mention you're seriously limiting the possible extensions to use, since you hardcoded them

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.