how can I implement java EOF - java

I'm learning java from the very beginning and I'm trying to accept a problem in a programming site, Its very basic, all i need to do is to print a+b till the end of file, I searched everywhere for EOF and all of them implemented an end of file,supposing a real file, but in the problem I'm writing the code of, I shouldn't use actual file.
I use this code in C++:
#include<iostream>;
using namespace std;
int main()
{
int a,b;
while(cin>>a)
{
cin>>b;
cout<<a+b<<endl;
}
}
now I kinda converted it in this way to java:
package a.b;
import java.util.*;
public class AB {
static Scanner in=new Scanner(System.in);
public static void main(String[] args) {
int a,b;
a=in.nextInt();
while(in.nextInt()!=null)
{
b=in.nextInt();
System.out.println(a+b);
a=in.nextInt();
}
}
}
but I don't know how to implement an EOF for it. any help would be appreciated.

system default input stream has no eof ... rather you can implement:
it should not be
in.nextInt()!=null
but rather
in.hasNextInt()
or you may get a line
in.nextLine();
and then extract each number separately by your own method.

Related

Whats wrong with this method? Why won't it pass test case?

This code is supposed to count from an inputted parameter from a user, pass it to a function and it counts it down to one. I believe the program acts as it should, the problem is that it doesn't pass Mooc.fi's tests
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int number = Integer.valueOf(scanner.nextLine());
printFromNumbertoOne(number);
}
public static void printFromNumbertoOne(int number) {
for (int i = number; i > 0; i--) {
System.out.println(i);
}
The error I'm receiving is:
Method printFromNumberToOne(int) of class FromParameterToOne missing
What am I missing? And is there a way to check the test cases? Mooc.fi seems like it's very picky on what it takes for answers.
Thanks!
CAMELCASE of the method!
After beating my head against this problem all day it came to me after taking a break.
It should be printFromNumberToOne

How can i reduce Execution Time for this code

import java.util.*;
import java.io.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Print output to STDOUT. Your class should be named Solution. */
Scanner sc= new Scanner(System.in);
int siz= sc.nextInt();
int max= sc.nextInt();
CircularQueue<Long> queue = new CircularQueue<>(max);
while(siz-->0){
queue.add(sc.nextLong());
}
System.out.println(queue.size());
for(int i=queue.size();i>0;i--){
System.out.print(queue.get(i-1)+" ");
}
}
public static class CircularQueue<E> extends LinkedList<E> {
private int capacity = 10;
public CircularQueue(int capacity){
this.capacity = capacity;
}
#Override
public boolean add(E e) {
if(contains(e)){
return true;
}
if(size() >= capacity)
removeFirst();
return super.add(e);
}
}
}
In this Program I have created fixed size Linked List in which while adding values greater than the size of list then it removes old value and adds new value at Last.
Suggest some changes in code without changing the logic. Thanks in Advance
contains() of LinkedList has O(n) time at the worst case.
Construct an auxiliary HashSet for this purpose, or invent another way to trace elements which had appeared already.
Scanner class is slow for large inputs, so try to avoid it. you can use BufferedReader class which is faster than Scanner class.
Scanner is a much more powerful utility than BufferedReader but BuffredReader has a significantly large buffer (8KB) than Scanner (1KB) and also Scanner uses regular expression to read and parse text input which makes it slow.
The call to queue.size in main method.
You can save one calculation if you call it once and save its value then reuse in sysout statement and in for loop.
Avoid generalizing imports. Ex. import java.io.*
Put exactly the packages you want specifically to access.
That may save also little bit of time.

Java communication with a website

I made a simple chatbot that responds to certain prompts with string output in Java. I want this program to output it's responses to an element on an HTML based webpage, and read input from the a different element on the same page.
Does anyone know how I could accomplish this?
Here's the code if you're interested:
import java.util.Scanner;
public class botRun
{
#SuppressWarnings("resource")
public static void main(String[] args)
{
PepperBot bot = new PepperBot();
Scanner in = new Scanner(System.in);
String input = in.nextLine();
while(true)
{
if(bot.shouldRespond(input))
{
System.out.println(bot.getRespond(input));
}
input = in.nextLine();
}
}
}
all other methods, "shouldRespond(String x)" and getRespond(String x) are in a separate class for object "PepperBot()" and check if the bot should respond and get the appropriate response respectively.
Pretty much any solution will do, as long as it isn't costly.

How to pass information through a method and returning a value

