I keep running into the same complier error in my program. Any ideas on what is stopping the program from compiling. The error I recieve is ----jGRASP wedge2: exit code for process is 1.
import java.util.Scanner;
public class CheckerboardV1
{
static Scanner keyboard = new Scanner(System.in);
static final char FULL_CHAR = '*';
static final char EMPTY_CHAR = '-';
public static void main(String[] args)
{
System.out.println("Checkerboard Version 1 ...");
System.out.print("Enter size:>");
int size = keyboard.nextInt();
row(size);
}
public static int row(int size)
{
for(int i=0;i<size;i++) //How Deep
{
for(int n=0;n<4;n++) //Fills one line
{
for(int m=0;m<size;m++) //Empty Characters
{
System.out.print(EMPTY_CHAR);
}
for(int m=0;m<size;m++)
{
System.out.print(FULL_CHAR); //Full Characters
}
}
System.out.println(); //Starts new Line
return size;
}
}
}
Related
I am trying to load data from a txt file and it will only read one line of the txt file. When I specify what the int I variable is in my for loop within my loadData method it will print that particular line. I am not sure why it won't just add and print all my data.
I tried using an outer for loop to see if would print and add the data that way, but no luck
import java.io.*;
import java.util.*;
public class BingoSortTest
{
static BingoPlayer [] test;
public static void main (String [] args) throws IOException
{
Scanner keyboard = new Scanner(System.in);
test = new BingoPlayer [10];
loadData();
System.out.print(Arrays.toString(test));
}
public static void loadData() throws IOException
{
Scanner S = new Scanner(new FileInputStream("players.txt"));
double houseMoney = S.nextDouble();
S.nextLine();
int player = S.nextInt();
S.nextLine();
for(int i = 0; i < test.length; i++)
{
String line = S.nextLine();
String [] combo = line.split(",");
String first = combo [0];
String last = combo [1];
double playerMoney = Double.parseDouble(combo[2]);
BingoPlayer plays = new BingoPlayer(first, last, playerMoney);
add(plays);
}
}
public static void add(BingoPlayer d)
{
int count = 0;
if (count< test.length)
{
test[count] = d;
count++;
}
else
System.out.println("No room");
}
}
Here is the contents of the txt file I am using:
50.00
10
James,Smith,50.0
Michael,Smith,50.0
Robert,Smith,50.0
Maria,Garcia,50.0
David,Smith,50.0
Maria,Rodriguez,50.0
Mary,Smith,50.0
Maria,Hernandez,50.0
Maria,Martinez,50.0
James,Clapper,50.0
Every Time you put a BingoPlayer at Index 0 .
public static void add(BingoPlayer d)
{
int count = 0; // <-------------------- Here
if (count< test.length)
{
test[count] = d;
count++;
}
else
System.out.println("No room");
}
you have to define static counter variable where array of BingoPlayer is defined.
define count variable static
static BingoPlayer [] test;
static int count = 0;
and chane the add function definition like this.
public static void add(BingoPlayer d)
{
if (count< test.length) {
test[count] = d;
count++;
}
else
System.out.println("No room");
}
I am trying to create a method that counts the number of times a specified character appears in a given string, by printing the sentence and the specified character and its count.
This is what I have completed so far:-
import java.util.Scanner;
public class Program4 {
public int count ( String sentence, char letter)
{
int times = 0;
for (int x=0;x<sentence.length();x++)
{
if (sentence.charAt(x) ==letter){times++;}
}
return times;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Program4 program4 = new Program4();
}
program4.count("Hello World",'o');
scan.close();
}
}
I understand I need a "system.output.println" but I don't know what value goes inside to get the output I am looking for. I would appreciate any help, I am beginner with java. Thank you.
it works, you had compile time error in code
import java.util.Scanner;
public class Program4 {
public int count(String sentence, char letter) {
int times = 0;
for (int x = 0; x < sentence.length(); x++) {
if (sentence.charAt(x) == letter) {
times++;
}
}
return times;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Program4 program4 = new Program4();
int timesCount = program4.count("Hello World", 'o');
System.out.println("count is :" + timesCount);
scan.close();
}
}
I am trying to read data from a text file named Marathon_Data.txt and when I run, this error shows up:
"Exception in thread "main" java.util.InputMismatchException"
We had not yet learned how to handle exceptions, so thus far have only been using throws IOException. Would this be causing the issue?
Thanks
import java.io.*;
import java.util.Scanner;
public class Marathon
{
final static int SIZE = 5;
public static void main(String[] args) throws IOException
{
int miles[][] = new int[SIZE][7];
String names[] = new String[SIZE];
//variables
int i=0, j=0;
int totalWeek = 0;
double average = 0.0;
//opening file
File file = new File("Marathon_Data.txt");
Scanner inputFile = new Scanner(file);
while (inputFile.hasNext())
{
j=0;
names[i]=inputFile.next();
while(inputFile.hasNext())
{
miles[i][j]=inputFile.nextInt();
j++;
}
i++;
}
//Display total miles ran in each week by each runner and their average
System.out.println("Name Total Miles Average");
for(i=0;i<miles.length;i++)
{
System.out.print(names[i]);
for(j=0;j<miles[0].length;j++)
{
totalWeek +=miles[i][j];
}
average = (double)totalWeek / miles[0].length;
System.out.printf("%d\t\t%.2f\n", totalWeek, average);
totalWeek = 0;
}
}//end main method
}//end class
After trying out a few ways to fix my code from an arrayindexoutofbound exception, there's one way it wouldn't produce the exception, however the program would not output anything like it should, it would not throw exceptions or errors, and it shows that the program is not terminated. Here's the code:
import java.util.*;
public class treez {
final Scanner in;
public treez()
{
in=new Scanner(System.in);
run();
in.close();
}
public void run()
{
System.out.println("Enter normal restaurant numbers and pho restaurant numbers");
final String input=in.nextLine();
final int[] rest=createarray(input);
final int normal=rest[0];
final int pho=rest[1];
final String input1=in.nextLine();
final int[] phoresspecific=createarray(input1);
Arrays.sort(phoresspecific);
boolean[][] matrix;
matrix=new boolean[normal][normal];
for(int i=0;i<normal;i++)
{
for(int j=0;j<normal;j++)
{
matrix[i][j]=false;
}
}
int[] total=new int[normal];
for(int i=0;i<matrix.length;i++) matrix[i]=new boolean[normal];
for(int i=0;i<normal;i++)
{
total[i]=i;
total[i]=Arrays.binarySearch(total, i);
}
Arrays.sort(total);
for(int i=0 ; i < normal - 1; i++) {
int x = 0;
int y = 0;
String ans = in.nextLine();
int[] arraya = createaarray(ans);
x = arraya[0];
y = arraya[1];
matrix[x][y]= true;
matrix[y][x]=true;
}
in.close();
int answer=decisions(pho,matrix,phoresspecific,normal);
System.out.println(answer);
}
private int decisions(int pho, boolean matrix[][],int[] phoresspecific,int normal)
{
int steps=0;
if(pho==2)
{
for(int j=0;j<normal-1;j++)
{
for(int i=phoresspecific[0];i<normal-1;i++)
{
if(matrix[j][i]==true)
{
j++;
steps++;
if(j==phoresspecific[1]) break;
}
}
if(j==phoresspecific[1]) break;
}
}
else
{
int counter=0;
int[] step=new int[pho-1];
for(int j=0;j<pho-1;j++)
{
counter=0;
for(int i=0;i<normal-1;i++)
{
for(int l=phoresspecific[j];l<normal-1;l+=0)
{
if(matrix[i][l]==true)
{
i++;
step[j]++;
if(i==j)
{
counter++;
}
}
}
if(counter==pho) break;
}
}
Arrays.sort(step);
steps=step[0];
}
steps--;
return(steps);
}
private int[] createarray(final String input)
{
final String[] args=input.trim().split("\\s+");
final int[] list=new int[2];
for(int i=0;i<2;i++)
list[i]=Integer.parseInt(args[i]);
return list;
}
private int[] createaarray(final String ans)
{
final String[] args=ans.trim().split("\\s+");
final int[] list=new int[2];
for(int i=0;i<2;i++)
list[i]=Integer.parseInt(args[i]);
return list;
}
public static void main(String args[])
{
new treez();
}
}
From the previous mistakes, I believe the error is either in line 83 or 85, which are:
for(int l=phoresspecific[j];l<normal-1;l+=0)
{
if(matrix[i][l]==true)
in
for(int l=phoresspecific[j];l<normal-1;l+=0)
{
if(matrix[i][l]==true)
{
i++;
step[j]++;
if(i==j)
{
counter++;
}
}
}
With input:
8 5
0 6 4 3 7
0 1
0 2
2 3
4 3
6 1
1 5
7 3
The program would neither produce an output or terminate. Help would be appreciated.
Edit:the exception:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8
at treez.decisions(treez.java:85)
at treez.run(treez.java:52)
at treez.<init>(treez.java:9)
at treez.main(treez.java:127)
Edit2:This program is used to determine the minimum time used for a person to reach all the locations wrote in from input line 2. There's no edge weight since it takes one minute from one place to another. All the lines after second lines states there is a road connecting place a and b.
Have you checkt your build configurations?
And its kind a hard looking for possible errors when you provide not a lot of information about your program.
EDIT: I managed to get it running and can confirm that you program (with your input does not return anything and I didn't step trough every piece of code)
But you always use the "createarray" function, which you have two times implemented and its the same function (exept of the name of the value you pass).
Then you have an infinite loop in this code:
for(int i=0;i<normal-1;i++){
for(int l=phoresspecific[j];l<normal-1;l+=0){
if(matrix[i][l]==true){
i++;
step[j]++;
if(i==j) counter++;
}
}
if(counter==pho) break;
}
The inner for-loop doesnt increment its value and is always trying to evaluate the if condition wich will always stay the same due to the never changing index l . That is probably your error.
Another EDIT:
In the second line of your parameters you have more than two values, but your functions always read only two parameters.
import java.util.Scanner;
class Sorting
{
static int m,n;
static int x=0;`enter code here`
static int b[]=new int[m*n];
static int a[][]=new int[m][n];
static void print()
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(+a[i][j]);
}
System.out.println();
}
}
static void convertBack()
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
a[i][j]=b[x];
x++;
}
}
}
static void sort()
{
for(int i=0;i<m*n;i++)
{
for(int j=0;j<m*n;j++)
{
if(b[i]>b[j])
{
int temp=b[i];
b[i]=b[j];
b[j]=temp;
}
else
{
continue;
}
}
}
}
static void convert()
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
b[x]=a[i][j];
x++;
}
}
}
static void enterArray()
{
Scanner in=new Scanner(System.in);
System.out.println("Enter the elements");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
a[i][j]=Integer.parseInt(in.nextLine());
}
}
}
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
System.out.println("Enter the number of rows");
m=Integer.parseInt(in.nextLine());
System.out.println("Enter the number of columns");
n=Integer.parseInt(in.nextLine());
enterArray();
convert();
sort();
convertBack();
print();
}
}
The above code compiles fine however on running i am getting an error as follows:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Sorting.enterArray(2dsort.java:76)
at Sorting.main(2dsort.java:87)
please help !
You are initializing your arrays before setting the values of m and n, so you get empty arrays :
static int m,n; // both are 0 by default
static int b[]=new int[m*n]; // equivalent to new int [0];
static int a[][]=new int[m][n]; // equivalent to new int [0][0];
You should create your arrays after initializing m and n :
m=Integer.parseInt(in.nextLine());
System.out.println("Enter the number of columns");
n=Integer.parseInt(in.nextLine());
b=new int[m*n];
a=new int[m][n];
...