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?
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?
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") {
...
}
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 ==
@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
}
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.
@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.
@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.
@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.
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...
I've always used a BufferedReader, with a FileReader in the constructor. What kind of exception are you getting back?
@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
@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.
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>".
"/"is perfectly fine on *nix systems. On windows you want to use "\\". Or you can save yourself the headache and just use File.separator.
@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
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...
@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 @Frostmane suggested and use File.separator/String concatenation, what happens then?
@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 @Frostmane suggested and use File.separator/String concatenation, what happens then?
thats greek for me.
@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").
@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 @Frostmane suggested and use 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.
@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.
Could you
Let's crack this thing.
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
@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
@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
@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".
@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
@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.
@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
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.
@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.
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.
@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.
}
Please Log In to post.
Log in to comment