Java Break; for string input?

Avatar image for animalfather
AnimalFather

1012

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#1  Edited By AnimalFather

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?

Avatar image for fishmicmuffin
fishmicmuffin

1071

Forum Posts

702

Wiki Points

0

Followers

Reviews: 0

User Lists: 6

#2  Edited By fishmicmuffin

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") { 
... 

Avatar image for gs_dan
GS_Dan

1438

Forum Posts

68

Wiki Points

0

Followers

Reviews: 15

User Lists: 1

#3  Edited By GS_Dan

@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

Avatar image for deactivated-63cabd6b28362
deactivated-63cabd6b28362

81

Forum Posts

539

Wiki Points

0

Followers

Reviews: 0

User Lists: 8

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 ==

Avatar image for deactivated-63cabd6b28362
deactivated-63cabd6b28362

81

Forum Posts

539

Wiki Points

0

Followers

Reviews: 0

User Lists: 8

@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

}

Avatar image for nickl
NickL

2276

Forum Posts

695

Wiki Points

0

Followers

Reviews: 0

User Lists: 2

#6  Edited By NickL

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.

Avatar image for animalfather
AnimalFather

1012

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#7  Edited By AnimalFather

@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.

Avatar image for fishmicmuffin
fishmicmuffin

1071

Forum Posts

702

Wiki Points

0

Followers

Reviews: 0

User Lists: 6

#8  Edited By fishmicmuffin
@Frostmane: Interesting. Why wasn't it working for him, then? Do you know? I'm no expert on Java so I'm curious.
Avatar image for nickl
NickL

2276

Forum Posts

695

Wiki Points

0

Followers

Reviews: 0

User Lists: 2

#9  Edited By NickL

@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.

Avatar image for jimbo
Jimbo

10472

Forum Posts

2

Wiki Points

0

Followers

Reviews: 0

User Lists: 3

#10  Edited By Jimbo

You can't keep this to one thread?

Avatar image for freakache
FreakAche

3102

Forum Posts

114

Wiki Points

0

Followers

Reviews: 0

User Lists: 8

#11  Edited By FreakAche

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.

Avatar image for animalfather
AnimalFather

1012

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#12  Edited By AnimalFather

@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.

Avatar image for animalfather
AnimalFather

1012

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#13  Edited By AnimalFather

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...

Avatar image for shiptoncraig
shiptoncraig

153

Forum Posts

314

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#14  Edited By shiptoncraig

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

Avatar image for animalfather
AnimalFather

1012

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#15  Edited By AnimalFather

@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

Avatar image for freakache
FreakAche

3102

Forum Posts

114

Wiki Points

0

Followers

Reviews: 0

User Lists: 8

#16  Edited By FreakAche

@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.

Avatar image for illegalizepelvicthrusts
IllegalizePelvicThrusts

16

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 1

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>".

Avatar image for deactivated-63cabd6b28362
deactivated-63cabd6b28362

81

Forum Posts

539

Wiki Points

0

Followers

Reviews: 0

User Lists: 8

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

Avatar image for animalfather
AnimalFather

1012

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#19  Edited By AnimalFather

@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

Avatar image for i2v2nr20i
i2v2nr20i

79

Forum Posts

53

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#20  Edited By i2v2nr20i

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...

Avatar image for shiptoncraig
shiptoncraig

153

Forum Posts

314

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#21  Edited By shiptoncraig

@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?

Avatar image for animalfather
AnimalFather

1012

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#22  Edited By AnimalFather

@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.

Avatar image for groin
groin

870

Forum Posts

34

Wiki Points

0

Followers

Reviews: 0

User Lists: 4

#23  Edited By groin

@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").

Avatar image for shiptoncraig
shiptoncraig

153

Forum Posts

314

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#24  Edited By shiptoncraig

@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.

Avatar image for illegalizepelvicthrusts
IllegalizePelvicThrusts

16

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 1

@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.

Avatar image for animalfather
AnimalFather

1012

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#26  Edited By AnimalFather

doesnt work either

Avatar image for nickl
NickL

2276

Forum Posts

695

Wiki Points

0

Followers

Reviews: 0

User Lists: 2

#27  Edited By NickL

@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?

Avatar image for animalfather
AnimalFather

1012

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#28  Edited By AnimalFather

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

Avatar image for illegalizepelvicthrusts
IllegalizePelvicThrusts

16

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 1

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

Avatar image for shiptoncraig
shiptoncraig

153

Forum Posts

314

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#30  Edited By shiptoncraig

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.

Avatar image for animalfather
AnimalFather

1012

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#31  Edited By AnimalFather

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

Avatar image for hugh_jazz
hugh_jazz

475

Forum Posts

316

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#32  Edited By hugh_jazz

You didn't try the double-slashes yet?

Avatar image for animalfather
AnimalFather

1012

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#33  Edited By AnimalFather

@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

Avatar image for hugh_jazz
hugh_jazz

475

Forum Posts

316

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#34  Edited By hugh_jazz

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?

Avatar image for animalfather
AnimalFather

1012

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#35  Edited By AnimalFather

@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

Avatar image for hugh_jazz
hugh_jazz

475

Forum Posts

316

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#36  Edited By hugh_jazz

@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".

Avatar image for zimbodk
ZimboDK

863

Forum Posts

20

Wiki Points

0

Followers

Reviews: 1

User Lists: 2

#37  Edited By ZimboDK
@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.
Avatar image for animalfather
AnimalFather

1012

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#38  Edited By AnimalFather

@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

Avatar image for hugh_jazz
hugh_jazz

475

Forum Posts

316

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#39  Edited By hugh_jazz

@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.

Avatar image for animalfather
AnimalFather

1012

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#40  Edited By AnimalFather

@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

Avatar image for zimbodk
ZimboDK

863

Forum Posts

20

Wiki Points

0

Followers

Reviews: 1

User Lists: 2

#41  Edited By ZimboDK
@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)
Avatar image for goodkn1ght
GoodKn1ght

522

Forum Posts

45

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#42  Edited By GoodKn1ght

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.

Avatar image for slasherman
SlasherMan

1723

Forum Posts

53

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#43  Edited By SlasherMan

@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.

Avatar image for hugh_jazz
hugh_jazz

475

Forum Posts

316

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#44  Edited By hugh_jazz

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.

Avatar image for illegalizepelvicthrusts
IllegalizePelvicThrusts

16

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 1

@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.

}