Can't Use Starts/EndsWith with Scanner - arrays

Just wondering, is it not allowed to use Starts/EndsWith functions with Scanner? I'm trying to make a simple program where some elements from the set of array that I will provide will be printed out using those functions. But obviously there is an error that I don't know where it is. Please help
import java.util.Scanner;
public class test {
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
String[] animals = new String[5];
System.out.println("Enter animal type");
for (String j : animals){
if (j.startsWith("do"))
System.out.println(j);
}
}
}

Related

ArrayList and initializing a 2D array not working?

I'm learning the AP CSA course and tried to print these arrays. It keeps saying "can't find symbol" and not gonna lie I have absolutely no idea what that means.
//ArrayList:
public class al{
public static void main(String[] args){
List l = new ArrayList();
System.out.print(l.size());
}
}
//2DArray:
public class arrays{
public static void main(String[] args){
int[][] arry = {{1,2,3},
{1,2,3},
{1,2,3}};
printArray(arry);
}
}
In your first program to print the ArrayList, it is failing because you need to import the List and ArrayList in your program, please try this
import java.util.ArrayList;
import java.util.List;
public class al{
public static void main(String[] args){
List l = new ArrayList();
System.out.print(l.size());
}
}
In the second program the printArray method you called is missing in definition, i.e you have to define the printArray method in your class, you are using it from method main then you need to declare it static like below
static void printArray(int[][] arr){
for(int i=0; i<arr.length; i++){
String row = "";
for(int j=0; j<arr[0].length;j++){
row+=arr[i][j]+" ";
}
System.out.println(row);
}
}
Also while you are learning, please also read about the naming conventions in java (i suppose you are learning java), like a Class name should start from a capital letter.

How to pass a list element as function parameter?

I have a char array: my_array={"a","b","c"}
And I need to get 1 specific element and put it as function parameter
For example: function myFunction({"b"}){}
I tried this but got an error:
unexpected character
I learned from TTCN-3 and I try this on it.
This will help you. Use this kind of code will help you to pass one parameter each time.
public class MyClass {
public static void main(String args[]) {
MyClass mc = new MyClass();
char my_array[]={'a','b','c'};
char returnValue;
for(int i=0;i<my_array.length;i++){
returnValue = mc.charReturn(my_array[i]);
System.out.println("Got it from method:"+returnValue);
}
}
public char charReturn(char fromClass){
return fromClass;
}
}

How do I fix in.nextLine() returning an array of empty strings?

im very new to this, working on some ap comp sci problems
i want to store user inputs into an array list of strings, but when i print the array im trying to fill, i just get something like [, , , ] how can i fix this?
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Enter words, followed by the word \"exit\"");
Scanner in = new Scanner(System.in);
ArrayList<String> words = new ArrayList<String>();
WordList test = new WordList(words);
while(!in.next().equals("exit"))
{
words.add(in.nextLine());
}
System.out.println(words);
The problem you have is that in the while loop condition you read what the user typed in the input, and then inside the while you read am empty line.
You need to read the line only inside the while, and break the loop if the user typed 'exit'. As follows:
public static void main(String[] args) {
System.out.println("Enter words, followed by the word \"exit\"");
Scanner in = new Scanner(System.in);
ArrayList<String> words = new ArrayList<String>();
while (true) {
String str = in.nextLine();
if ("exit".equals(str)) {
break;
}
words.add(str);
}
System.out.println(words);
}

How to define an array in hadoop partitioner

I am new in hadoop and mapreduce programming and don't know what should i do. I want to define an array of int in hadoop partitioner. i want to feel in this array in main function and use its content in partitioner. I have tried to use IntWritable and array of it but none of them didn't work . I tried to use IntArrayWritable but again it didn't work. I will be pleased if some one help me. Thank you so much
public static IntWritable h = new IntWritable[1];
public static void main(String[] args) throws Exception {
h[0] = new IntWritable(1);
}
public static class CaderPartitioner extends Partitioner <Text,IntWritable> {
#Override
public int getPartition(Text key, IntWritable value, int numReduceTasks) {
return h[0].get();
}
}
if you have limited number of values, you can do in the below way.
set the values on the configuration object like below in main method.
Configuration conf = new Configuration();
conf.setInt("key1", value1);
conf.setInt("key2", value2);
Then implement the Configurable interface for your Partitioner class and get the configuration object, then key/values from it inside your Partitioner
public class testPartitioner extends Partitioner<Text, IntWritable> implements Configurable{
Configuration config = null;
#Override
public int getPartition(Text arg0, IntWritable arg1, int arg2) {
//get your values based on the keys in the partitioner
int value = getConf().getInt("key");
//do stuff on value
return 0;
}
#Override
public Configuration getConf() {
// TODO Auto-generated method stub
return this.config;
}
#Override
public void setConf(Configuration configuration) {
this.config = configuration;
}
}
supporting link
https://cornercases.wordpress.com/2011/05/06/an-example-configurable-partitioner/
note if you have huge number of values in a file then better to find a way to get cache files from job object in Partitioner
Here's a refactored version of the partitioner. The main changes are:
Removed the main() which isnt needed, initialization should be done in the constructor
Removed static from the class and member variables
public class CaderPartitioner extends Partitioner<Text,IntWritable> {
private IntWritable[] h;
public CaderPartitioner() {
h = new IntWritable[1];
h[0] = new IntWritable(1);
}
#Override
public int getPartition(Text key, IntWritable value, int numReduceTasks) {
return h[0].get();
}
}
Notes:
h doesn't need to be a Writable, unless you have additional logic not included in the question.
It isn't clear what the h[] is for, are you going to configure it? In which case the partitioner will probably need to implement Configurable so you can use a Configurable object to set the array up in some way.

