Reached end of file while parsing....Simple....Beginner Programmer - java

I do not get what is wrong, It would be great help if anyone would explain it to me so that I (or you) can fix it.
The error that keeps occurring for many of my programs is:
File: F:\Java Work\Classexample3.java [line: 40]
Error: reached end of file while parsing
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Classexample3 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String answer;
System.out.println("What kind of beverage do you want? (hot or cold)");
answer = br.readline();
if (answer.equals("hot")) {
System.out.println("Please choose an item from the list below:");
System.out.println("- tea \n- coffee");
answer = br.readline();
if (answer.equals("tea")) {
System.out.println("You have purchased a hot tea:");
} else if (answer.equals("coffee")) {
System.out.println("You have purchased a hot coffee");
answer = br.readline();
}
} else if (answer.equals("cold")) {
System.out.println("Please choose an item from the list below:");
System.out.println("- bubble tea \n- pop");
answer = br.readline();
if (answer.equal("bubble tea")) {
System.out.println("You have purchased a cold bubble tea");
} else if (answer.equals("pop")) {
System.out.println("You have purchased a cold pop");
} else {
System.out.println("This item is not on the list");
}
}
}

You are missing a } at the end to close the class.

You're missing a } to close the corresponding public class Classexample3 {. The error is exactly as it states - the Java compiler expects the class declaration to be closed at some point, but the file ended without doing so.

That error means something wasn't closed like a closing bracket is missing. Looks like the closing bracket for your Classexample3 class is missing at the bottom of your code.

Once you close your class with an missing }, you will a lot of other errors.
First, br.readline() should be br.readLine()
Second, answer.equal("bubble tea") should be answer.equals("bubble tea")
These things matter and if you use an IDE, it will make your life as a Java developer a lot easier :)

Related

Scanner input through break-line

Is it possible to catch a break-line as an "input-confirmation"?
The idea is that you skip an input, if it should not be used. Or is there an already existing java method for that problem?
Edit code:
import java.util.Scanner;
public class probe {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Input:");
boolean input = false;
if(scanner.hasNextLine()){
input=true;
}else if(!(scanner.hasNextLine())){
System.out.println(input);
}
System.out.println(input);
}
}
So .hasNextLine() is obviously not the right method to solve my problem, I just implemented it like that, because I think it is getting clearer.
But I think I'm already on the wrong way.
One Solution which come to my mind is, that to compare the char-value of an break-line to the inputed char.
I'm Sorry if I can't explain my problem more properly :(
As Tom already pointed out this should be the code snippet you're looking for:
if (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println(line.isEmpty() ? "Nothing to print out" : "Your input was: " + line);
}

Abbreviation Decoder Script in Java

I am working on a program that is supposed to pull abbreviated text meanings from a list that is created from if-else statements. I am running into trouble with the logic of making the program see an incorrect input and provide a suggestion from the supported list. Here is the code we were given to edit.
import java.util.Scanner;
public class TweetDecoder {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String origTweet = "";
System.out.println("Enter abbreviation from tweet: ");
origTweet = scnr.next();
if (origTweet.equals("LOL")) {
System.out.println("LOL = laughing out loud");
}
else if (origTweet.equals("BFN")) {
System.out.println("BFN = bye for now");
}
else if (origTweet.equals("FTW")) {
System.out.println("FTW = for the win");
}
else if (origTweet.equals("IRL")) {
System.out.println("IRL = in real life");
}
else {
System.out.println("Sorry, don't know that one.");
}
return;
}
}
This is for a class so I would like to know if someone can push me in the right direction rather than give the full answer or the string that I should be using. I feel like is should be something to do with String Comparison or String Access Operations but I cant seem to get it nailed down. If someone can assist I would greatly appreciate it. Thank you in advance!
it wouldn't let me add a comment, so i suppose here will do.
I would suggest looking into the .startsWith method. Its a method contained in the String class.
For example,
if(origTweet.startsWith("L")) //
System.out.println("Perhaps you meant LOL");
sorry if this isn't what you meant / wanted

about java scanner. Error: java.util.NoSuchElementException: No line found

I'm a oversea student. I use blueJ do my assessment. My code could run in terminal well, but after I submit my assessment the page response fails and shows
java.util.NoSuchElementException: No line found
My code:
public void input() {
Scanner keyboard = new Scanner(System.in);
System.out.print("Move (l/r/u/d): ");
String name = keyboard.nextLine();
if(name.equals("l")) {
move(-1,0);
}
else if(name.equals("r")) {
move(1,0);
}
else if(name.equals("u")) {
move(0,-1);
}
else if(name.equals("d")) {
move(0,1);
}
else {
System.out.println("Invalid move");
}
}
I suggest that you read the requirements for your assignments again carefully.
The symptoms clearly indicate that your program is being tested in a context in which there is no input to be read from System.in. The most obvious explanation is that your program is supposed to be getting its input some other way. But unless we see the requirements, we cab only guess what it is supposed to do.
I guess another possibility is that the program that is testing your assignment code is faulty.
By seeing the error line not found,u replace print() into println().

Echo class in Java