I'm new to Java, and need help. I have been asked to write a program that rolls dice and determines the chance of the player to get two "1s" on the top face of the dice. I have different functions such as role(), getTopFace() etc. I want to be get what the number on the dice is using these functions, but don't know how to call them in my main function. Here's my code:
import javax.swing.JOptionPane;
import java.util.Random;
public class SnakeEyes {
private final int sides;
private int topFace;
public static void main(String[]args)
{
String numberSides;
int n;
numberSides=JOptionPane.showInputDialog("Please enter the number of sides on the dice:");
n = Integer.parseInt ( numberSides);
int[]die=new int[n];
for (int index=0; index<n;index++)
{
die[index]=index+1;
}
//Here is where I want to get information from my functions and calculate the ods of getting two 1's.
}
public void Die(int n)
{
if(n>0)
{
int sides=n;
topFace=(int)(Math.random()*sides)+1;
}
else{
JOptionPane.showMessageDialog(null, " Die : precondition voliated");
}
}
public int getTopFace(int topFace)
{
return topFace;
}
public int role(int[] die)
{
topFace=(int)(Math.random()*sides)+1;
return topFace;
}
}
Make an object of your class SnakeEyes in your main method, and call the required functions using that object.
Example:
SnakeEyes diceObj = new SnakeEyes();
int topFace = diceObj.role(n,....);
If you want to call this functions from main this functions must be "static", because main its a static function and static function can only call other static functions.
But... this is a very ugly design for a java program, before jumping to write java code you need to understand at least a little about object orientation. For example, why you can't call a non-static function from a static function?, the answer of this question requires knowledge about object orientation and its a knowledge you need if you want to write serious java code.

".class" error in Java

I am trying to run this program but I cannot, the compiler is sending me a ".class" error.
Can somebody help me with my problem and if it is possible a general tip about ".class" error?
Here is the program:
import java.io.*;
class Bus
{
private int kostos;
private int plithos;
private int typepiv;
Bus(int x,int y,int z)
{
kostos=x;
plithos=y;
typepiv=z;
}
public void KB(int[] x)
{
try{
for(int i=1;i<5;i++)
{
if(typepiv==2)
{
plithos=plithos+plithos/2;
kostos=kostos-kostos/2;
}
if(typepiv==3)
{
plithos=plithos-plithos/5;
kostos=kostos-kostos*25/100;
}
if(typepiv==1)
{
plithos=plithos;
kostos=kostos;
}
x[i]=plithos*kostos;
}
} catch(Exception ex){
ex.printStackTrace();
}
}
}
class testBus
{
public static void main(String args[])
{
String leof[]=new String[4];
int leof1[][]=new int[4][3];
for(int i=1;i<5;i++)
{
System.out.println("dwste onoma leoforiou");
leof[i]=UserInput.getString();
System.out.println("dwste kostos thesis enilika");
leof1[i][1]=UserInput.getInteger();
System.out.println("dwste plithos thesewn");
leof1[i][2]=UserInput.getInteger();
System.out.println("dwste tupos epibath gia enilikes=1,gia
paidia=2,gia suntaksiouxous=3");
leof1[i][3]=UserInput.getInteger();
Bus leof2=new Bus(leof1[i][1],leof1[i][2],leof1[i][3]);
}
int KostEnoik[]=new int[4];
----->leof2.KB(KostEnoik);
System.out.print("onoleo");
System.out.print(" ");
System.out.print("plithos");
System.out.print(" ");
System.out.print("kost(EURO)");
System.out.print("typepiv");
System.out.print(" ");
System.out.print("apotelesma kostEnoik");
for(int g=1;g<5;g++)
{
System.out.print(leof[g]);
System.out.print(leof1[g][2]);
System.out.print(leof1[g][1]);
System.out.print(leof1[g][3]);
System.out.print(KostEnoik[g]);
}
}
}
the compiler message says :
testBus.java:56:error:cannot find symbol
leof2.KB(KostEnoik);
symbol:bariable leof2
location:class testBus
1 error
Remove the array brackets [] when invoking KB
leof2.KB(KostEnoik);
and remove the preceding enclosing brace }.
Aside: Java naming conventions indicate that variables start with a lowercase letter e.g. kostEnoik. Also consider giving the method KB a meaningful name, e.g. calculateCost
Read Java naming conventions
concern is with your access
leof2.KB(KostEnoik[]);
You are trying to access the "leof2" variable outside of the scope in which it is defined i.e. outside for loop and scope is upto for loop and that's why the compiler will not be able to find that varialble .
leof1[i][3]=UserInput.getInteger();
Bus leof2=new Bus(leof1[i][1],leof1[i][2],leof1[i][3]);
}
int KostEnoik[]=new int[4];
leof2.KB(KostEnoik[]);
You are trying to access the "leof2" variable outside of the scope in which it's defined (in this particular case, the for loop) and that's not allowed.
method KB takes an int array as argument, but you don't have to add the [] when passing the argument. The correct line is
leof2.KB(KostEnoik);
However, there's something pretty odd with you logic: you're repeatedly (for loop) setting leof2, but only the last iteration of the loop will have any effect. I'm almost certain that that's not what you actually want, but the correct answer to where Bus leof2 should actually be defined depends on the correction of that issue.
leof2.KB(KostEnoik); this is the main culprit. whether you have imported UserInput.
Also try to go through the Java Basics
any method can be invoked using object when it is non static or class name when it is static. Please consider this link
Get leof2 object out side the For Loop.
Don't type [] when you pass the array as argument "leof2.KB(KostEnoik[]);".

Categories