Arrays? or not?

i am not an expert in java, but i need to solve this problem/activity for my course subject, that's why i really need your help guys. I have a programming problem. thing is i can't figure out what method or java codes should i use for this problem:
Create a class address book that can contain 100 entries of Name, Address, contact number and email address.
You should provide the following methods for the address book:
Add entry, Delete entry, View all entries and Update an entry
UPDATE: this is the codes I got so far
I am thinking i could use 2d array for this but, as soon as i start coding i can't really continue further, I don't know if its possible to use array or not in this kind of activity. I tried searching for other java codes but the more I learned new techniques or codes that might be possible, the more i got confused on WHAT codes must I use!
if anyone can help me build the coding for this activity I would really apprecaite and will surely study how the hell will/can YOU do it! because im really interested in learning Java, I just need some help to realize how should i DO this. thanks in advance!
THIS ARE THE CODES I'VE GOT SO FAR:
the capability of these program is only for adding editing viewing and deleting NAMES, iam still figuring out how to add dimensions to my array or should I add? or if not array? HoW? how am I supposed to answer this activity prior tto its REQUIREMENTS :(
package javaactivities;
import java.util.*;
import java.util.Scanner;
public class AddressBook {
static List<String> l=new ArrayList<String>();
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
boolean y=true;
do{
System.out.println("Enter \n1 to add an entry\n2 to edit entry");
System.out.println("3 to delete an entry\n4 to view entries\n5 to exit");
System.out.print("enter your choice: ");
int choice=in.nextInt();
switch(choice)
{
case 1:
insert();
break;
case 2:
edit();
break;
case 3:
delete();
break;
case 4:
print();
break;
case 5:
toexit();
break;
default:
System.out.println("bad input");
break;
}
System.out.println("want to process more? y/n");
String x=in.next();
char ch=x.charAt(0);
if( ch=='n')
y=false;
}
while(y!=false);
}
static public void insert(){
Scanner in=new Scanner(System.in);
boolean y=true;
do{
System.out.println("enter name to add in list");
String entry=in.next();
l.add(entry);
System.out.println("want to insert more?y/n");
String x=in.next();
char ch=x.charAt(0);
if( ch=='n')
y=false;
}
while(y!=false);
}
static public void print(){
if(l.isEmpty())
System.out.println("list is empty ");
else
System.out.println("members of lists are:");
for(int i=0 ; i<l.size();i++)
System.out.println("Entry "+i+" : "+ l.get(i)+" ");
}
static public void edit(){
Scanner in=new Scanner(System.in);
String num2;
System.out.println("enter name you want to add");
num2=in.next();
try{
System.out.println("enter entry # of the name you want to edit");
int num1=in.nextInt();
l.set(num1, num2);
}catch(IndexOutOfBoundsException e){
System.err.println("caught IndexOutOfBoundsException: specified position is empty "+e.getMessage());
}
}
static public void delete(){
Scanner in=new Scanner(System.in);
System.out.println("enter entry # you want to delete");
int num=in.nextInt();
l.remove(num);
}
static public void toexit(){
System.exit(0);
}
}
First, implement all the required classes, two in your case:
AddressBook
Entry
The skeleton could be something like that
public final class AddressBook {
public static final class Entry {
private String name;
private String address;
private String contactNumber;
private String email;
private Entry(String name, String address, String contactNumber, String email) {
this.name = name;
this.address = address;
this.contactNumber = contactNumber;
this.email = email;
}
public String getName() {return name;}
public String getAddress() {return address;}
public String getContactNumer() {return contactNumber;}
public String getEmail() {return email;}
}
private ArrayList<Entry> entries = new ArrayList<Entry>();
public AddressBook() {;}
public int size() {return entries.size();}
public int get(int index) {return entries.get(index);}
...
public Entry add(String name, String address, String contactNumber, String email) {
Entry entry = new Entry(name, address, contactNumber, email);
entries.add(entry);
return entry;
}
...
}
In order to implement viewAll() you could choose to override toString() methods in both classes, to delete() it seemes that you have to implement find() as well etc. Then just use these classes
public final class Main {
private static AddressBook book = new AddressBook();
public static void main(String[] args) {
...
switch(choice) {
case 1:
book.add(...);
break;
case 2:
book.delete(...);
break;
...
}
System.out.println(book.toString());
...
}
}
As you have several entries to store my suggestion is a linked list. Linked list is a data structure where you can store multiple entries.Java provide a
http://www.mycstutorials.com/articles/data_structures/linkedlists
this link will help you with linked list.

Resources