r/javahelp • • Dec 29 '15

i dont know why this program doesn't work

hi all

i am a beginner with java and made this program. but i don't know why it doesn't want to work

here is the code

Slimer2001

0 Upvotes

12 comments sorted by

2

u/AnEmortalKid Coffee Enthusiast Dec 29 '15

When you say it doesn't work, what do you mean?

Does it run? Does it run (System.out.println("do you want to read or write a file?[R|W]");)

Are you using an IDE? (Like eclipse, netbeans or intellij).

What is the program supposed to do? What defines "working".

1

u/Slimer2001 Dec 29 '15

It runs until it come by the try and it goes to the catch but there isn't a error

2

u/AnEmortalKid Coffee Enthusiast Dec 29 '15

You have multiple try catch statements. Which method does it not work in?

-2

u/Slimer2001 Dec 29 '15

Do you know how I can fix it I am a Little noob at this but I try my best

2

u/AnEmortalKid Coffee Enthusiast Dec 29 '15

Well when say it gets to the try part, I am not sure whhich one you refer to. Are you using an IDE like Eclipse?

1

u/Slimer2001 Dec 29 '15

I'm using eclipse (but I does give any error)

2

u/AnEmortalKid Coffee Enthusiast Dec 29 '15

Could it be related to this ""your path"+ fileName" ? try to remove the "your path" from the file reader and file writer since iirc it will create a File with the path "your pathC:/whatevermyfolder/file.xt"

1

u/Slimer2001 Dec 29 '15

Oops i forgot to say that you need to put you own path by my path Sorry

2

u/AnEmortalKid Coffee Enthusiast Dec 29 '15

This is what I got when running it:

do you want to read or write a file?[R|W]
r
Type the name of the file you want to read 
Sorry the filename specified by you is not found

It probably has to do with you using "scr.next()". Take a look at this regarding the Scanner class that you are using (https://www.reddit.com/r/javahelp/wiki/scanner).

So what is happening here is that me pressing enter after my input, creates this "r\n". So the first input you read is the r (for the character). When you say nextLine() , your scanner will read from after the 'r' to the end of the line, which is just "\n" so there's essentially no input.

2

u/AnEmortalKid Coffee Enthusiast Dec 29 '15

Read the scanner wiki, and you'll figure out what you need to change on line 30 (readOrWrite = whatever). To make it work. Sample of it working:

do you want to read or write a file?[R|W]
r
Type the name of the file you want to read 
src\com\experiments\reader\test.txt
Test Text
Test line 2

1

u/Slimer2001 Dec 29 '15

Thank you for the great explainions. I will read that wiki a Change what I need to change

3

u/king_of_the_universe Dec 29 '15

What do you expect it to do, and what does it do instead? If there is an error or exception, please paste the full text of it.