#1 Posted by AnimalFather (800 posts) -

so i got a do while loop going and i want to break with a message when Scanner input is lets say X

so i put this in my loop:

//if (banana=="x"){

//System.out.println("Game Over.");

//break;}

and ya it doesnt break or do anything....what gives?

#2 Posted by fishmicmuffin (1044 posts) -

I'm relatively sure you're just breaking out of the if statement in that case. Put it in the condition for the loop itself rather than an if test inside - for example: 
 
while(banana != "x") { 
... 

#3 Edited by GS_Dan (1406 posts) -

@AnimalFather: Hey again :P

Break isn't the best way of quitting a while loop, it's actually frowned upon.

Try setting the requirements of the while loop to take it into account. For example:

while(banana!="x"){
//action
}

EDIT: Ninja'd

#4 Posted by Frostmane (56 posts) -

break will break out of the loop. Are you sure you're hitting that line? If you want to exit the whole program you can use System.exit.

Also you should do "x".equals(variable) because it's null pointer proof and you don't want to compare strings with ==

#5 Posted by Frostmane (56 posts) -

@fishmicmuffin:

Break 100% does not cause a break out of an if statement. Break is also a perfectly valid control flow statement. You just shouldn't do this:

while(true){

if whatever break

}

#6 Edited by NickL (2247 posts) -

edit: just looked at the title again, try using banana.equals

My first question would be is banana a string or a char?

If it is a char, try using single quotes (banana == 'x')

If it is a string, try using .equals (banana.equals("x") )

As stated above though, you should try to include the exit case in the while statement itself if possible.

#7 Edited by AnimalFather (800 posts) -

@fishmicmuffin said:

I'm relatively sure you're just breaking out of the if statement in that case. Put it in the condition for the loop itself rather than an if test inside - for example: while(banana != "x") { ... }

thanks a google.

or wait it didnt work.

ok now it works thanks guys, got some good programmers on this forum.

#8 Posted by fishmicmuffin (1044 posts) -
@Frostmane: Interesting. Why wasn't it working for him, then? Do you know? I'm no expert on Java so I'm curious.
#9 Posted by NickL (2247 posts) -

@Frostmane said:

@fishmicmuffin:

Break 100% does not cause a break out of an if statement. Break is also a perfectly valid control flow statement. You just shouldn't do this:

while(true){

if whatever break

}

break is a valid control flow statement assuming there isn't an easy way around avoiding it and also assuming that no one else will ever see your code. Using break statements can make a while statement a lot more confusing then it needs to be often times.

In a for statement there are a lot of cases where a break is unavoidable however.

#10 Posted by Jimbo (9928 posts) -

You can't keep this to one thread?

#11 Posted by FreakAche (2955 posts) -

Does your actual code use a comparison operator on a String or is what you've shown us here just pseudocode? Strings are stored on the heap, so the comparison operator is just looking at the value of the pointer.

#12 Posted by AnimalFather (800 posts) -

@fishmicmuffin said:

@Frostmane: Interesting. Why wasn't it working for him, then? Do you know? I'm no expert on Java so I'm curious.

the "x".equals(variable) worked with a if within the do while loop.

#13 Edited by AnimalFather (800 posts) -

I have another question.

so im programming a guess the word game. everything works fine. but the problem is that my words cant come from main class or another class but from an external file which has a list of words. and they need to be crypted. and i need to decrypt them and then randomly pick a word to be used in my guess the word game. anyone has a hint or clue or even source code (lol) for something like that?

for instance: FileReader f = new FileReader("C:/Grey/Monaco/Desktop/TheList.txt"); does not work why?

i imported java.io.* too...

#14 Posted by shiptoncraig (149 posts) -

I've always used a BufferedReader, with a FileReader in the constructor. What kind of exception are you getting back?

#15 Posted by AnimalFather (800 posts) -

@shiptoncraig said:

I've always used a BufferedReader, with a FileReader in the constructor. What kind of exception are you getting back?

getting invalid escape sequence and unhandled exception FileNotFound Exception

#16 Posted by FreakAche (2955 posts) -

@AnimalFather said:

I have another question.

so im programming a guess the word game. everything works fine. but the problem is that my words cant come from main class or another class but from an external file which has a list of words. and they need to be crypted. and i need to decrypt them and then randomly pick a word to be used in my guess the word game. anyone has a hint or clue or even source code (lol) for something like that?

for instance: FileReader f = new FileReader("C:/Grey/Monaco/Desktop/TheList.txt"); does not work why?

i imported java.io.* too...

Aren't those slashes backwards?

Also, I'm with @shiptoncraig you should wrap a BufferedReader around your FileReader if you want to avoid a massive headache.

#17 Posted by IllegalizePelvicThrusts (14 posts) -

As far as hints go, take a look at this lesson on file i/o from the official Java Tutorials. Pay close attention to Character Streams and Buffered Streams.
 
Your slashes seem to be backwards in that string. Remember that "\x" (where x is a character) is considered an escape sequence, so "C:\n" will get interpreted as "C:\<newline>".

#18 Posted by Frostmane (56 posts) -

"/"is perfectly fine on *nix systems. On windows you want to use "\\". Or you can save yourself the headache and just use File.separator.

#19 Edited by AnimalFather (800 posts) -

@IllegalizePelvicThrusts said:

As far as hints go, take a look at this lesson on file i/o from the official Java Tutorials. Pay close attention to Character Streams and Buffered Streams. Your slashes seem to be backwards in that string. Remember that "\x" (where x is a character) is considered an escape sequence, so "C:\n" will get interpreted as "C:\<newline>".

i changed the slashes and now it only give the invalid escape seq error

#20 Edited by I2v2nr20I (72 posts) -

You could always just use a Scanner on the file, by passing a new Scanner a File object,

Scanner fileIn = new Scanner(new File("filename.txt"))

Then you can just store fileIn.nextLine() into a local String variable and use str.split(' ') to create an array of words from that line. Loop through the file while(fileIn.hasNext()) and store your word arrays into an ArrayList<String> and viola!

EDIT:

And I think if my memory serves me Scanner.next() delimits on spaces automatically... So that'd be even easier... Yeah...

#21 Posted by shiptoncraig (149 posts) -

@AnimalFather said:

@IllegalizePelvicThrusts said:

As far as hints go, take a look at this lesson on file i/o from the official Java Tutorials. Pay close attention to Character Streams and Buffered Streams. Your slashes seem to be backwards in that string. Remember that "\x" (where x is a character) is considered an escape sequence, so "C:\n" will get interpreted as "C:\<newline>".

i changed the slashes and not it only give the invalid escape seq error

If you do what File.separator/String concatenation, what happens then?

#22 Posted by AnimalFather (800 posts) -

@shiptoncraig said:

@AnimalFather said:

@IllegalizePelvicThrusts said:

As far as hints go, take a look at this lesson on file i/o from the official Java Tutorials. Pay close attention to Character Streams and Buffered Streams. Your slashes seem to be backwards in that string. Remember that "\x" (where x is a character) is considered an escape sequence, so "C:\n" will get interpreted as "C:\<newline>".

i changed the slashes and not it only give the invalid escape seq error

If you do what File.separator/String concatenation, what happens then?

thats greek for me.

#23 Edited by groin (854 posts) -

@AnimalFather said:

I have another question.

so im programming a guess the word game. everything works fine. but the problem is that my words cant come from main class or another class but from an external file which has a list of words. and they need to be crypted. and i need to decrypt them and then randomly pick a word to be used in my guess the word game. anyone has a hint or clue or even source code (lol) for something like that?

for instance: FileReader f = new FileReader("C:/Grey/Monaco/Desktop/TheList.txt"); does not work why?

i imported java.io.* too...

You have to escape the slash characters (e.g., "C:\\Grey\\Monaco\\Desktop\\TheList.txt").

#24 Edited by shiptoncraig (149 posts) -

@AnimalFather said:

@shiptoncraig said:

@AnimalFather said:

@IllegalizePelvicThrusts said:

As far as hints go, take a look at this lesson on file i/o from the official Java Tutorials. Pay close attention to Character Streams and Buffered Streams. Your slashes seem to be backwards in that string. Remember that "\x" (where x is a character) is considered an escape sequence, so "C:\n" will get interpreted as "C:\<newline>".

i changed the slashes and not it only give the invalid escape seq error

If you do what File.separator/String concatenation, what happens then?

thats greek for me.

FileReader f = new FileReader("C:/Grey/Monaco/Desktop/TheList.txt")

Would become:

FileReader f = new FileReader("C:" + File.separator + "Grey" + File.separator + "Monaco" + File.separator + "Desktop" + File.separator + "TheList.txt");

or:

String separator = File.separator;

FileReader f = new FileReader("C:" + separator + "Grey" + separator + "Monaco" + separator + "Desktop" + separator + "TheList.txt");

Better yet, instead of using loads of separate strings and concatenation, use a StringBuilder or StringBuffer object.

#25 Edited by IllegalizePelvicThrusts (14 posts) -

@AnimalFather said:

thats greek for me.

File.separator is a special field that, under Windows, should be the same as "\\". "\\" is the escape sequence for backslash, so you should put that in a string literal ("something surrounded by double quotes like that") when you want backslash.

This might help:

"C:" + File.separator+ "DirectoryName" + File.separator+ "file.txt"

is the same as (on your system):

"C:" + "\\" + "DirectoryName" + "\\" + "file.txt"

is the same as:

"C:\\DirectoryName\\file.txt"

EDIT:

@shiptoncraig said:

Better yet, instead of using loads of separate strings and concatenation, use a StringBuilder or StringBuffer object.

If you are doing this for a class, StringBuilder/StringBuffer might be overkill. Those classes are used to write (arguably) cleaner code and for performance reasons. You'll want to learn how to use them later, but for now, I don't think it's necessary.

#26 Posted by AnimalFather (800 posts) -

doesnt work either

#27 Posted by NickL (2247 posts) -

@AnimalFather said:

doesnt work either

Did you try the \\ or the File.seperator or both?

Do you get the same error still or a different one?

#28 Posted by AnimalFather (800 posts) -

tried everything the fucking thing wont read tried + tried "\\" file seperator

#29 Posted by IllegalizePelvicThrusts (14 posts) -

Are you getting any sort of error message when you compile or run your code?

#30 Posted by shiptoncraig (149 posts) -

Could you

  • paste the bit of code you currently have.
  • check if the file you are trying to access exists in the right location.
  • check that the file isn't currently opened in an editor?

Let's crack this thing.

#31 Edited by AnimalFather (800 posts) -

import java.util.Scanner;

import java.io.*;

public class Game {

public static void main(String[] args) {

FileReader f = new FileReader("C:\grey\TheList.txt") //wont work

FileReader r = new FileReader ("C:" + File.Seperator + "grey" + File.Seperator + "TheList.txt"); // wont work

#32 Posted by Hugh_Jazz (368 posts) -

You didn't try the double-slashes yet?

#33 Edited by AnimalFather (800 posts) -

@Hugh_Jazz said:

You didn't try the double-slashes yet?

FileReader f = new FileReader("C:\\grey\\TheList.txt"); // wont work

FileReader f = new FileReader("C:\\grey\TheList.txt"); // wont work

#34 Posted by Hugh_Jazz (368 posts) -

Now I noticed you misspelled File.Separator(I hope it's not supposed to be File.Seperator). Also, what's the error you're getting when using the doubleslashes?

#35 Posted by AnimalFather (800 posts) -

@Hugh_Jazz said:

Now I noticed you misspelled File.Separator(I hope it's not supposed to be File.Seperator). Also, what's the error you're getting when using the doubleslashes?

invvalid escape sequence

#36 Posted by Hugh_Jazz (368 posts) -

@AnimalFather said:

@Hugh_Jazz said:

Now I noticed you misspelled File.Separator(I hope it's not supposed to be File.Seperator). Also, what's the error you're getting when using the doubleslashes?

invvalid escape sequence

And with FileReader r = new FileReader ("C:" + File.Separator + "grey" + File.Separator + "TheList.txt"); ?

Note to spell it "Separator".

#37 Posted by ZimboDK (848 posts) -
@AnimalFather
 
Try this instead:
 
File f = new File("c:\\grey\\TheList.txt");
FileReader fr = new FileReader(f);
 
If it still doesn't work, then you need to doublecheck the file path.
#38 Posted by AnimalFather (800 posts) -

@ZimboDK said:

@AnimalFather:

Try this instead:

File f = new File("c:\\grey\\TheList.txt");
FileReader fr = new FileReader(f); If it still doesn't work, then you need to doublecheck the file path.

wow first line works not the second

#39 Posted by Hugh_Jazz (368 posts) -

@AnimalFather said:

@ZimboDK said:

@AnimalFather:

Try this instead:

File f = new File("c:\\grey\\TheList.txt");
FileReader fr = new FileReader(f); If it still doesn't work, then you need to doublecheck the file path.

wow first line works not the second

Still the same error?

I suppose it could be an encoding issue, I dunno. I don't know how case-sensitive this stuff is, nor how it handles different file-formats.

#40 Posted by AnimalFather (800 posts) -

@Hugh_Jazz said:

@AnimalFather said:

@ZimboDK said:

@AnimalFather:

Try this instead:

File f = new File("c:\\grey\\TheList.txt");
FileReader fr = new FileReader(f); If it still doesn't work, then you need to doublecheck the file path.

wow first line works not the second

Still the same error?

I suppose it could be an encoding issue, I dunno. I don't know how case-sensitive this stuff is, nor how it handles different file-formats.

Unhandled exception type FileNotFoundException on the second line

#41 Posted by ZimboDK (848 posts) -
@AnimalFather said:

Unhandled exception type FileNotFoundException on the second line
File path is wrong then. Put it in root and remove caps just to be sure (c:\thelist.txt)
#42 Posted by GoodKn1ght (491 posts) -

I did not realize that there was a large programing population on these forums. You may also want to try stackoverflow.com, you can post all of your code and they can usually spot something that you might have looked over.

#43 Edited by SlasherMan (1725 posts) -

@AnimalFather:

File f = new File("C:\\grey\\TheList.txt");

if(!f.exists()){

f.createNewFile();

}

OR

try{

Files.createFile("C:\\grey\\TheList.txt");

}catch(FileAlreadyExistsException e){}

What exactly are you trying to do anyway?

Edit: Nvm, I just noticed you were trying to use a file that should already exist (the code above creates the file for you if it can't find it, but that's useless for what you're trying to do). Ignore the above then, and just make sure the filepath, filename, etc of the file you're trying to open are correct and that it actually exists.

#44 Posted by Hugh_Jazz (368 posts) -

Check the case of the letters, maybe case-sensitivity is messing you up, though I find it unlikely. Also check the file format. Lastly, maybe try to escape the '.' like so:

File f = new File("c:\\grey\\TheList\.txt");

I'm off to sleep, now that my whiskey is done. Best of luck to you, and let us know how you fare.

#45 Posted by IllegalizePelvicThrusts (14 posts) -

@AnimalFather said:

Unhandled exception type FileNotFoundException on the second line

Try wrapping the file access code in a try...catch block:

try {

// Code goes here

}

catch (FileNotFoundException e) {

// error-handing code; you should probably exit the program because, in this situation, it's a major error.

}