Please help me with this HW assignment. I am supposed to modify the EchoNumber class which extends the Echo class to count the number of characters in every line in a text file in addition to displaying the text. Here is the Echo class:
import java.util.Scanner;
import java.io.*;
public class Echo{
String fileName; // external file name
Scanner scan; // Scanner object for reading from external file
public Echo(String f) throws IOException
{
fileName = f;
scan = new Scanner(new FileReader(fileName));
}
// reads lines, hands each to processLine
public void readLines(){
while(scan.hasNext()){
processLine(scan.nextLine());
}
scan.close();
}
// does the real processing work
public void processLine(String line){
System.out.println(line);
}
}
Here is the EchoNumber class, notice where it says "Your code goes here":
import java.io.*;
public class EchoNumber extends Echo
{
// the current line number
private int lineNumber;
public EchoNumber (String datafile) throws IOException
{
super( datafile);
lineNumber=1;
}
// Prints a line with a leading line number and a trailing line length
// Overrides the processLine method in Echo class
public void processLine(String line){
/* your code goes here */
}
}
Here is the EchoTester class:
import java.io.*;
import java.util.Scanner;
public class EchoTester
{
public static void main(String[] args)
{
// uses try/catch to handle IOExceptions in main
try
{
String fileName;
Scanner nameReader = new Scanner(System.in);
System.out.println("Enter a file name");
fileName = nameReader.nextLine();
EchoNumber e = new EchoNumber(fileName);
e.readLines();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
And finally the .txt file:
The best things in life are free
A stitch in time saves nine
Still waters run deep
He teaches ill who teaches all
You can not take it with you when you die
Better untaught than ill taught
Do not cross your bridges before you come to them
Soon learnt soon forgotten
Even a worm will turn
It was the last straw that broke the camels back
The way to a mans heart is through his stomach
If the stone fall upon the egg alas for the egg If the egg fall upon the stone alas for the egg
Where there is a will there is a way
Marry in haste and repent at leisure
One tongue is enough for a woman
If you wish good advice consult an old man
The best advice is found on the pillow
All clouds bring not rain
You can not tell a book by its cover
No news is good news
Bad news travels fast
Live and let live
Birds of a feather flock together
Now is the time
For all good men who actually have the time
To come to the aid of the country in which they live
The output is supposed to be something like:
1 The best things in life are free-32
2 A stitch in time saves nine-27
3 Still waters run deep-21
4 He teaches ill who teaches all-30
5 You can not take it with you when you die-41
6 Better untaught than ill taught-31
7 Do not cross your bridges before you come to them-49
8 Soon learnt soon forgotten-26
9 It was the last straw that broke the camels back-48
Except without spaces between each line. For some reason it fuses into one paragraph if I did not separate each line.
Something like this:
public void processLine(String line){
System.out.println(lineNumber + " " + line + "-" + line.length());
++lineNumber;
}
I haven't tested it, so if it isn't 100% correct, I'll leave it as an exercise for you to complete, but it should put you on the right track. Good luck.
THIS is the correct code. It is important that lineNumber++ is below the print statement!
System.out.println(lineNumber + " " + line + "-" + line.length());
lineNumber++;

How do I set the statement if to read letters instead of numbers?

"if" statement only allows to put numbers in it.
Is there a way to make it read letters?
I'm only in my fifth lesson of Java (I study in a uni and the teacher is very slow but I want to learn things fast)
for example.
import java.util.Scanner;
public class Java {
public static void main (String [] args) {
Scanner scan = new Scanner(System.in);
int answer1;
System.out.println("Do you like Java?");
answer1 = scan.nextInt();
if (answer1 == yes)
System.out.println("Cool ~");
else
System.out.println("Ehh...");
}
}
I want to put "yes" instead of the number 5.
So if the user types "yes" it will print "correct".
P.S. I didn't find a clear answer to that in the search engine.
It's not a duplicated thread as I'm trying to find a clear answer to that.
I need a detailed explanation about it.
I'm still a beginner, using those "high tech java words" won't help me.
You need to modify your program so that your scanner to reads a String instead of an int. You can do that as:
import java.util.Scanner;
public class Java {
public static void main (String [] args) {
Scanner scan = new Scanner(System.in);
String answer1;
System.out.println("Do you like Java?");
answer1 = scan.next();
if (answer1.equals("yes"))
System.out.println("Cool ~");
else
System.out.println("Ehh...");
}
}
I used next() for this since we only want one word (token), but be aware that there are other options for reading Strings.
Notice also that I've changed the test in the condition because it's now a String. See this answer for more on comparing Strings.
You need to modify your program so that your scanner to reads a String instead of an int. You can do that as:
import java.util.Scanner; public class Java {
public static void main (String [] args) {
Scanner scan = new Scanner(System.in);
String answer1;
System.out.println("Do you like Java?");
answer1 = scan.next();
if (answer1.equals("yes"))
System.out.println("Cool ~");
else
System.out.println("Ehh...");
} }
I used next() for this since we only want one word (token), but be aware that there are other options for reading Strings.
Notice also that I've changed the test in the condition because it's
now a String. See this answer for more on comparing Strings.
Ok, what if you want the program to read both words and numbers:
Here's my program (more in depth, when you see the full thing), but this is one of 5 parts (that look a like) where I'm having the program...
public static void Gdr1() {
try {
System.out.print("[Code: Gdr1] Grade 1: %");
Scanner gdr1 = new Scanner(System.in);
Z = gdr1.next();
Z = Double.toString(Grd1);
Grd1 = Double.parseDouble(Z);
if ((Grd1<100)&&(Grd1>=5)) {
Gdr2();
} else if ((Grd1>=100)&&(Grd1<125)) {
System.out.println(" System> Great Job "+Stu+"!");
Gdr2();
} else if (Grd1<5) {
System.out.println("I'm sorry, the lowest grade I am allowed to compute is 5...");
Gdr1();
} else if (Z.equalsIgnoreCase("restart")) {
restart01();
} else {
System.out.println("("+Z+") cannot be resolved in my system...");
Gdr1();
}
} catch (Exception e) {}
}
Now everything works in the program, besides for when the End-User's input = "restart", I know some of the code in the program seems complicated, but it does work (most of it), can anyone help me try to figure this out, its for my portfolio at my school due latest by 1/25/2017 # 11:59 pm.
The things like Z (constant String), ""+Stu+"" (variable input), and [Code: Gdr1] are there for a purpose...

Categories

Resources