I am a newb with Java so don't bite me please..
I've made this method, but it will not show up in the console screen, why?
public class ADSopgave2K1 {
public static void main(String[] args) {
}
public void print(String s, int pos) {
s = "";
pos = s.length();
int count = s.length();
char[] ray;
System.out.println("Enter a word: ");
Scanner userInputF = new Scanner(System.in);
s = userInputF.nextLine();
ray = s.toCharArray();
for (int t = 0; t < s.length(); t++) {
System.out.println(ray[t]);
return;
}
}
}
You did not call that method yet. Try to call your method.
public static void main(String[] args) {
ADSopgave2K1 intance=new ADSopgave2K1();
intance.print();
}
Edit
public void print() {
System.out.println("Enter a word: ");
Scanner userInputF = new Scanner(System.in);
String s = userInputF.nextLine();
char[] ray = s.toCharArray();
for (int t = 0; t < s.length(); t++) {
System.out.println(ray[t]);
}
}
When you run your program, Java will call main(String[] args).
But this is an empty function, so you will not see any output.
Becasue, you did not call anything in main(String[] args) method. Make your print method static and call this to your main method.
public static void print(String s, int pos){
}
EDIT:
public static void main(String[] args){
print("test",1);
}
ADSopgave2K1 r=new ADSopgave2K1();
r.print("jai", 4);
Create an object of your class in side main method and then call its method.
You should call print() method.
public class ADSopgave2K1 {
public static void main(String[] args)
{
print("Hello World", 1);
}
public void print(String s, int pos)
{
s = "";
pos = s.length();
int count = s.length();
char[] ray;
System.out.println("Enter a word: ");
Scanner userInputF = new Scanner(System.in);
s = userInputF.nextLine();
ray = s.toCharArray();
for (int t = 0; t < s.length(); t++) {
System.out.println(ray[t]);
return;
}
}
}
You have to call your method inside main() by creating instance for your class
either you make print method static and call it with correct argument
Or
make instance of ADSopgave2K1 class and call it with correct args
Related
So I need to write a method which accepts one String object and one integer and repeat that string times integer.
For example: repeat("ya",3) need to display "yayaya"
I wrote down this code but it prints one under the other. Could you guys help me please?
public class Exercise{
public static void main(String[] args){
repeat("ya", 5);
}
public static void repeat(String str, int times){
for(int i = 0;i < times;i++){
System.out.println(str);
}
}
}
You are printing it on new line, so try using this :
public static void repeat(String str, int times){
for(int i = 0;i < times;i++){
System.out.print(str);
}
}
You're using System.out.println which prints what is contained followed by a new line. You want to change this to:
public class Exercise{
public static void main(String[] args){
repeat("ya", 5);
}
public static void repeat(String str, int times){
for(int i = 0; i < times; i++){
System.out.print(str);
}
// This is only if you want a new line after the repeated string prints.
System.out.print("\n");
}
}
Change System.out.println() to System.out.print().
Example using println():
System.out.println("hello");// contains \n after the string
System.out.println("hello");
Output:
hello
hello
Example using print():
System.out.print("hello");
System.out.print("hello");
Output:
hellohello
Try to understand the diference.
Your example using recursion/without loop:
public class Repeat {
StringBuilder builder = new StringBuilder();
public static void main(String[] args) {
Repeat rep = new Repeat();
rep.repeat("hello", 10);
System.out.println(rep.builder);
}
public void repeat(String str, int times){
if(times == 0){
return;
}
builder.append(str);
repeat(str, --times);
}
}
public class pgm {
public static void main(String[] args){
repeat("ya", 5);
}
public static void repeat(String str, int times){
for(int i = 0;i < times;i++){
System.out.print(str);
}
}
}
I want to pass the result of this method.
static public int[][] scanCube(Cube c){
int counter0 = 0;
Scanner in = new Scanner(System.in);
counter0 = 0;
while(counter0 < 4){
cube[BOTTOM][counter0] = Integer.parseInt(in.nextLine());
counter0++;
}
return cube;
}
To this constructor so that I can call the method above in main.
private Cube(int [][] Scancube){
cube = new int[Scancube.length][];
for(int i = 0; i < Scancube.length; i++){
cube[i] = Arrays.copyOf(Scancube[i], Scancube[i].length);
}
}
So I can use this in main like this.
public static void main(String[] args) {
Cube c = new Cube();
Cube.scanCube(c);
System.out.println(c);
Cube.solve(c);
}
int[][] value= Cube.scanCube(c);
System.out.println(value);
Cube.solve(value);
Try like this, as you need to store the value first in the variable of the type you are using then you can pass the variable as a input for your next method.
In the following program, the user is supposed to enter a String (name of a City) and the program should return the index of the corresponding City in the array.
But I get an error, in the subroutine indexCities the following message:
"nameCity cannot be resolved".
I guess it is a problem of variable scoping but I don't figure out how I should do.
Thanks for your help.
import java.util.Scanner;
public class villes {
public static void main(String[] args) {
String cities[] = {"Vierzon","Salbris","Nouans","LB","LFSA","Orleans"};
Scanner input = new Scanner(System.in);
String nameCity = input.nextLine();
indexCities(cities);
}
public static int indexCities(String cities[]) {
for (int i = 0; i < cities.length; i++) {
if(nameCity.equals(cities[i])) {
System.out.println(i);
break;
}
}
}
}
nameCity is a local variable inside your main method. You can not access it outside the method.
One option for you is to pass the nameCity also as an argument in indexCities method. Also return type of your indexCities method should be void since you are not returning anything.
public static void main(String[] args) {
String cities[] = {"Vierzon","Salbris","Nouans","LB","LFSA","Orleans"};
Scanner input = new Scanner(System.in);
String nameCity = input.nextLine();
indexCities(cities, nameCity);
}
public static void indexCities(String cities[], String nameCity){
for (int i = 0; i < cities.length; i++) {
if(nameCity.equals(cities[i])) {
System.out.println(i);
break;
}
}
}
You could do it in this way:
public static void main(String[] args) {
String cities[] = { "Vierzon", "Salbris", "Nouans", "LB", "LFSA", "Orleans" };
int index = indexCities(cities, "Vierzon");
System.out.println("Index of city Vierzon is: " + index);
}
public static int indexCities(String cities[], String cityName) {
List<String> cityList = Arrays.asList(cities);
return cityList.indexOf(name);
}
Scope of variable nameCity is limited to main function. You can not access it outside of main function.
The variable is out of scope when you try to use it inside the method indexCities. One solution is making the variable nameCity an instance variable by moving it's definition out of the main method, but your code can be improved in several ways too. Check some option below:
This will print the index of the city you're looking for inside the array:
import java.util.Scanner;
public class villes {
public static void main(String[] args) {
String cities[] = {"Vierzon","Salbris","Nouans","LB","LFSA","Orleans"};
Scanner input = new Scanner(System.in);
String nameCity = input.nextLine();
indexCities(nameCity, cities);
}
public static void indexCities(String copyOfNameCity, String cities[]){
for (int i = 0; i < cities.length; i++) {
if(copyOfNameCity.equals(cities[i])) {
System.out.println(i);
break;
}
}
}
}
You you can improve it by making the method return a value. Like this:
import java.util.Scanner;
public class villes {
public static void main(String[] args) {
String cities[] = {"Vierzon","Salbris","Nouans","LB","LFSA","Orleans"};
Scanner input = new Scanner(System.in);
String nameCity = input.nextLine();
int cityIndex = indexCities(nameCity, cities);
System.out.println(cityIndex == -1 ? "City not found" : "City found in index " + cityIndex);
}
public static int indexCities(String nameCity, String cities[]){
for (int i = 0; i < cities.length; i++) {
if(nameCity.equals(cities[i])) {
return i;
}
}
return -1;
}
}
Another way is:
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class test2 {
public static void main(String[] args) {
String cities[] = {"Vierzon", "Salbris", "Nouans", "LB", "LFSA", "Orleans"};
Scanner input = new Scanner(System.in);
System.out.print("Enter the name of city to be searched -> ");
String nameCity = input.nextLine();
int cityIndex = indexCities(nameCity, cities);
System.out.println(cityIndex == -1 ? "City not found" : "Found at position " + cityIndex);
input.close();
}
public static int indexCities(String cityName, String cities[]) {
List<String> cityList = Arrays.asList(cities);
return cityList.indexOf(cityName);
}
}
I tested my program on http://www.hackerearth.com/problem/algorithm/roys-life-cycle/ . However, I always got Error: Main method not found in class ActivityTime, please define the main method as:public static void main(String[] args)
This is my program
import java.util.Scanner;
/**
* Created by DUY on 10/12/2014.
*/
class EatSleepCode {
public static void main(String[] args){
int numberDay = 0;
int numberMinuteOfDay = 18;
Scanner input = new Scanner(System.in);
numberDay = input.nextInt();
input.nextLine();
String[] str = new String[numberMinuteOfDay];
for(int i = 0; i < numberDay; i++){
str[i] = input.nextLine();
}
ActivityTime code = new ActivityTime(numberDay,str,'C');
code.findLongestTime();
}
}
class ActivityTime{
public int longestTimeOfDay;
public int longestTimeOfTotal;
public int numberDay;
public String[] str;
public char act;
public ActivityTime(int numberDay, String[] str, char act){
this.numberDay = numberDay;
this.str = str;
this.act = act;
}
public void findLongestTime(){
int tmp1 = 0, tmp2 = 0;
for(int i = 0; i < numberDay; i++){
tmp1 = 0;
for(int j = 0; j < str.length; j++){
if(str[i].charAt(j) != act){
tmp1 = 0;
tmp2 = 0;
}
else {
tmp1 ++;
tmp2 ++;
}
if(tmp1 > longestTimeOfDay){
longestTimeOfDay = tmp1;
}
if(tmp2 > longestTimeOfTotal){
longestTimeOfTotal = tmp2;
}
}
}
System.out.println(longestTimeOfDay + " " + longestTimeOfTotal );
}
}
Can you help me solve this error? Thank you very much
You ought to separate these classes into two files, one called EatSleepCode.java and one called ActivityTime.java.
Once you've done that, you'll be clearer on which one you're running as your main class. It's EatSleepCode that has the public static void main in it, so presumably that is what you intend as your main class; ActivityTime doesn't have such a method, which is why you can't run that as your main class. That's what the error means that you're getting.
This is my program that makes some calculations to a numbers into the array named "initialMarks". However I would like to fill the initialMarks array from another class using scanner.Could you help me to figure it out how to do that? Is it possible to outprint the result array "result" in a third class?
public class testingN
{
public static void main(String[] args)
{
int [] initialMarks = new int [4];
int [] result = new int [6];
result[0] = computedMarks(initialMarks[0], initialMarks[1])[0];
result[1] = computedMarks(initialMarks[2], initialMarks[3])[1];
for(int i=0; i< result.length; i++)
System.out.println(result[i]);
}
public static int [] computedMarks(int mark1, int mark2)
{
int [] i= new int [6];
for (int j = 0; j < i.length; j++)
{
if ((mark1 < 35 && mark2 > 35) || (mark1 > 35 && mark2 < 35))
{
i[j] = 35;
}
else
{
i[j] = (mark1 * mark2);
}
}
return i;
}
}
Your other class can have a method that returns a stream, and you can feed that into your Scanner's constructor.
In your other class's String getInitialMarks() method:
// Generate a string with all the "marks" as "stringWithMarks", separated by "\n" characters
InputStream is = new ByteArrayInputStream( stringWithMarks.getBytes( "UTF-8" ) );
return is;
Then in your first class, otherClass:
Scanner scanner = new Scanner(otherClass.getInitialMarks());
And proceed to read in the marks as if it were user input.
public class testingN
{
static int [] result = new int [6];
static int [] initialMarks = new int [4];
public static void main(String[] args)
{
Declaring result as class member (global variable) and being static should help your cause
Example: using from another class
public class AnotherClass {
public static void main(String[] args) {
// example
testingN.result[0] = 4;
System.out.println(testingN.result[0]);
}
}
Edit: Run the code below Here. You'll see it works just fine.
class testingN
{
static int [] result = new int [6];
static int [] initialMarks = new int [4];
}
public class AnotherClass {
public static void main(String[] args) {
// example
testingN.result[0] = 4;
System.out.println(testingN.result[0]);
}
}