Basically i need to use JOptionPane and have the user enter a name followed by a . (dot) and a file extension. After they have done that the output should be the extension name without the dot.

Ex
Please enter a file with dot extention > new.html
You choose html

heres what i have, i have no clue on how the heck i get rid of the dot or a way to start after the . (dot)

String input = JOptionPane.showInputDialog(null," Please enter a file plus its dot format");

        JOptionPane.showMessageDialog(null, "you choose " +);

Recommended Answers

All 32 Replies

Have you looked at the String API?

yes, have been looking at it for a bit. Ill let ya know if i find it.

Would i use a indexOf() and then this.charAt(k) == Dot

Dot is the name of my char.

The indexOf will tell you where is the '.' dot:

String s = "abc.txt";
int index = s.indexOf("."); // 3
// a b c  .
// 0 1 2 3

Then use that index as parameter to another method of String that returns what you want.

Thanks, going to try that!

Still having some issues. Will this work while using the JOptionPane?

well i know i need indexOf and a substring. Just no idea how i get

public int indexOf(String str)

in there for my indexOf to work

public static void main(String[] args){



        char Find = '.';
        String input = null; String Output = null;
      

        String In = JOptionPane.showInputDialog(null," Please enter a file plus its dot format");

         int index = input.indexOf(Find);

        Output.substring(Find);

        JOptionPane.showMessageDialog(null," you choose the " +Output);

what i have so far, but i just keep getting this error

Exception in thread "main" java.lang.NullPointerException
at javaapplication17.Main.main(Main.java:32)
Java Result: 1

You have the index of the '.' like I showed you. Now use the substring method to get the part of the string that you need. Then display in any way you want.

The exception tells you where the error is.

Well i have the Output.substring() but i have no idea what i put in between the ().. do i put Index, our Output or find. Ill let ya know if i figure it out

Well i have the Output.substring() but i have no idea what i put in between the ().. do i put Index, our Output or find. Ill let ya know if i figure it out

The exception tells you the line and that you are using something which is null. As for the other: READ THE STRING API FOR THAT METHOD.

I have the substring there, yet i still get errors. I dont think it knows where to end the substring and i dont think it will work when my input and output strings = null.. i get errors when those are there.

I have the substring there, yet i still get errors. I dont think it knows where to end the substring and i dont think it will work when my input and output strings = null.. i get errors when those are there.

You know "where" the '.' is. Look again the substring methods and stop using variables that are null.

Looking at it now, shows my error is on the int index = input.indexOf(Find);

Ill keep seeing whats wrong with it.

Alright got that, thank, now one last thing,
need a tip on how i get the output to not show the .
i assume its in the part where this is.

int index = In.indexOf(Find);

      String one = In.substring(index);

i must be missing one thing.

Good you are on the right track.
I believe that you got rid of the NullPointerExceptions.

But you haven't looked very careful at String API for the substring methods.

Thanks, gonna double check this over, thanks again for the help so far!

I am looking over the API's and the substring can use 2 integers except when i do something like String one = In.substring(index, 0); its out of reach but if i do String one = In.substring(0,index); it gives me the first letters i have typed. Just trying to figure how i get the last letters/extension without the dot.

I am looking over the API's and the substring can use 2 integers except when i do something like String one = In.substring(index, 0); its out of reach but if i do String one = In.substring(0,index); it gives me the first letters i have typed. Just trying to figure how i get the last letters/extension without the dot.

You want the first letters or the last letters? I thought you wanted the first. Anyway. Try to print these:

System.out.println(In.substring(0,index));
System.out.println(In.substring(index));

If you want the last letters without the '.' then it shouldn't be too difficult to understand which one to use and how, provided of course you have understood how they work.

Looking for the last letters. So if its hello.java, i just would like java to come out. im still stuck with that dang . in there lol.

Looking for the last letters. So if its hello.java, i just would like java to come out. im still stuck with that dang . in there lol.

So when you call this:

System.out.println(In.substring(index));

You get:
> .java

Then read again the substring(index) API and see how you can change the argument.

yes, that is correct, i am actually using the JOptionPane so i have a UI or dialog box but yes it still does that.

yes, that is correct, i am actually using the JOptionPane so i have a UI or dialog box but yes it still does that.

Forget the JOptionPane. Try to print the right result first.

Tried the print result and still no go. I really cannot find a way to get rid of the . in this API stuff. I can get it to stop at the . and show the first word but i cannot get rid of it at the end. i may have to ask my teacher on this.

Tried the print result and still no go. I really cannot find a way to get rid of the . in this API stuff. I can get it to stop at the . and show the first word but i cannot get rid of it at the end. i may have to ask my teacher on this.

Read what the subString(int) method does. Forget about the 'index' variable and what it has.

quoted from my book
" return a substring of the String object beginning at the character at index startindex and ending at the character at index endindex -1"

Now i know that its going to begin at the . so know thats why its in there but idk how to get it to begin after the index.

how to get it to begin after the index.

Being able to figure that out has nothing to do with your knowledge of java. It has to do with you being able to think.

Everything you didn't know about coding has been said. There is nothing else I can tell you regarding the coding. You know which method to use and what it does.

Now it is time for you to think and show how smart you are.

commented: Ballsy thing to say, however, much more helpful to OP in the long run. +4
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.